Class: FattureInCloud_Ruby_Sdk::OAuth2AuthorizationCodeManager

Inherits:
OAuth2Manager
  • 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

Instance Method Summary collapse

Methods inherited from OAuth2Manager

#execute_post, get_scope_string

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.



46
47
48
49
50
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 46

def initialize(client_id, client_secret, redirect_uri, base_uri = 'https://api-v2.fattureincloud.it')
  super client_id, base_uri
  @client_secret = client_secret
  @redirect_uri = redirect_uri
end

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri.



39
40
41
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 39

def base_uri
  @base_uri
end

#client_idObject

Returns the value of attribute client_id.



39
40
41
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 39

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



39
40
41
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 39

def client_secret
  @client_secret
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



39
40
41
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 39

def redirect_uri
  @redirect_uri
end

Instance Method Details

#fetch_token(code) ⇒ OAuth2TokenResponse

Get the access token.

Parameters:

  • code (String)

    The code.

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 75

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,
  }

  json = execute_post(token_uri, data)

  OAuth2TokenResponse.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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 56

def get_authorization_url(scopes, state)
  authorization_uri = "#{@base_uri}/oauth/authorize"
  scope_string = OAuth2Manager.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:



111
112
113
114
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 111

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) ⇒ OAuth2TokenResponse

Get the refresh token.

Parameters:

  • refresh_token (String)

    The refresh token.

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fattureincloud_ruby_sdk/oauth2/oauth2.rb', line 93

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,
  }

  json = execute_post(token_uri, data)

  OAuth2TokenResponse.new(json["token_type"], json['access_token'], json['refresh_token'], json['expires_in'])
end