multi-auth-project-sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +28 -0
  3. data/README.md +139 -0
  4. data/lib/multi_auth_sample/api_helper.rb +10 -0
  5. data/lib/multi_auth_sample/client.rb +105 -0
  6. data/lib/multi_auth_sample/configuration.rb +170 -0
  7. data/lib/multi_auth_sample/controllers/authentication_controller.rb +151 -0
  8. data/lib/multi_auth_sample/controllers/base_controller.rb +65 -0
  9. data/lib/multi_auth_sample/controllers/o_auth_authorization_controller.rb +188 -0
  10. data/lib/multi_auth_sample/exceptions/api_exception.rb +10 -0
  11. data/lib/multi_auth_sample/exceptions/o_auth_provider_exception.rb +48 -0
  12. data/lib/multi_auth_sample/http/auth/api_header.rb +47 -0
  13. data/lib/multi_auth_sample/http/auth/api_key.rb +47 -0
  14. data/lib/multi_auth_sample/http/auth/basic_auth.rb +49 -0
  15. data/lib/multi_auth_sample/http/auth/custom_auth.rb +40 -0
  16. data/lib/multi_auth_sample/http/auth/o_auth_acg.rb +144 -0
  17. data/lib/multi_auth_sample/http/auth/o_auth_bearer_token.rb +42 -0
  18. data/lib/multi_auth_sample/http/auth/o_auth_ccg.rb +88 -0
  19. data/lib/multi_auth_sample/http/auth/o_auth_ropcg.rb +120 -0
  20. data/lib/multi_auth_sample/http/http_call_back.rb +10 -0
  21. data/lib/multi_auth_sample/http/http_method_enum.rb +10 -0
  22. data/lib/multi_auth_sample/http/http_request.rb +10 -0
  23. data/lib/multi_auth_sample/http/http_response.rb +10 -0
  24. data/lib/multi_auth_sample/models/base_model.rb +62 -0
  25. data/lib/multi_auth_sample/models/o_auth_provider_error_enum.rb +45 -0
  26. data/lib/multi_auth_sample/models/o_auth_scope_o_auth_acg_enum.rb +20 -0
  27. data/lib/multi_auth_sample/models/o_auth_token.rb +110 -0
  28. data/lib/multi_auth_sample/models/service_status.rb +129 -0
  29. data/lib/multi_auth_sample/models/suite_code_enum.rb +29 -0
  30. data/lib/multi_auth_sample/models/user.rb +100 -0
  31. data/lib/multi_auth_sample/utilities/date_time_helper.rb +11 -0
  32. data/lib/multi_auth_sample/utilities/file_wrapper.rb +16 -0
  33. data/lib/multi_auth_sample.rb +54 -0
  34. data/test/controllers/controller_test_base.rb +47 -0
  35. data/test/controllers/test_authentication_controller.rb +110 -0
  36. data/test/http_response_catcher.rb +19 -0
  37. metadata +154 -0
@@ -0,0 +1,65 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # BaseController.
8
+ class BaseController
9
+ attr_accessor :config, :http_call_back
10
+
11
+ def self.user_agent
12
+ 'APIMATIC 3.0'
13
+ end
14
+
15
+
16
+ GLOBAL_ERRORS = {
17
+ 'default' => ErrorCase.new
18
+ .error_message('HTTP response not OK.')
19
+ .exception_type(APIException)
20
+ }.freeze
21
+
22
+ # Initialization constructor.
23
+ # @param [GlobalConfiguration] global_configuration The instance of GlobalConfiguration.
24
+ def initialize(global_configuration)
25
+ @global_configuration = global_configuration
26
+ @config = @global_configuration.client_configuration
27
+ @http_call_back = @config.http_callback
28
+ @api_call = ApiCall.new(@global_configuration)
29
+ end
30
+
31
+ # Creates a new builder for the Api Call instance.
32
+ # @return [ApiCall] The instance of ApiCall.
33
+ def new_api_call_builder
34
+ @api_call.new_builder
35
+ end
36
+
37
+ # Creates a new instance of the request builder.
38
+ # @param [String] http_method The HTTP method to use in the request.
39
+ # @param [String] path The endpoint path to use in the request.
40
+ # @param [String] server The server to extract the base uri for the request.
41
+ # @return [RequestBuilder] The instance of RequestBuilder.
42
+ def new_request_builder(http_method, path, server)
43
+ RequestBuilder.new
44
+ .http_method(http_method)
45
+ .path(path)
46
+ .server(server)
47
+ end
48
+
49
+ # Creates a new instance of the response handler.
50
+ # @return [ResponseHandler] The instance of ResponseHandler.
51
+ def new_response_handler
52
+ ResponseHandler.new
53
+ end
54
+
55
+ # Creates a new instance of the parameter.
56
+ # @param [String|optional] key The key of the parameter.
57
+ # @param [Object] value The value of the parameter.
58
+ # @return [Parameter] The instance of Parameter.
59
+ def new_parameter(value, key: nil)
60
+ Parameter.new
61
+ .key(key)
62
+ .value(value)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,188 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # OAuthAuthorizationController
8
+ class OAuthAuthorizationController < BaseController
9
+ # Create a new OAuth 2 token.
10
+ # @param [String] authorization Required parameter: Authorization header in
11
+ # Basic auth format
12
+ # @param [String] scope Optional parameter: Requested scopes as a
13
+ # space-delimited list.
14
+ # @param [Hash] _field_parameters Additional optional form parameters are
15
+ # supported by this endpoint.
16
+ # @return [OAuthToken] response from the API call
17
+ def request_token_o_auth_ccg(authorization,
18
+ scope: nil,
19
+ _field_parameters: nil)
20
+ new_api_call_builder
21
+ .request(new_request_builder(HttpMethodEnum::POST,
22
+ '/request_token',
23
+ Server::AUTH)
24
+ .form_param(new_parameter('client_credentials', key: 'grant_type'))
25
+ .header_param(new_parameter(authorization, key: 'Authorization'))
26
+ .form_param(new_parameter(scope, key: 'scope'))
27
+ .header_param(new_parameter('application/x-www-form-urlencoded', key: 'content-type'))
28
+ .header_param(new_parameter('application/json', key: 'accept'))
29
+ .additional_form_params(_field_parameters))
30
+ .response(new_response_handler
31
+ .deserializer(APIHelper.method(:custom_type_deserializer))
32
+ .deserialize_into(OAuthToken.method(:from_hash))
33
+ .local_error('400',
34
+ 'OAuth 2 provider returned an error.',
35
+ OAuthProviderException)
36
+ .local_error('401',
37
+ 'OAuth 2 provider says client authentication failed.',
38
+ OAuthProviderException))
39
+ .execute
40
+ end
41
+
42
+ # Create a new OAuth 2 token.
43
+ # @param [String] authorization Required parameter: Authorization header in
44
+ # Basic auth format
45
+ # @param [String] code Required parameter: Authorization Code
46
+ # @param [String] redirect_uri Required parameter: Redirect Uri
47
+ # @param [Hash] _field_parameters Additional optional form parameters are
48
+ # supported by this endpoint.
49
+ # @return [OAuthToken] response from the API call
50
+ def request_token_o_auth_acg(authorization,
51
+ code,
52
+ redirect_uri,
53
+ _field_parameters: nil)
54
+ new_api_call_builder
55
+ .request(new_request_builder(HttpMethodEnum::POST,
56
+ '/oauth2/non-auth-server/token',
57
+ Server::DEFAULT)
58
+ .form_param(new_parameter('authorization_code', key: 'grant_type'))
59
+ .header_param(new_parameter(authorization, key: 'Authorization'))
60
+ .form_param(new_parameter(code, key: 'code'))
61
+ .form_param(new_parameter(redirect_uri, key: 'redirect_uri'))
62
+ .header_param(new_parameter('application/x-www-form-urlencoded', key: 'content-type'))
63
+ .header_param(new_parameter('application/json', key: 'accept'))
64
+ .additional_form_params(_field_parameters))
65
+ .response(new_response_handler
66
+ .deserializer(APIHelper.method(:custom_type_deserializer))
67
+ .deserialize_into(OAuthToken.method(:from_hash))
68
+ .local_error('400',
69
+ 'OAuth 2 provider returned an error.',
70
+ OAuthProviderException)
71
+ .local_error('401',
72
+ 'OAuth 2 provider says client authentication failed.',
73
+ OAuthProviderException))
74
+ .execute
75
+ end
76
+
77
+ # Obtain a new access token using a refresh token
78
+ # @param [String] authorization Required parameter: Authorization header in
79
+ # Basic auth format
80
+ # @param [String] refresh_token Required parameter: Refresh token
81
+ # @param [String] scope Optional parameter: Requested scopes as a
82
+ # space-delimited list.
83
+ # @param [Hash] _field_parameters Additional optional form parameters are
84
+ # supported by this endpoint.
85
+ # @return [OAuthToken] response from the API call
86
+ def refresh_token_o_auth_acg(authorization,
87
+ refresh_token,
88
+ scope: nil,
89
+ _field_parameters: nil)
90
+ new_api_call_builder
91
+ .request(new_request_builder(HttpMethodEnum::POST,
92
+ '/oauth2/non-auth-server/token',
93
+ Server::DEFAULT)
94
+ .form_param(new_parameter('refresh_token', key: 'grant_type'))
95
+ .header_param(new_parameter(authorization, key: 'Authorization'))
96
+ .form_param(new_parameter(refresh_token, key: 'refresh_token'))
97
+ .form_param(new_parameter(scope, key: 'scope'))
98
+ .header_param(new_parameter('application/x-www-form-urlencoded', key: 'content-type'))
99
+ .header_param(new_parameter('application/json', key: 'accept'))
100
+ .additional_form_params(_field_parameters))
101
+ .response(new_response_handler
102
+ .deserializer(APIHelper.method(:custom_type_deserializer))
103
+ .deserialize_into(OAuthToken.method(:from_hash))
104
+ .local_error('400',
105
+ 'OAuth 2 provider returned an error.',
106
+ OAuthProviderException)
107
+ .local_error('401',
108
+ 'OAuth 2 provider says client authentication failed.',
109
+ OAuthProviderException))
110
+ .execute
111
+ end
112
+
113
+ # Create a new OAuth 2 token.
114
+ # @param [String] authorization Required parameter: Authorization header in
115
+ # Basic auth format
116
+ # @param [String] username Required parameter: Resource owner username
117
+ # @param [String] password Required parameter: Resource owner password
118
+ # @param [String] scope Optional parameter: Requested scopes as a
119
+ # space-delimited list.
120
+ # @param [Hash] _field_parameters Additional optional form parameters are
121
+ # supported by this endpoint.
122
+ # @return [OAuthToken] response from the API call
123
+ def request_token_o_auth_ropcg(authorization,
124
+ username,
125
+ password,
126
+ scope: nil,
127
+ _field_parameters: nil)
128
+ new_api_call_builder
129
+ .request(new_request_builder(HttpMethodEnum::POST,
130
+ '/oauth2/non-auth-server/token',
131
+ Server::DEFAULT)
132
+ .form_param(new_parameter('password', key: 'grant_type'))
133
+ .header_param(new_parameter(authorization, key: 'Authorization'))
134
+ .form_param(new_parameter(username, key: 'username'))
135
+ .form_param(new_parameter(password, key: 'password'))
136
+ .form_param(new_parameter(scope, key: 'scope'))
137
+ .header_param(new_parameter('application/x-www-form-urlencoded', key: 'content-type'))
138
+ .header_param(new_parameter('application/json', key: 'accept'))
139
+ .additional_form_params(_field_parameters))
140
+ .response(new_response_handler
141
+ .deserializer(APIHelper.method(:custom_type_deserializer))
142
+ .deserialize_into(OAuthToken.method(:from_hash))
143
+ .local_error('400',
144
+ 'OAuth 2 provider returned an error.',
145
+ OAuthProviderException)
146
+ .local_error('401',
147
+ 'OAuth 2 provider says client authentication failed.',
148
+ OAuthProviderException))
149
+ .execute
150
+ end
151
+
152
+ # Obtain a new access token using a refresh token
153
+ # @param [String] authorization Required parameter: Authorization header in
154
+ # Basic auth format
155
+ # @param [String] refresh_token Required parameter: Refresh token
156
+ # @param [String] scope Optional parameter: Requested scopes as a
157
+ # space-delimited list.
158
+ # @param [Hash] _field_parameters Additional optional form parameters are
159
+ # supported by this endpoint.
160
+ # @return [OAuthToken] response from the API call
161
+ def refresh_token_o_auth_ropcg(authorization,
162
+ refresh_token,
163
+ scope: nil,
164
+ _field_parameters: nil)
165
+ new_api_call_builder
166
+ .request(new_request_builder(HttpMethodEnum::POST,
167
+ '/oauth2/non-auth-server/token',
168
+ Server::DEFAULT)
169
+ .form_param(new_parameter('refresh_token', key: 'grant_type'))
170
+ .header_param(new_parameter(authorization, key: 'Authorization'))
171
+ .form_param(new_parameter(refresh_token, key: 'refresh_token'))
172
+ .form_param(new_parameter(scope, key: 'scope'))
173
+ .header_param(new_parameter('application/x-www-form-urlencoded', key: 'content-type'))
174
+ .header_param(new_parameter('application/json', key: 'accept'))
175
+ .additional_form_params(_field_parameters))
176
+ .response(new_response_handler
177
+ .deserializer(APIHelper.method(:custom_type_deserializer))
178
+ .deserialize_into(OAuthToken.method(:from_hash))
179
+ .local_error('400',
180
+ 'OAuth 2 provider returned an error.',
181
+ OAuthProviderException)
182
+ .local_error('401',
183
+ 'OAuth 2 provider says client authentication failed.',
184
+ OAuthProviderException))
185
+ .execute
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,10 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # Class for exceptions when there is a network error, status code error, etc.
8
+ class APIException < CoreLibrary::ApiException
9
+ end
10
+ end
@@ -0,0 +1,48 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # OAuth 2 Authorization endpoint exception.
8
+ class OAuthProviderException < APIException
9
+ SKIP = Object.new
10
+ private_constant :SKIP
11
+
12
+ # Gets or sets error code.
13
+ # @return [OAuthProviderErrorEnum]
14
+ attr_accessor :error
15
+
16
+ # Gets or sets human-readable text providing additional information on
17
+ # error.
18
+ # Used to assist the client developer in understanding the error that
19
+ # occurred.
20
+ # @return [String]
21
+ attr_accessor :error_description
22
+
23
+ # Gets or sets a URI identifying a human-readable web page with information
24
+ # about the error, used to provide the client developer with additional
25
+ # information about the error.
26
+ # @return [String]
27
+ attr_accessor :error_uri
28
+
29
+ # The constructor.
30
+ # @param [String] The reason for raising an exception.
31
+ # @param [HttpResponse] The HttpReponse of the API call.
32
+ def initialize(reason, response)
33
+ super(reason, response)
34
+ hash = APIHelper.json_deserialize(@response.raw_body)
35
+ unbox(hash)
36
+ end
37
+
38
+ # Populates this object by extracting properties from a hash.
39
+ # @param [Hash] The deserialized response sent by the server in the
40
+ # response body.
41
+ def unbox(hash)
42
+ @error = hash.key?('error') ? hash['error'] : nil
43
+ @error_description =
44
+ hash.key?('error_description') ? hash['error_description'] : SKIP
45
+ @error_uri = hash.key?('error_uri') ? hash['error_uri'] : SKIP
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,47 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # Utility class for custom header authorization.
8
+ class ApiHeader < CoreLibrary::HeaderAuth
9
+ # Display error message on occurrence of authentication failure.
10
+ # @returns [String] The oAuth error message.
11
+ def error_message
12
+ 'ApiHeader: token or api_key is undefined.'
13
+ end
14
+
15
+ # Initialization constructor.
16
+ def initialize(api_header_credentials)
17
+ auth_params = {}
18
+ auth_params['token'] = api_header_credentials.token unless
19
+ api_header_credentials.nil? || api_header_credentials.token.nil?
20
+ auth_params['api-key'] = api_header_credentials.api_key unless
21
+ api_header_credentials.nil? || api_header_credentials.api_key.nil?
22
+
23
+ super auth_params
24
+ end
25
+ end
26
+
27
+ # Data class for ApiHeaderCredentials.
28
+ # Data class for ApiHeaderCredentials.
29
+ class ApiHeaderCredentials
30
+ attr_reader :token, :api_key
31
+
32
+ def initialize(token:, api_key:)
33
+ raise ArgumentError, 'token cannot be nil' if token.nil?
34
+ raise ArgumentError, 'api_key cannot be nil' if api_key.nil?
35
+
36
+ @token = token
37
+ @api_key = api_key
38
+ end
39
+
40
+ def clone_with(token: nil, api_key: nil)
41
+ token ||= self.token
42
+ api_key ||= self.api_key
43
+
44
+ ApiHeaderCredentials.new(token: token, api_key: api_key)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # Utility class for custom query_parameter authorization.
8
+ class ApiKey < CoreLibrary::QueryAuth
9
+ # Display error message on occurrence of authentication failure.
10
+ # @returns [String] The oAuth error message.
11
+ def error_message
12
+ 'ApiKey: token or api_key is undefined.'
13
+ end
14
+
15
+ # Initialization constructor.
16
+ def initialize(api_key_credentials)
17
+ auth_params = {}
18
+ auth_params['token'] = api_key_credentials.token unless
19
+ api_key_credentials.nil? || api_key_credentials.token.nil?
20
+ auth_params['api-key'] = api_key_credentials.api_key unless
21
+ api_key_credentials.nil? || api_key_credentials.api_key.nil?
22
+
23
+ super auth_params
24
+ end
25
+ end
26
+
27
+ # Data class for ApiKeyCredentials.
28
+ # Data class for ApiKeyCredentials.
29
+ class ApiKeyCredentials
30
+ attr_reader :token, :api_key
31
+
32
+ def initialize(token:, api_key:)
33
+ raise ArgumentError, 'token cannot be nil' if token.nil?
34
+ raise ArgumentError, 'api_key cannot be nil' if api_key.nil?
35
+
36
+ @token = token
37
+ @api_key = api_key
38
+ end
39
+
40
+ def clone_with(token: nil, api_key: nil)
41
+ token ||= self.token
42
+ api_key ||= self.api_key
43
+
44
+ ApiKeyCredentials.new(token: token, api_key: api_key)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # Utility class for basic authorization.
8
+ class BasicAuth < CoreLibrary::HeaderAuth
9
+ # Display error message on occurrence of authentication failure.
10
+ # @returns [String] The oAuth error message.
11
+ def error_message
12
+ 'BasicAuth: username or password is undefined.'
13
+ end
14
+
15
+ # Initialization constructor.
16
+ def initialize(basic_auth_credentials)
17
+ auth_params = {}
18
+ unless basic_auth_credentials.nil? ||
19
+ basic_auth_credentials.username.nil? ||
20
+ basic_auth_credentials.password.nil?
21
+ auth_params['Authorization'] =
22
+ "Basic #{AuthHelper.get_base64_encoded_value(basic_auth_credentials.username,
23
+ basic_auth_credentials.password)}"
24
+ end
25
+
26
+ super auth_params
27
+ end
28
+ end
29
+
30
+ # Data class for BasicAuthCredentials.
31
+ class BasicAuthCredentials
32
+ attr_reader :username, :password
33
+
34
+ def initialize(username:, password:)
35
+ raise ArgumentError, 'username cannot be nil' if username.nil?
36
+ raise ArgumentError, 'password cannot be nil' if password.nil?
37
+
38
+ @username = username
39
+ @password = password
40
+ end
41
+
42
+ def clone_with(username: nil, password: nil)
43
+ username ||= self.username
44
+ password ||= self.password
45
+
46
+ BasicAuthCredentials.new(username: username, password: password)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,40 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # Utility class for custom authorization.
8
+ class CustomAuth < CoreLibrary::HeaderAuth
9
+ # Display error message on occurrence of authentication failure.
10
+ # @returns [String] The oAuth error message.
11
+ def error_message
12
+ # TODO: Add your error message here in case of failure in authentication
13
+ #
14
+ # Example:
15
+ # 'CustomAuth: config.access_token is undefined.'
16
+ end
17
+
18
+ # Initialization constructor.
19
+ def initialize(config)
20
+ auth_params = {}
21
+
22
+ # TODO: Add your custom authentication here
23
+ # The following properties are available to use
24
+ # auth_params['accessToken'] = config.access_token
25
+
26
+ super auth_params
27
+ @_config = config
28
+ end
29
+
30
+ # Validate arguments for authentication.
31
+ # @return [Boolean] true if the auth parameters are present.
32
+ def valid
33
+ # TODO: Add your validation checks here
34
+ #
35
+ # Example:
36
+ # return false unless @_config.access_token.nil?
37
+ true
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,144 @@
1
+ # multi_auth_sample
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module MultiAuthSample
7
+ # Utility class for OAuth 2 authorization and token management.
8
+ class OAuthACG < CoreLibrary::HeaderAuth
9
+ # Display error message on occurrence of authentication failure.
10
+ # @returns [String] The oAuth error message.
11
+ def error_message
12
+ 'OAuthACG: OAuthToken is undefined or expired.'
13
+ end
14
+
15
+ # Initialization constructor.
16
+ def initialize(o_auth_acg_credentials, config)
17
+ auth_params = {}
18
+ @_o_auth_client_id = o_auth_acg_credentials.o_auth_client_id unless
19
+ o_auth_acg_credentials.nil? || o_auth_acg_credentials.o_auth_client_id.nil?
20
+ @_o_auth_client_secret = o_auth_acg_credentials.o_auth_client_secret unless
21
+ o_auth_acg_credentials.nil? || o_auth_acg_credentials.o_auth_client_secret.nil?
22
+ @_o_auth_redirect_uri = o_auth_acg_credentials.o_auth_redirect_uri unless
23
+ o_auth_acg_credentials.nil? || o_auth_acg_credentials.o_auth_redirect_uri.nil?
24
+ @_o_auth_token = o_auth_acg_credentials.o_auth_token unless
25
+ o_auth_acg_credentials.nil? || o_auth_acg_credentials.o_auth_token.nil?
26
+ @_o_auth_scopes = o_auth_acg_credentials.o_auth_scopes unless
27
+ o_auth_acg_credentials.nil? || o_auth_acg_credentials.o_auth_scopes.nil?
28
+ @_config = config
29
+ @_o_auth_api = OAuthAuthorizationController.new(config)
30
+ auth_params['Authorization'] = "Bearer #{@_o_auth_token.access_token}" unless @_o_auth_token.nil?
31
+
32
+ super auth_params
33
+ end
34
+
35
+ # Validates the oAuth token.
36
+ # @return [Boolean] true if the token is present and not expired.
37
+ def valid
38
+ !@_o_auth_token.nil? && !token_expired?(@_o_auth_token)
39
+ end
40
+
41
+ # Builds and returns an authorization URL.
42
+ # The user is expected to obtain an authorization code from this URL and then call the
43
+ # fetch token function with that authorization code.
44
+ # @param [String] state An opaque state string.
45
+ # @param [Hash] additional_params Any additional query parameters to be added to the URL.
46
+ # @return [String] additional_params The authorization URL.
47
+ def get_authorization_url(state: nil, additional_params: nil)
48
+ auth_url = @_config.get_base_uri
49
+ auth_url += '/oauth/authorize'
50
+ query_params = {
51
+ 'response_type' => 'code',
52
+ 'client_id' => @_o_auth_client_id,
53
+ 'redirect_uri' => @_o_auth_client_id
54
+ }
55
+ query_params['scope'] = Array(@_o_auth_scopes).compact.join(' ') if @_o_auth_scopes
56
+ query_params['state'] = state if state
57
+ query_params.merge!(additional_params) if additional_params
58
+ auth_url = APIHelper.append_url_with_query_parameters(auth_url,
59
+ query_params)
60
+ APIHelper.clean_url(auth_url)
61
+ end
62
+
63
+ # Builds the basic auth header for endpoints in the OAuth Authorization Controller.
64
+ # @return [String] The value of the Authentication header.
65
+ def build_basic_auth_header
66
+ "Basic #{AuthHelper.get_base64_encoded_value(@_o_auth_client_id, @_o_auth_client_secret)}"
67
+ end
68
+
69
+ # Fetches the token.
70
+ # @param [String] auth_code The authentication code.
71
+ # @param [Hash] additional_params Any additional form parameters.
72
+ # @return [OAuthToken] The oAuth token instance.
73
+ def fetch_token(auth_code, additional_params: nil)
74
+ token = @_o_auth_api.request_token_o_auth_acg(
75
+ build_basic_auth_header,
76
+ auth_code,
77
+ @_o_auth_redirect_uri,
78
+ _field_parameters: additional_params
79
+ )
80
+ if token.respond_to?('expires_in') && !token.expires_in.nil?
81
+ token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i)
82
+ end
83
+ token
84
+ end
85
+
86
+ # Checks if OAuth token has expired.
87
+ # @param [OAuthToken] token The oAuth token instance.
88
+ # @return [Boolean] true if the token's expiry exist and also the token is expired, false otherwise.
89
+ def token_expired?(token)
90
+ token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry)
91
+ end
92
+
93
+ # Refreshes OAuth token.
94
+ # @param [Hash] additional_params Any additional form parameters.
95
+ # @return [OAuthToken] The oAuth token instance.
96
+ def refresh_token(additional_params: nil)
97
+ token = @_o_auth_api.refresh_token_o_auth_acg(
98
+ OAuthACG.build_basic_auth_header,
99
+ @_o_auth_token.refresh_token,
100
+ scope: !@_o_auth_scopes.nil? ? Array(@_o_auth_scopes).compact.join(' ') : nil,
101
+ _field_parameters: additional_params
102
+ )
103
+ if token.respond_to?('expires_in') && !token.expires_in.nil?
104
+ token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i)
105
+ end
106
+ token
107
+ end
108
+ end
109
+
110
+ # Data class for OAuthACGCredentials.
111
+ class OAuthACGCredentials
112
+ attr_reader :o_auth_client_id, :o_auth_client_secret, :o_auth_redirect_uri,
113
+ :o_auth_token, :o_auth_scopes
114
+
115
+ def initialize(o_auth_client_id:, o_auth_client_secret:,
116
+ o_auth_redirect_uri:, o_auth_token: nil, o_auth_scopes: nil)
117
+ raise ArgumentError, 'o_auth_client_id cannot be nil' if o_auth_client_id.nil?
118
+ raise ArgumentError, 'o_auth_client_secret cannot be nil' if o_auth_client_secret.nil?
119
+ raise ArgumentError, 'o_auth_redirect_uri cannot be nil' if o_auth_redirect_uri.nil?
120
+
121
+ @o_auth_client_id = o_auth_client_id
122
+ @o_auth_client_secret = o_auth_client_secret
123
+ @o_auth_redirect_uri = o_auth_redirect_uri
124
+ @o_auth_token = o_auth_token
125
+ @o_auth_scopes = o_auth_scopes
126
+ end
127
+
128
+ def clone_with(o_auth_client_id: nil, o_auth_client_secret: nil,
129
+ o_auth_redirect_uri: nil, o_auth_token: nil,
130
+ o_auth_scopes: nil)
131
+ o_auth_client_id ||= self.o_auth_client_id
132
+ o_auth_client_secret ||= self.o_auth_client_secret
133
+ o_auth_redirect_uri ||= self.o_auth_redirect_uri
134
+ o_auth_token ||= self.o_auth_token
135
+ o_auth_scopes ||= self.o_auth_scopes
136
+
137
+ OAuthACGCredentials.new(o_auth_client_id: o_auth_client_id,
138
+ o_auth_client_secret: o_auth_client_secret,
139
+ o_auth_redirect_uri: o_auth_redirect_uri,
140
+ o_auth_token: o_auth_token,
141
+ o_auth_scopes: o_auth_scopes)
142
+ end
143
+ end
144
+ end