Class: FattureInCloud_Ruby_Sdk::OAuth2AuthorizationCodeManager

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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • client_id (String)

    The client id.

  • client_secret (String)

    The client secret.

  • redirect_uri (String)

    The redirect uri.

  • base_uri (String) (defaults to: 'https://api-v2.fattureincloud.it')

    The base uri.



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_uriObject

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_idObject

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_secretObject

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_uriObject

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.

Parameters:

  • scopes (Array<Scope>)

    The scopes.

Returns:

  • (String)

    The scopes 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.

Parameters:

  • code (String)

    The code.

Returns:



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.

Parameters:

  • scopes (Array<Scope>)

    The scopes.

  • state (String)

    The state.

Returns:

  • (String)

    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 get_authorization_url(scopes, state)
  authorization_uri = "#{@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(authorization_uri)
  url.query = URI.encode_www_form(params)
  url.to_s
end

#get_params_from_url(url) ⇒ OAuth2AuthorizationCodeParams

Get url parameters.

Parameters:

  • url (String)

    The url.

Returns:



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.

Parameters:

  • refresh_token (String)

    The refresh token.

Returns:



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