Skip to content

Commit

Permalink
Added documentation on authentication and authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 20, 2014
1 parent 8e69754 commit 80e49da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -33,6 +33,7 @@ are up to you.
* [Versioning APIs][versioning-apis]
* [Routes][routes]
* [Error handling][error-handling]
* [Authentication and authorization][authentication-and-authorization]
* [API documentation](http://rubydoc.info/gems/webmachine/frames/file/README.md)
* [Mailing list](mailto:webmachine.rb@librelist.com)
* IRC channel #webmachine on freenode
Expand Down Expand Up @@ -165,3 +166,4 @@ LICENSE for details.
[versioning-apis]: /documentation/versioning-apis.md
[routes]: /documentation/routes.md
[error-handling]: /documentation/error-handling.md
[authentication-and-authorization]: /documentation/authentication-and-authorization.md
37 changes: 37 additions & 0 deletions documentation/authentication-and-authorization.md
@@ -0,0 +1,37 @@
# Authentication

To secure a resource, override the `is_authorized?` method to return a boolean indicating whether or not the client is authenticated (ie. your application believes they are who they say they are). Confusingly, the HTTP "401 Unauthorized" response code actually relates to authentication, not authorization (see the [Authorization](#authorization) section below).

## HTTP Basic Auth

```ruby

class MySecureResource < Webmachine::Resource

include Webmachine::Resource::Authentication

def is_authorized?(authorization_header)
basic_auth(authorization_header, "My Application") do |username, password|
@user = User.find_by_username(username)
!@user.nil? && @user.auth?(password)
end
end

end

```

# Authorization

Once the client is authenticated (that is, you believe they are who they say they are), override `forbidden?` to return true if the client does not have permission to perform the given method this resource.

```ruby

class MySecureResource < Webmachine::Resource

def forbidden?
MySecureResourcePolicy.new(@user, @my_domain_model).forbidden?(request.method)
end

end
```

0 comments on commit 80e49da

Please sign in to comment.