Class: HaveAPI::Authentication::OAuth2::Config
- Inherits:
-
Object
- Object
- HaveAPI::Authentication::OAuth2::Config
- Defined in:
- lib/haveapi/authentication/oauth2/config.rb
Overview
Config passed to the OAuth2 provider
Create your own subclass and pass it to with_config. The created provider can then be added to authentication chain.
In general, it is up to the implementation to provide the authentication flow -- render HTML page in #render_authorize_page and then process it in #handle_post_authorize. The implementation must also handle generation of all needed tokens, their persistence and validity checking.
Instance Method Summary collapse
-
#authorize_path ⇒ String
Path to the authorization endpoint on this API.
-
#find_authorization_by_code(client, code) ⇒ Authorization?
Find authorization by code.
-
#find_authorization_by_refresh_token(client, refresh_token) ⇒ Authorization?
Find authorization by refresh token.
-
#find_client_by_id(client_id) ⇒ Client?
Find client by ID.
-
#find_user_by_access_token(request, access_token) ⇒ Object?
Find user by the bearer token sent in HTTP header or as a query parameter.
-
#get_authorization_code(auth_res) ⇒ String
Get oauth2 authorization code.
-
#get_tokens(authorization, sinatra_request) ⇒ Array
Get access token, its expiration date and optionally a refresh token.
-
#handle_post_authorize(sinatra_request, sinatra_params, oauth2_request, client) ⇒ AuthResult?
Handle POST requests made from #render_authorize_page.
-
#initialize(provider, server, v) ⇒ Config
constructor
A new instance of Config.
-
#oauth2_params(req) ⇒ Hash<String, String>
Parameters needed for the authorization process.
-
#refresh_tokens(authorization, sinatra_request) ⇒ Array
Refresh access token and optionally generate new refresh token.
-
#render_authorize_page(oauth2_request, sinatra_params, client, auth_result: nil) ⇒ String
Render authorization page.
Constructor Details
#initialize(provider, server, v) ⇒ Config
Returns a new instance of Config.
13 14 15 16 17 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 13 def initialize(provider, server, v) @provider = provider @server = server @version = v end |
Instance Method Details
#authorize_path ⇒ String
Path to the authorization endpoint on this API
122 123 124 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 122 def @provider. end |
#find_authorization_by_code(client, code) ⇒ Authorization?
Find authorization by code
100 101 102 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 100 def (client, code) end |
#find_authorization_by_refresh_token(client, refresh_token) ⇒ Authorization?
Find authorization by refresh token
108 109 110 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 108 def (client, refresh_token) end |
#find_client_by_id(client_id) ⇒ Client?
Find client by ID
92 93 94 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 92 def find_client_by_id(client_id) end |
#find_user_by_access_token(request, access_token) ⇒ Object?
Find user by the bearer token sent in HTTP header or as a query parameter
116 117 118 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 116 def find_user_by_access_token(request, access_token) end |
#get_authorization_code(auth_res) ⇒ String
Get oauth2 authorization code
Called when the authentication is successful and complete. This method must generate and return authorization_code which is then sent to the client. It is up to the API implementation to persist the code.
60 61 62 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 60 def (auth_res) end |
#get_tokens(authorization, sinatra_request) ⇒ Array
Get access token, its expiration date and optionally a refresh token
The client has used the authorization_code returned by #get_authorization_code and now requests its access token. It is up to the implementation to create and persist the tokens. The authorization code should be invalidated.
73 74 75 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 73 def get_tokens(, sinatra_request) end |
#handle_post_authorize(sinatra_request, sinatra_params, oauth2_request, client) ⇒ AuthResult?
Handle POST requests made from #render_authorize_page
Process form data and return AuthResult or nil. When nil is returned the authorization process is aborted and the user is redirected back to the client.
48 49 50 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 48 def (sinatra_request, sinatra_params, oauth2_request, client) end |
#oauth2_params(req) ⇒ Hash<String, String>
Parameters needed for the authorization process
Use these in #render_authorization_page, put them e.g. in hidden form fields.
132 133 134 135 136 137 138 139 140 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 132 def oauth2_params(req) { client_id: req.client_id, response_type: req.response_type, redirect_uri: req.redirect_uri, scope: req.scope.join(' '), state: req.state, } end |
#refresh_tokens(authorization, sinatra_request) ⇒ Array
Refresh access token and optionally generate new refresh token
The implementation should invalidate the current tokens and generate and persist new ones.
85 86 87 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 85 def refresh_tokens(, sinatra_request) end |
#render_authorize_page(oauth2_request, sinatra_params, client, auth_result: nil) ⇒ String
Render authorization page
This method can be called on both GET and POST requests, e.g. if the user provided incorrect credentials or if there are multiple authentication steps.
It should return full HTML page that will be sent to the user. The page usually contains a login form.
33 34 35 |
# File 'lib/haveapi/authentication/oauth2/config.rb', line 33 def (oauth2_request, sinatra_params, client, auth_result: nil) end |