Class: FattureInCloud_Ruby_Sdk::OAuth2AuthorizationCodeManager
- Inherits:
-
Object
- Object
- FattureInCloud_Ruby_Sdk::OAuth2AuthorizationCodeManager
- Defined in:
- lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb
Overview
The OAuth2AuthorizationCodeManager class is used to manage the OAuth2 authorization code flow.
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
Class Method Summary collapse
-
.get_scope_string(scopes) ⇒ String
Build rh escopes string.
Instance Method Summary collapse
-
#fetch_token(code) ⇒ OAuth2AuthorizationCodeTokenResponse
Get the access token.
-
#get_authorization_url(scopes, state) ⇒ String
Get the authorization url.
-
#get_params_from_url(url) ⇒ OAuth2AuthorizationCodeParams
Get url parameters.
-
#initialize(client_id, client_secret, redirect_uri, base_uri = 'https://api-v2.fattureincloud.it') ⇒ OAuth2AuthorizationCodeManager
constructor
Initializes a new instance of the OAuth2AuthorizationCodeManager class.
-
#refresh_token(refresh_token) ⇒ OAuth2AuthorizationCodeTokenResponse
Get the refresh token.
Constructor Details
#initialize(client_id, client_secret, redirect_uri, base_uri = 'https://api-v2.fattureincloud.it') ⇒ OAuth2AuthorizationCodeManager
Initializes a new instance of the OAuth2AuthorizationCodeManager class.
14 15 16 17 18 19 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 14 def initialize(client_id, client_secret, redirect_uri, base_uri = 'https://api-v2.fattureincloud.it') @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri @base_uri = base_uri end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
7 8 9 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 7 def base_uri @base_uri end |
#client_id ⇒ Object
Returns the value of attribute client_id.
7 8 9 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 7 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
7 8 9 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 7 def client_secret @client_secret end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
7 8 9 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 7 def redirect_uri @redirect_uri end |
Class Method Details
.get_scope_string(scopes) ⇒ String
Build rh escopes string.
96 97 98 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 96 def self.get_scope_string(scopes) scopes.map { |scope| scope.to_s }.join(' ') end |
Instance Method Details
#fetch_token(code) ⇒ OAuth2AuthorizationCodeTokenResponse
Get the access token.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 44 def fetch_token(code) token_uri = "#{@base_uri}/oauth/token" data = { 'grant_type': 'authorization_code', 'client_id': @client_id, 'client_secret': @client_secret, 'redirect_uri': @redirect_uri, 'code': code, } res = Typhoeus.post(token_uri, body: data) if res.response_code != 200 raise "Error fetching token: #{res.response_body}" end json = JSON.parse(res.body) OAuth2AuthorizationCodeTokenResponse.new(json["token_type"], json['access_token'], json['refresh_token'], json['expires_in']) end |
#get_authorization_url(scopes, state) ⇒ String
Get the authorization url.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 25 def (scopes, state) = "#{@base_uri}/oauth/authorize" scope_string = OAuth2AuthorizationCodeManager.get_scope_string(scopes) params = { 'response_type': 'code', 'client_id': @client_id, 'redirect_uri': @redirect_uri, 'scope': scope_string, 'state': state } url = URI.parse() url.query = URI.encode_www_form(params) url.to_s end |
#get_params_from_url(url) ⇒ OAuth2AuthorizationCodeParams
Get url parameters.
88 89 90 91 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 88 def get_params_from_url(url) url_obj = URI::decode_www_form(URI.parse(url).query).to_h OAuth2AuthorizationCodeParams.new(url_obj['code'], url_obj['state']) end |
#refresh_token(refresh_token) ⇒ OAuth2AuthorizationCodeTokenResponse
Get the refresh token.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 66 def refresh_token(refresh_token) token_uri = "#{@base_uri}/oauth/token" data = { 'grant_type': 'refresh_token', 'client_id': @client_id, 'client_secret': @client_secret, 'refresh_token': refresh_token, } res = Typhoeus.post(token_uri, body: data) if res.response_code != 200 raise "Error fetching token: #{res.response_body}" end json = JSON.parse(res.body) OAuth2AuthorizationCodeTokenResponse.new(json["token_type"], json['access_token'], json['refresh_token'], json['expires_in']) end |