Class: MdNotes::OAuth2
- Inherits:
-
Object
- Object
- MdNotes::OAuth2
- Defined in:
- lib/md_notes/http/auth/o_auth2.rb
Overview
Utility class for OAuth 2 authorization and token management.
Class Method Summary collapse
-
.apply(config, http_request) ⇒ Object
Add OAuth2 authentication to the http request.
- .auth_controller(config) ⇒ Object
-
.authorize(config, scope: nil, additional_params: nil) ⇒ Object
Get an OAuth token that you must then set in your Configuration object to authorize subsequent calls.
-
.build_basic_auth_header(config) ⇒ Object
Builds the basic auth header for endpoints in the OAuth Authorization Controller.
-
.check_auth(token) ⇒ Object
Checks if OAuth token is valid.
-
.refresh_token(config, scope: nil, additional_params: nil) ⇒ Object
Refreshes OAuth token and returns it.
Class Method Details
.apply(config, http_request) ⇒ Object
Add OAuth2 authentication to the http request. be added.
18 19 20 21 22 |
# File 'lib/md_notes/http/auth/o_auth2.rb', line 18 def self.apply(config, http_request) check_auth config.o_auth_token token = config.o_auth_token.access_token http_request.headers['Authorization'] = "Bearer #{token}" end |
.auth_controller(config) ⇒ Object
11 12 13 |
# File 'lib/md_notes/http/auth/o_auth2.rb', line 11 def self.auth_controller(config) OAuthAuthorizationController.new config end |
.authorize(config, scope: nil, additional_params: nil) ⇒ Object
Get an OAuth token that you must then set in your Configuration object to authorize subsequent calls.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/md_notes/http/auth/o_auth2.rb', line 28 def self.(config, scope: nil, additional_params: nil) token = auth_controller(config).request_token( build_basic_auth_header(config), config.o_auth_username, config.o_auth_password, scope: scope ? Array(scope).compact.join(' ') : nil, _field_parameters: additional_params ) token.expiry = (Time.now.to_i + token.expires_in.to_i) if token.expires_in token end |
.build_basic_auth_header(config) ⇒ Object
Builds the basic auth header for endpoints in the OAuth Authorization Controller.
42 43 44 45 46 47 |
# File 'lib/md_notes/http/auth/o_auth2.rb', line 42 def self.build_basic_auth_header(config) value = "#{config.o_auth_client_id}:" \ "#{config.o_auth_client_secret}" encoded = Base64.strict_encode64(value) "Basic #{encoded}" end |
.check_auth(token) ⇒ Object
Checks if OAuth token is valid.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/md_notes/http/auth/o_auth2.rb', line 50 def self.check_auth(token) # Check if OAuth token exists. if token.nil? raise 'Client is not authorized. An OAuth token is needed to ' \ 'make API calls.' # Check if OAuth token has expired. elsif token.expiry && token.expiry < Time.now.to_i raise 'The OAuth token has expired. Please refresh the existing token' \ ' or generate a new one.' end end |
.refresh_token(config, scope: nil, additional_params: nil) ⇒ Object
Refreshes OAuth token and returns it. You must then set it in your Configuration object. token.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/md_notes/http/auth/o_auth2.rb', line 67 def self.refresh_token(config, scope: nil, additional_params: nil) token = auth_controller(config).refresh_token( build_basic_auth_header(config), config.o_auth_token.refresh_token, scope: scope ? Array(scope).compact.join(' ') : nil, _field_parameters: additional_params ) token.expiry = (Time.now.to_i + token.expires_in.to_i) if token.expires_in token end |