Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added documentation on authentication and authorization
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |