Warden::OAuth2
This library provides a robust set of authorization strategies for
Warden meant to be used to implement an OAuth 2.0 (draft 22)
provider.
Usage
Grape API Example
require 'grape'
require 'warden-oauth2'
class MyAPI < Grape::API
use Warden::Manager do |config|
strategies.add :bearer, Warden::OAuth2::Strategies::Bearer
strategies.add :client, Warden::OAuth2::Strategies::Client
strategies.add :public, Warden::OAuth2::Strategies::Public
config.default_strategies :bearer, :client, :public
config.failure_app Warden::OAuth2::FailureApp
end
helpers do
def warden; env['warden'] end
end
resources :hamburgers do
before do
warden.authenticate! scope: :hamburgers
end
end
end
Configuration
You can configure Warden::OAuth2 like so:
Warden::OAuth2.configure do |config|
config.some_option = some_value
end
Configurable Options
- client_model: A client application class. See Models below.
Defaults to
ClientApplication
. - token_model: An access token class. See Models below. Defaults
to
AccessToken
.
Models
You will need to supply data models to back up the persistent facets of
your OAuth 2.0 implementation. Below are examples of the interfaces that
each require.
Client Application
class ClientApplication
def self.locate(client_id, client_secret = nil)
end
def scope?(scope)
end
end
Access Token
class AccessToken
def self.locate(token_string)
end
def expired?
end
def scope?(scope)
end
end
Strategies
Bearer
This strategy authenticates by trying to find an access token that is
supplied according to the OAuth 2.0 Bearer Token specification
(draft 8). It does this by first extracting the access
token in string form and then calling the .locate
method on your
access token model (see Configuration above).
Token-based strategies will also fail if they are expired or lack
sufficient scope. See Models above.
User: The Warden user is set to the client application returned by
.locate
.
Client
This strategy authenticates an OAuth 2.0 client application directly for
endpoints that don't require a specific user. You might use this
strategy when you want to create an API for client statistics or if you
wish to rate limit based on a client application even for publicly
accessible endpoints.
User: The Warden user is set to the access token returned by .locate
.
Public
This strategy succeeds by default and only fails if the authentication
scope is set and is something other than :public
.
User: The Warden user is set to nil
.
License
Copyright (C) 2011 by Michael Bleigh and Opperator, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.