I''m developing an API for my site, and I''m wondering how I can make it so requests require basic access authentication (like in Twitter''s API for example)? I''m using restful-authentication on Rails 2.3.2. Thanks!
Marnen Laibow-Koser
2009-Jun-18 03:48 UTC
Re: How can I implement authentication in my API?
Andrew wrote:> I''m developing an API for my site, and I''m wondering how I can make it > so requests require basic access authentication (like in Twitter''s API > for example)?[...] I''m no expert on this, but perhaps you want to use OAuth like Twitter has done. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
There''s already support for basic auth in restful-authentication; it only gets used by default for (off the top of my head) JSON and XML formatted requests. The support is pretty simple - take a look at (in restful_auth) AuthenticatedSystem#access_denied, and also the core docs for ActionController::HttpAuthentication. --Matt Jones On Jun 17, 9:59 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m developing an API for my site, and I''m wondering how I can make it > so requests require basic access authentication (like in Twitter''s API > for example)? I''m using restful-authentication on Rails 2.3.2. Thanks!
Thanks Matt, that''s exactly what I was looking for. On Jun 18, 10:06 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There''s already support for basic auth in restful-authentication; it > only gets used by default for (off the top of my head) JSON and XML > formatted requests. The support is pretty simple - take a look at (in > restful_auth) AuthenticatedSystem#access_denied, and also the core > docs for ActionController::HttpAuthentication. > > --Matt Jones > > On Jun 17, 9:59 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m developing an API for my site, and I''m wondering how I can make it > > so requests require basic access authentication (like in Twitter''s API > > for example)? I''m using restful-authentication on Rails 2.3.2. Thanks!
Can you post your code how you fixed it please! I am using also restful authentication and want to provide basic authenticaton for xml! Thank you Wouter On 18 jun, 19:06, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There''s already support for basic auth in restful-authentication; it > only gets used by default for (off the top of my head) JSON and XML > formatted requests. The support is pretty simple - take a look at (in > restful_auth) AuthenticatedSystem#access_denied, and also the core > docs for ActionController::HttpAuthentication. > > --Matt Jones > > On Jun 17, 9:59 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m developing an API for my site, and I''m wondering how I can make it > > so requests require basic access authentication (like in Twitter''s API > > for example)? I''m using restful-authentication on Rails 2.3.2. Thanks!
def authenticate @current_user = authenticate_or_request_with_http_basic { |u, p| User.authenticate(u, p) } end Use as a before filter. On Jul 16, 11:18 am, Wouter <wouterg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can you post your code how you fixed it please! > I am using also restful authentication and want to provide basic > authenticaton for xml! > > Thank you > > Wouter > > On 18 jun, 19:06, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > There''s already support for basic auth in restful-authentication; it > > only gets used by default for (off the top of my head) JSON and XML > > formatted requests. The support is pretty simple - take a look at (in > > restful_auth) AuthenticatedSystem#access_denied, and also the core > > docs for ActionController::HttpAuthentication. > > > --Matt Jones > > > On Jun 17, 9:59 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I''m developing an API for my site, and I''m wondering how I can make it > > > so requests require basic access authentication (like in Twitter''s API > > > for example)? I''m using restful-authentication on Rails 2.3.2. Thanks!
But with this code i have to authenticate every time when i try to access the website.. I have this code for authenticate: def authenticate case request.format when Mime::XML, Mime::ATOM if self.user = authenticate_or_request_with_http_basic { |u, p| User.authenticate(u, p) } @current_user = user else request_http_basic_authentication end else user = User.find_by_remember_token(cookies[:auth_token]) end end But when i try to access an xml resource i get the authentication dialog and i get a page with error (ouldn''t find Profile without an ID) and when i refresh i get the right xml page.. How can i fix this.. My authentication doesnt work because of this on android! Thank you On Jul 16, 8:36 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def authenticate > @current_user = authenticate_or_request_with_http_basic { |u, p| > User.authenticate(u, p) } > end > > Use as a before filter. > > On Jul 16, 11:18 am, Wouter <wouterg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Can you post your code how you fixed it please! > > I am using also restful authentication and want to provide basic > > authenticaton for xml! > > > Thank you > > > Wouter > > > On 18 jun, 19:06, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > There''s already support for basic auth in restful-authentication; it > > > only gets used by default for (off the top of my head) JSON and XML > > > formatted requests. The support is pretty simple - take a look at (in > > > restful_auth) AuthenticatedSystem#access_denied, and also the core > > > docs for ActionController::HttpAuthentication. > > > > --Matt Jones > > > > On Jun 17, 9:59 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m developing an API for my site, and I''m wondering how I can make it > > > > so requests require basic access authentication (like in Twitter''s API > > > > for example)? I''m using restful-authentication on Rails 2.3.2. Thanks! > >
But with this code i have to authenticate every time when i try to access the website.. I have this code for authenticate: def authenticate case request.format when Mime::XML, Mime::ATOM if self.user = authenticate_or_request_with_http_basic { |u, p| User.authenticate(u, p) } @current_user = user else request_http_basic_authentication end else user = User.find_by_remember_token(cookies[:auth_token]) end end But when i try to access an xml resource i get the authentication dialog and i get a page with error (ouldn''t find Profile without an ID) and when i refresh i get the right xml page.. How can i fix this.. My authentication doesnt work because of this on android! Thank you On Jul 17, 11:20 pm, Wouter <wouterg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> But with this code i have to authenticate every time when i try to > access the website.. > > I have this code for authenticate: > > def authenticate > case request.format > when Mime::XML, Mime::ATOM > if self.user = authenticate_or_request_with_http_basic { |u, > p| User.authenticate(u, p) } > @current_user = user > else > request_http_basic_authentication > end > else > user = User.find_by_remember_token(cookies[:auth_token]) > end > end > > But when i try to access an xml resource i get the authentication > dialog and i get a page with error (ouldn''t find Profile without an > ID) > and when i refresh i get the right xml page.. How can i fix this.. My > authentication doesnt work because of this on android! > > Thank you > > On Jul 16, 8:36 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > def authenticate > > @current_user = authenticate_or_request_with_http_basic { |u, p| > > User.authenticate(u, p) } > > end > > > Use as a before filter. > > > On Jul 16, 11:18 am, Wouter <wouterg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Can you post your code how you fixed it please! > > > I am using also restful authentication and want to provide basic > > > authenticaton for xml! > > > > Thank you > > > > Wouter > > > > On 18 jun, 19:06, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > There''s already support for basic auth in restful-authentication; it > > > > only gets used by default for (off the top of my head) JSON and XML > > > > formatted requests. The support is pretty simple - take a look at (in > > > > restful_auth) AuthenticatedSystem#access_denied, and also the core > > > > docs for ActionController::HttpAuthentication. > > > > > --Matt Jones > > > > > On Jun 17, 9:59 pm, Andrew <luckyd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I''m developing an API for my site, and I''m wondering how I can make it > > > > > so requests require basic access authentication (like in Twitter''s API > > > > > for example)? I''m using restful-authentication on Rails 2.3.2. Thanks! > >