Class: HaveAPI::Authentication::OAuth2::Config

Inherits:
Object
  • Object
show all
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

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_pathString

Path to the authorization endpoint on this API

Returns:

  • (String)


122
123
124
# File 'lib/haveapi/authentication/oauth2/config.rb', line 122

def authorize_path
  @provider.authorize_path
end

#find_authorization_by_code(client, code) ⇒ Authorization?

Find authorization by code

Parameters:

  • client (Client)
  • code (String)

Returns:



100
101
102
# File 'lib/haveapi/authentication/oauth2/config.rb', line 100

def find_authorization_by_code(client, code)

end

#find_authorization_by_refresh_token(client, refresh_token) ⇒ Authorization?

Find authorization by refresh token

Parameters:

  • client (Client)
  • refresh_token (String)

Returns:



108
109
110
# File 'lib/haveapi/authentication/oauth2/config.rb', line 108

def find_authorization_by_refresh_token(client, refresh_token)

end

#find_client_by_id(client_id) ⇒ Client?

Find client by ID

Parameters:

  • client_id (String)

Returns:



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

Parameters:

  • sinatra_request (Sinatra::Request)
  • access_token (String)

Returns:

  • (Object, nil)

    user



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.

Parameters:

Returns:

  • (String)


60
61
62
# File 'lib/haveapi/authentication/oauth2/config.rb', line 60

def get_authorization_code(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.

Parameters:

  • authorization (Authorization)
  • sinatra_request (Sinatra::Request)

Returns:

  • (Array)

    access token, expiration date and optional refresh token



73
74
75
# File 'lib/haveapi/authentication/oauth2/config.rb', line 73

def get_tokens(authorization, 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.

Parameters:

  • sinatra_request (Sinatra::Request)
  • sinatra_params (Hash)

    request params

  • oauth2_request (Rack::OAuth2::Server::Authorize::Request)
  • client (Client)

Returns:



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.

Returns:

  • (Hash<String, String>)


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.

Parameters:

  • authorization (Authorization)
  • sinatra_request (Sinatra::Request)

Returns:

  • (Array)

    access token, expiration date and optional refresh token



85
86
87
# File 'lib/haveapi/authentication/oauth2/config.rb', line 85

def refresh_tokens(authorization, 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.

Parameters:

  • oauth2_request (Rack::OAuth2::Server::Authorize::Request)
  • sinatra_params (Hash)

    request params

  • client (Client)
  • auth_result (AuthResult, nil) (defaults to: nil)

Returns:

  • (String)

    HTML



33
34
35
# File 'lib/haveapi/authentication/oauth2/config.rb', line 33

def render_authorize_page(oauth2_request, sinatra_params, client, auth_result: nil)

end