tesla-api-sdk 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +36 -31
  3. data/lib/tesla_fleet_management_api/client.rb +14 -8
  4. data/lib/tesla_fleet_management_api/configuration.rb +8 -8
  5. data/lib/tesla_fleet_management_api/controllers/base_controller.rb +1 -1
  6. data/lib/tesla_fleet_management_api/controllers/charging_controller.rb +3 -3
  7. data/lib/tesla_fleet_management_api/controllers/energy_controller.rb +11 -11
  8. data/lib/tesla_fleet_management_api/controllers/{oauth_authorization_controller.rb → o_auth_authorization_controller.rb} +16 -16
  9. data/lib/tesla_fleet_management_api/controllers/partner_controller.rb +4 -4
  10. data/lib/tesla_fleet_management_api/controllers/user_controller.rb +4 -4
  11. data/lib/tesla_fleet_management_api/controllers/vehicle_commands_controller.rb +486 -0
  12. data/lib/tesla_fleet_management_api/controllers/vehicles_controller.rb +21 -21
  13. data/lib/tesla_fleet_management_api/exceptions/{oauth_provider_exception.rb → o_auth_provider_exception.rb} +2 -2
  14. data/lib/tesla_fleet_management_api/http/auth/oauth2.rb +163 -0
  15. data/lib/tesla_fleet_management_api/models/actuate_trunk_request.rb +74 -0
  16. data/lib/tesla_fleet_management_api/models/add_charge_schedule_request.rb +160 -0
  17. data/lib/tesla_fleet_management_api/models/add_precondition_schedule_request.rb +132 -0
  18. data/lib/tesla_fleet_management_api/models/adjust_volume_request.rb +73 -0
  19. data/lib/tesla_fleet_management_api/models/command_response.rb +75 -0
  20. data/lib/tesla_fleet_management_api/models/command_result.rb +82 -0
  21. data/lib/tesla_fleet_management_api/models/guest_mode_request.rb +73 -0
  22. data/lib/tesla_fleet_management_api/models/kind_get_wall_connector_charging_history.rb +1 -1
  23. data/lib/tesla_fleet_management_api/models/{oauth_provider_error.rb → o_auth_provider_error.rb} +3 -3
  24. data/lib/tesla_fleet_management_api/models/o_auth_scope_oauth2.rb +82 -0
  25. data/lib/tesla_fleet_management_api/models/{oauth_token.rb → o_auth_token.rb} +2 -2
  26. data/lib/tesla_fleet_management_api/models/which_trunk.rb +36 -0
  27. data/lib/tesla_fleet_management_api.rb +17 -5
  28. metadata +17 -7
  29. data/lib/tesla_fleet_management_api/http/auth/oauth_2.rb +0 -154
@@ -0,0 +1,82 @@
1
+ # tesla_fleet_management_api
2
+ #
3
+ # This file was automatically generated by
4
+ # APIMATIC v3.0 ( https://www.apimatic.io ).
5
+
6
+ module TeslaFleetManagementApi
7
+ # CommandResult Model.
8
+ class CommandResult < BaseModel
9
+ SKIP = Object.new
10
+ private_constant :SKIP
11
+
12
+ # TODO: Write general description for this method
13
+ # @return [TrueClass | FalseClass]
14
+ attr_accessor :result
15
+
16
+ # TODO: Write general description for this method
17
+ # @return [String]
18
+ attr_accessor :reason
19
+
20
+ # A mapping from model property names to API property names.
21
+ def self.names
22
+ @_hash = {} if @_hash.nil?
23
+ @_hash['result'] = 'result'
24
+ @_hash['reason'] = 'reason'
25
+ @_hash
26
+ end
27
+
28
+ # An array for optional fields
29
+ def self.optionals
30
+ []
31
+ end
32
+
33
+ # An array for nullable fields
34
+ def self.nullables
35
+ []
36
+ end
37
+
38
+ def initialize(result:, reason:, additional_properties: nil)
39
+ # Add additional model properties to the instance
40
+ additional_properties = {} if additional_properties.nil?
41
+
42
+ @result = result
43
+ @reason = reason
44
+ @additional_properties = additional_properties
45
+ end
46
+
47
+ # Creates an instance of the object from a hash.
48
+ def self.from_hash(hash)
49
+ return nil unless hash
50
+
51
+ # Extract variables from the hash.
52
+ result = hash.key?('result') ? hash['result'] : nil
53
+ reason = hash.key?('reason') ? hash['reason'] : nil
54
+
55
+ # Create a new hash for additional properties, removing known properties.
56
+ new_hash = hash.reject { |k, _| names.value?(k) }
57
+
58
+ additional_properties = APIHelper.get_additional_properties(
59
+ new_hash, proc { |value| value }
60
+ )
61
+
62
+ # Create object from extracted values.
63
+ CommandResult.new(result: result,
64
+ reason: reason,
65
+ additional_properties: additional_properties)
66
+ end
67
+
68
+ # Provides a human-readable string representation of the object.
69
+ def to_s
70
+ class_name = self.class.name.split('::').last
71
+ "<#{class_name} result: #{@result}, reason: #{@reason}, additional_properties:"\
72
+ " #{@additional_properties}>"
73
+ end
74
+
75
+ # Provides a debugging-friendly string with detailed object information.
76
+ def inspect
77
+ class_name = self.class.name.split('::').last
78
+ "<#{class_name} result: #{@result.inspect}, reason: #{@reason.inspect},"\
79
+ " additional_properties: #{@additional_properties}>"
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,73 @@
1
+ # tesla_fleet_management_api
2
+ #
3
+ # This file was automatically generated by
4
+ # APIMATIC v3.0 ( https://www.apimatic.io ).
5
+
6
+ module TeslaFleetManagementApi
7
+ # GuestModeRequest Model.
8
+ class GuestModeRequest < BaseModel
9
+ SKIP = Object.new
10
+ private_constant :SKIP
11
+
12
+ # Enable or disable Guest Mode
13
+ # @return [TrueClass | FalseClass]
14
+ attr_accessor :enable
15
+
16
+ # A mapping from model property names to API property names.
17
+ def self.names
18
+ @_hash = {} if @_hash.nil?
19
+ @_hash['enable'] = 'enable'
20
+ @_hash
21
+ end
22
+
23
+ # An array for optional fields
24
+ def self.optionals
25
+ []
26
+ end
27
+
28
+ # An array for nullable fields
29
+ def self.nullables
30
+ []
31
+ end
32
+
33
+ def initialize(enable:, additional_properties: nil)
34
+ # Add additional model properties to the instance
35
+ additional_properties = {} if additional_properties.nil?
36
+
37
+ @enable = enable
38
+ @additional_properties = additional_properties
39
+ end
40
+
41
+ # Creates an instance of the object from a hash.
42
+ def self.from_hash(hash)
43
+ return nil unless hash
44
+
45
+ # Extract variables from the hash.
46
+ enable = hash.key?('enable') ? hash['enable'] : nil
47
+
48
+ # Create a new hash for additional properties, removing known properties.
49
+ new_hash = hash.reject { |k, _| names.value?(k) }
50
+
51
+ additional_properties = APIHelper.get_additional_properties(
52
+ new_hash, proc { |value| value }
53
+ )
54
+
55
+ # Create object from extracted values.
56
+ GuestModeRequest.new(enable: enable,
57
+ additional_properties: additional_properties)
58
+ end
59
+
60
+ # Provides a human-readable string representation of the object.
61
+ def to_s
62
+ class_name = self.class.name.split('::').last
63
+ "<#{class_name} enable: #{@enable}, additional_properties: #{@additional_properties}>"
64
+ end
65
+
66
+ # Provides a debugging-friendly string with detailed object information.
67
+ def inspect
68
+ class_name = self.class.name.split('::').last
69
+ "<#{class_name} enable: #{@enable.inspect}, additional_properties:"\
70
+ " #{@additional_properties}>"
71
+ end
72
+ end
73
+ end
@@ -4,7 +4,7 @@
4
4
  # APIMATIC v3.0 ( https://www.apimatic.io ).
5
5
 
6
6
  module TeslaFleetManagementApi
7
- # kind_Get wall connector charging history.
7
+ # kind_getWallConnectorChargingHistory.
8
8
  class KindGetWallConnectorChargingHistory
9
9
  KIND_GET_WALL_CONNECTOR_CHARGING_HISTORY = [
10
10
  # TODO: Write general description for CHARGE
@@ -5,8 +5,8 @@
5
5
 
6
6
  module TeslaFleetManagementApi
7
7
  # OAuth 2 Authorization error codes
8
- class OauthProviderError
9
- OAUTH_PROVIDER_ERROR = [
8
+ class OAuthProviderError
9
+ O_AUTH_PROVIDER_ERROR = [
10
10
  # The request is missing a required parameter, includes an unsupported
11
11
  # parameter value (other than grant type), repeats a parameter, includes
12
12
  # multiple credentials, utilizes more than one mechanism for
@@ -39,7 +39,7 @@ module TeslaFleetManagementApi
39
39
  def self.validate(value)
40
40
  return false if value.nil?
41
41
 
42
- OAUTH_PROVIDER_ERROR.include?(value)
42
+ O_AUTH_PROVIDER_ERROR.include?(value)
43
43
  end
44
44
 
45
45
  def self.from_value(value, default_value = INVALID_REQUEST)
@@ -0,0 +1,82 @@
1
+ # tesla_fleet_management_api
2
+ #
3
+ # This file was automatically generated by
4
+ # APIMATIC v3.0 ( https://www.apimatic.io ).
5
+
6
+ module TeslaFleetManagementApi
7
+ # OAuth 2 scopes supported by the API
8
+ class OAuthScopeOauth2
9
+ O_AUTH_SCOPE_OAUTH2 = [
10
+ # Allow Tesla customers to sign in to the application with their Tesla
11
+ # credentials.
12
+ OPENID = 'openid'.freeze,
13
+
14
+ # Allow getting a refresh token without needing user to log in again.
15
+ OFFLINE_ACCESS = 'offline_access'.freeze,
16
+
17
+ # Contact information, home address, profile picture, and referral
18
+ # information.
19
+ USER_DATA = 'user_data'.freeze,
20
+
21
+ # Allow access to your vehicle’s live data, service history, service
22
+ # scheduling data, service communications, eligible upgrades, nearby
23
+ # Superchargers and ownership details.
24
+ VEHICLE_DEVICE_DATA = 'vehicle_device_data'.freeze,
25
+
26
+ # Allow access to vehicle location information, including precise and
27
+ # coarse location data.
28
+ VEHICLE_LOCATION = 'vehicle_location'.freeze,
29
+
30
+ # Commands like add/remove driver, access Live Camera, unlock, wake up,
31
+ # remote start, and schedule software updates.
32
+ VEHICLE_CMDS = 'vehicle_cmds'.freeze,
33
+
34
+ # Vehicle charging history, billed amount, charging location, and commands
35
+ # to schedule, start, or stop charging.
36
+ VEHICLE_CHARGING_CMDS = 'vehicle_charging_cmds'.freeze,
37
+
38
+ # Access detailed vehicle specifications. Partner tokens only; usable
39
+ # without owner authorization.
40
+ VEHICLE_SPECS = 'vehicle_specs'.freeze,
41
+
42
+ # Energy live status, site info, backup history, energy history, and
43
+ # charge history.
44
+ ENERGY_DEVICE_DATA = 'energy_device_data'.freeze,
45
+
46
+ # Update energy settings like backup reserve percent, operation mode, and
47
+ # storm mode.
48
+ ENERGY_CMDS = 'energy_cmds'.freeze,
49
+
50
+ # Allow access to enterprise management functions for businesses.
51
+ ENTERPRISE_MANAGEMENT = 'enterprise_management'.freeze
52
+ ].freeze
53
+
54
+ def self.validate(value)
55
+ return false if value.nil?
56
+
57
+ O_AUTH_SCOPE_OAUTH2.include?(value)
58
+ end
59
+
60
+ def self.from_value(value, default_value = OPENID)
61
+ return default_value if value.nil?
62
+
63
+ str = value.to_s.strip
64
+
65
+ case str.downcase
66
+ when 'openid' then OPENID
67
+ when 'offline_access' then OFFLINE_ACCESS
68
+ when 'user_data' then USER_DATA
69
+ when 'vehicle_device_data' then VEHICLE_DEVICE_DATA
70
+ when 'vehicle_location' then VEHICLE_LOCATION
71
+ when 'vehicle_cmds' then VEHICLE_CMDS
72
+ when 'vehicle_charging_cmds' then VEHICLE_CHARGING_CMDS
73
+ when 'vehicle_specs' then VEHICLE_SPECS
74
+ when 'energy_device_data' then ENERGY_DEVICE_DATA
75
+ when 'energy_cmds' then ENERGY_CMDS
76
+ when 'enterprise_management' then ENTERPRISE_MANAGEMENT
77
+ else
78
+ default_value
79
+ end
80
+ end
81
+ end
82
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  module TeslaFleetManagementApi
7
7
  # OAuth 2 Authorization endpoint response
8
- class OauthToken < BaseModel
8
+ class OAuthToken < BaseModel
9
9
  SKIP = Object.new
10
10
  private_constant :SKIP
11
11
 
@@ -85,7 +85,7 @@ module TeslaFleetManagementApi
85
85
  refresh_token = hash.key?('refresh_token') ? hash['refresh_token'] : SKIP
86
86
 
87
87
  # Create object from extracted values.
88
- OauthToken.new(access_token: access_token,
88
+ OAuthToken.new(access_token: access_token,
89
89
  token_type: token_type,
90
90
  expires_in: expires_in,
91
91
  scope: scope,
@@ -0,0 +1,36 @@
1
+ # tesla_fleet_management_api
2
+ #
3
+ # This file was automatically generated by
4
+ # APIMATIC v3.0 ( https://www.apimatic.io ).
5
+
6
+ module TeslaFleetManagementApi
7
+ # WhichTrunk.
8
+ class WhichTrunk
9
+ WHICH_TRUNK = [
10
+ # TODO: Write general description for FRONT
11
+ FRONT = 'front'.freeze,
12
+
13
+ # TODO: Write general description for REAR
14
+ REAR = 'rear'.freeze
15
+ ].freeze
16
+
17
+ def self.validate(value)
18
+ return false if value.nil?
19
+
20
+ true
21
+ end
22
+
23
+ def self.from_value(value, default_value = FRONT)
24
+ return default_value if value.nil?
25
+
26
+ str = value.to_s.strip
27
+
28
+ case str.downcase
29
+ when 'front' then FRONT
30
+ when 'rear' then REAR
31
+ else
32
+ default_value
33
+ end
34
+ end
35
+ end
36
+ end
@@ -31,7 +31,7 @@ require_relative 'tesla_fleet_management_api/logging/configuration/' \
31
31
  'api_logging_configuration'
32
32
  require_relative 'tesla_fleet_management_api/logging/sdk_logger'
33
33
  require_relative 'tesla_fleet_management_api/http/auth/bearer_auth'
34
- require_relative 'tesla_fleet_management_api/http/auth/oauth_2'
34
+ require_relative 'tesla_fleet_management_api/http/auth/oauth2'
35
35
 
36
36
  # Models
37
37
  require_relative 'tesla_fleet_management_api/models/base_model'
@@ -128,17 +128,27 @@ require_relative 'tesla_fleet_management_api/models/' \
128
128
  'response_api1_dx_vehicles_options_response'
129
129
  require_relative 'tesla_fleet_management_api/models/' \
130
130
  'response_api1_dx_warranty_details_response'
131
- require_relative 'tesla_fleet_management_api/models/oauth_token'
131
+ require_relative 'tesla_fleet_management_api/models/command_result'
132
+ require_relative 'tesla_fleet_management_api/models/guest_mode_request'
133
+ require_relative 'tesla_fleet_management_api/models/actuate_trunk_request'
134
+ require_relative 'tesla_fleet_management_api/models/add_charge_schedule_request'
135
+ require_relative 'tesla_fleet_management_api/models/' \
136
+ 'add_precondition_schedule_request'
137
+ require_relative 'tesla_fleet_management_api/models/adjust_volume_request'
138
+ require_relative 'tesla_fleet_management_api/models/command_response'
139
+ require_relative 'tesla_fleet_management_api/models/o_auth_token'
132
140
  require_relative 'tesla_fleet_management_api/models/default_real_mode'
133
141
  require_relative 'tesla_fleet_management_api/models/kind'
134
142
  require_relative 'tesla_fleet_management_api/models/' \
135
143
  'kind_get_wall_connector_charging_history'
136
- require_relative 'tesla_fleet_management_api/models/oauth_provider_error'
144
+ require_relative 'tesla_fleet_management_api/models/which_trunk'
145
+ require_relative 'tesla_fleet_management_api/models/o_auth_provider_error'
146
+ require_relative 'tesla_fleet_management_api/models/o_auth_scope_oauth2'
137
147
 
138
148
  # Exceptions
139
149
  require_relative 'tesla_fleet_management_api/exceptions/api_exception'
140
150
  require_relative 'tesla_fleet_management_api/exceptions/' \
141
- 'oauth_provider_exception'
151
+ 'o_auth_provider_exception'
142
152
 
143
153
  require_relative 'tesla_fleet_management_api/configuration'
144
154
 
@@ -150,4 +160,6 @@ require_relative 'tesla_fleet_management_api/controllers/partner_controller'
150
160
  require_relative 'tesla_fleet_management_api/controllers/user_controller'
151
161
  require_relative 'tesla_fleet_management_api/controllers/vehicles_controller'
152
162
  require_relative 'tesla_fleet_management_api/controllers/' \
153
- 'oauth_authorization_controller'
163
+ 'vehicle_commands_controller'
164
+ require_relative 'tesla_fleet_management_api/controllers/' \
165
+ 'o_auth_authorization_controller'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tesla-api-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammad Rafay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-01-22 00:00:00.000000000 Z
11
+ date: 2026-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: apimatic_core_interfaces
@@ -69,15 +69,16 @@ files:
69
69
  - lib/tesla_fleet_management_api/controllers/base_controller.rb
70
70
  - lib/tesla_fleet_management_api/controllers/charging_controller.rb
71
71
  - lib/tesla_fleet_management_api/controllers/energy_controller.rb
72
- - lib/tesla_fleet_management_api/controllers/oauth_authorization_controller.rb
72
+ - lib/tesla_fleet_management_api/controllers/o_auth_authorization_controller.rb
73
73
  - lib/tesla_fleet_management_api/controllers/partner_controller.rb
74
74
  - lib/tesla_fleet_management_api/controllers/user_controller.rb
75
+ - lib/tesla_fleet_management_api/controllers/vehicle_commands_controller.rb
75
76
  - lib/tesla_fleet_management_api/controllers/vehicles_controller.rb
76
77
  - lib/tesla_fleet_management_api/exceptions/api_exception.rb
77
- - lib/tesla_fleet_management_api/exceptions/oauth_provider_exception.rb
78
+ - lib/tesla_fleet_management_api/exceptions/o_auth_provider_exception.rb
78
79
  - lib/tesla_fleet_management_api/http/api_response.rb
79
80
  - lib/tesla_fleet_management_api/http/auth/bearer_auth.rb
80
- - lib/tesla_fleet_management_api/http/auth/oauth_2.rb
81
+ - lib/tesla_fleet_management_api/http/auth/oauth2.rb
81
82
  - lib/tesla_fleet_management_api/http/http_call_back.rb
82
83
  - lib/tesla_fleet_management_api/http/http_method_enum.rb
83
84
  - lib/tesla_fleet_management_api/http/http_request.rb
@@ -85,6 +86,10 @@ files:
85
86
  - lib/tesla_fleet_management_api/http/proxy_settings.rb
86
87
  - lib/tesla_fleet_management_api/logging/configuration/api_logging_configuration.rb
87
88
  - lib/tesla_fleet_management_api/logging/sdk_logger.rb
89
+ - lib/tesla_fleet_management_api/models/actuate_trunk_request.rb
90
+ - lib/tesla_fleet_management_api/models/add_charge_schedule_request.rb
91
+ - lib/tesla_fleet_management_api/models/add_precondition_schedule_request.rb
92
+ - lib/tesla_fleet_management_api/models/adjust_volume_request.rb
88
93
  - lib/tesla_fleet_management_api/models/api1_dx_vehicles_options_response.rb
89
94
  - lib/tesla_fleet_management_api/models/api1_dx_warranty_details_response.rb
90
95
  - lib/tesla_fleet_management_api/models/api1_vehicles_mobile_enabled_response.rb
@@ -111,6 +116,8 @@ files:
111
116
  - lib/tesla_fleet_management_api/models/charging_session.rb
112
117
  - lib/tesla_fleet_management_api/models/charging_sessions_data.rb
113
118
  - lib/tesla_fleet_management_api/models/charging_sessions_response.rb
119
+ - lib/tesla_fleet_management_api/models/command_response.rb
120
+ - lib/tesla_fleet_management_api/models/command_result.rb
114
121
  - lib/tesla_fleet_management_api/models/default_real_mode.rb
115
122
  - lib/tesla_fleet_management_api/models/driver.rb
116
123
  - lib/tesla_fleet_management_api/models/drivers_response.rb
@@ -121,6 +128,7 @@ files:
121
128
  - lib/tesla_fleet_management_api/models/fleet_telemetry_errors_response.rb
122
129
  - lib/tesla_fleet_management_api/models/fleet_telemetry_jws_request.rb
123
130
  - lib/tesla_fleet_management_api/models/generic_update_response.rb
131
+ - lib/tesla_fleet_management_api/models/guest_mode_request.rb
124
132
  - lib/tesla_fleet_management_api/models/kind.rb
125
133
  - lib/tesla_fleet_management_api/models/kind_get_wall_connector_charging_history.rb
126
134
  - lib/tesla_fleet_management_api/models/live_status_response.rb
@@ -128,8 +136,9 @@ files:
128
136
  - lib/tesla_fleet_management_api/models/location1.rb
129
137
  - lib/tesla_fleet_management_api/models/me_response.rb
130
138
  - lib/tesla_fleet_management_api/models/mobile_enabled.rb
131
- - lib/tesla_fleet_management_api/models/oauth_provider_error.rb
132
- - lib/tesla_fleet_management_api/models/oauth_token.rb
139
+ - lib/tesla_fleet_management_api/models/o_auth_provider_error.rb
140
+ - lib/tesla_fleet_management_api/models/o_auth_scope_oauth2.rb
141
+ - lib/tesla_fleet_management_api/models/o_auth_token.rb
133
142
  - lib/tesla_fleet_management_api/models/off_grid_vehicle_charging_reserve_request.rb
134
143
  - lib/tesla_fleet_management_api/models/operation_request.rb
135
144
  - lib/tesla_fleet_management_api/models/orders_response.rb
@@ -167,6 +176,7 @@ files:
167
176
  - lib/tesla_fleet_management_api/models/vehicle_base.rb
168
177
  - lib/tesla_fleet_management_api/models/vehicle_option.rb
169
178
  - lib/tesla_fleet_management_api/models/warranty_item.rb
179
+ - lib/tesla_fleet_management_api/models/which_trunk.rb
170
180
  - lib/tesla_fleet_management_api/utilities/date_time_helper.rb
171
181
  - lib/tesla_fleet_management_api/utilities/file_wrapper.rb
172
182
  homepage: https://developer.tesla.com/
@@ -1,154 +0,0 @@
1
- # tesla_fleet_management_api
2
- #
3
- # This file was automatically generated by
4
- # APIMATIC v3.0 ( https://www.apimatic.io ).
5
-
6
- module TeslaFleetManagementApi
7
- # Utility class for OAuth 2 authorization and token management.
8
- class Oauth2 < CoreLibrary::HeaderAuth
9
- include CoreLibrary
10
- # Display error message on occurrence of authentication failure.
11
- # @returns [String] The oAuth error message.
12
- def error_message
13
- 'Oauth2: OAuthToken is undefined or expired.'
14
- end
15
-
16
- # Initialization constructor.
17
- def initialize(oauth_2_credentials, config)
18
- auth_params = {}
19
- @_oauth_client_id = oauth_2_credentials.oauth_client_id unless
20
- oauth_2_credentials.nil? || oauth_2_credentials.oauth_client_id.nil?
21
- @_oauth_client_secret = oauth_2_credentials.oauth_client_secret unless
22
- oauth_2_credentials.nil? || oauth_2_credentials.oauth_client_secret.nil?
23
- @_oauth_redirect_uri = oauth_2_credentials.oauth_redirect_uri unless
24
- oauth_2_credentials.nil? || oauth_2_credentials.oauth_redirect_uri.nil?
25
- @_oauth_token = oauth_2_credentials.oauth_token unless
26
- oauth_2_credentials.nil? || oauth_2_credentials.oauth_token.nil?
27
- @_config = config
28
- @_o_auth_api = OauthAuthorizationController.new(config)
29
- auth_params[:Authorization] = "Bearer #{@_oauth_token.access_token}" unless @_oauth_token.nil?
30
-
31
- super auth_params
32
- end
33
-
34
- # Validates the oAuth token.
35
- # @return [Boolean] true if the token is present and not expired.
36
- def valid
37
- !@_oauth_token.nil? && !token_expired?(@_oauth_token)
38
- end
39
-
40
- # Builds and returns an authorization URL.
41
- # The user is expected to obtain an authorization code from this URL and then call the
42
- # fetch token function with that authorization code.
43
- # @param [String] state An opaque state string.
44
- # @param [Hash] additional_params Any additional query parameters to be added to the URL.
45
- # @return [String] The authorization URL.
46
- def get_authorization_url(state: nil, additional_params: nil)
47
- auth_url = @_config.get_base_uri_executor.call(Server::AUTH_SERVER)
48
- auth_url += '/authorize'
49
- query_params = {
50
- 'response_type' => 'code',
51
- 'client_id' => @_oauth_client_id,
52
- 'redirect_uri' => @_oauth_redirect_uri
53
- }
54
- query_params['scope'] = Array(@_o_auth_scopes).compact.join(' ') if @_o_auth_scopes
55
- query_params['state'] = state if state
56
- query_params.merge!(additional_params) if additional_params
57
- auth_url = APIHelper.append_url_with_query_parameters(auth_url,
58
- query_params)
59
- APIHelper.clean_url(auth_url)
60
- end
61
-
62
- # Builds the basic auth header for endpoints in the OAuth Authorization Controller.
63
- # @return [String] The value of the Authentication header.
64
- def build_basic_auth_header
65
- "Basic #{AuthHelper.get_base64_encoded_value(@_oauth_client_id, @_oauth_client_secret)}"
66
- end
67
-
68
- # Fetches the token.
69
- # @param [String] auth_code The authentication code.
70
- # @param [Hash] additional_params Any additional form parameters.
71
- # @return [OAuthToken] The oAuth token instance.
72
- def fetch_token(auth_code, additional_params: nil)
73
- token = @_o_auth_api.request_token_oauth_2(
74
- build_basic_auth_header,
75
- auth_code,
76
- @_oauth_redirect_uri,
77
- _field_parameters: additional_params
78
- ).data
79
- if token.respond_to?('expires_in') && !token.expires_in.nil?
80
- token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i)
81
- end
82
- token
83
- end
84
-
85
- # Checks if OAuth token has expired.
86
- # @param [OAuthToken] token The oAuth token instance.
87
- # @return [Boolean] true if the token is present and not expired.
88
- def token_expired?(token)
89
- token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry)
90
- end
91
-
92
- # Refreshes OAuth token.
93
- # @param [Hash] additional_params Any additional form parameters.
94
- # @return [OAuthToken] The oAuth token instance.
95
- def refresh_token(additional_params: nil)
96
- token = @_o_auth_api.refresh_token_oauth_2(
97
- build_basic_auth_header,
98
- @_oauth_token.refresh_token,
99
- _field_parameters: additional_params
100
- ).data
101
- if token.respond_to?('expires_in') && !token.expires_in.nil?
102
- token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i)
103
- end
104
- token
105
- end
106
- end
107
-
108
- # Data class for Oauth2Credentials.
109
- class Oauth2Credentials
110
- attr_reader :oauth_client_id, :oauth_client_secret, :oauth_redirect_uri,
111
- :oauth_token
112
-
113
- def initialize(oauth_client_id:, oauth_client_secret:, oauth_redirect_uri:,
114
- oauth_token: nil)
115
- raise ArgumentError, 'oauth_client_id cannot be nil' if oauth_client_id.nil?
116
- raise ArgumentError, 'oauth_client_secret cannot be nil' if oauth_client_secret.nil?
117
- raise ArgumentError, 'oauth_redirect_uri cannot be nil' if oauth_redirect_uri.nil?
118
-
119
- @oauth_client_id = oauth_client_id
120
- @oauth_client_secret = oauth_client_secret
121
- @oauth_redirect_uri = oauth_redirect_uri
122
- @oauth_token = oauth_token
123
- end
124
-
125
- def self.from_env
126
- oauth_client_id = ENV['OAUTH_2_OAUTH_CLIENT_ID']
127
- oauth_client_secret = ENV['OAUTH_2_OAUTH_CLIENT_SECRET']
128
- oauth_redirect_uri = ENV['OAUTH_2_OAUTH_REDIRECT_URI']
129
- all_nil = [
130
- oauth_client_id,
131
- oauth_client_secret,
132
- oauth_redirect_uri
133
- ].all?(&:nil?)
134
- return nil if all_nil
135
-
136
- new(oauth_client_id: oauth_client_id,
137
- oauth_client_secret: oauth_client_secret,
138
- oauth_redirect_uri: oauth_redirect_uri)
139
- end
140
-
141
- def clone_with(oauth_client_id: nil, oauth_client_secret: nil,
142
- oauth_redirect_uri: nil, oauth_token: nil)
143
- oauth_client_id ||= self.oauth_client_id
144
- oauth_client_secret ||= self.oauth_client_secret
145
- oauth_redirect_uri ||= self.oauth_redirect_uri
146
- oauth_token ||= self.oauth_token
147
-
148
- Oauth2Credentials.new(oauth_client_id: oauth_client_id,
149
- oauth_client_secret: oauth_client_secret,
150
- oauth_redirect_uri: oauth_redirect_uri,
151
- oauth_token: oauth_token)
152
- end
153
- end
154
- end