omniauth-tesla 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3962bf12bb36e963a0c12b1a1101685c680059c0428adb59e9723aa71017b64
4
+ data.tar.gz: 218bf632239f2b9e40a5ac610cd2c4a4e6eececf74d2c437e415ae5fd3cf3bd9
5
+ SHA512:
6
+ metadata.gz: 575f8d8d322e468ee6a0d42e2d55c66008ada7d255ae29bc50833429110108d83a62bce15af1adfe2400670c7988e12add4160bb3b1f6298e7b0770ac243d566
7
+ data.tar.gz: d427bef752243cd9f739175ad110054146bee588a85d5a480d62956c7084d539bcd2f8fafe0dd58f3a4121c0d2a3e49091fa7104b34913fd7c17656748ac907b
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ *.gem
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gem "multi_json"
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+
2
+ ## Development
3
+
4
+ OAuth2 strategy OmniAuth for Tesla's OAuth2 API.
5
+
6
+ Example usage:
7
+
8
+ ```ruby
9
+ Rails.application.config.middleware.use OmniAuth::Builder do
10
+ provider :tesla,
11
+ client_id: ENV['TESLA_CLIENT_ID'],
12
+ client_secret: ENV['TESLA_CLIENT_SECRET'],
13
+ scope: 'openid offline_access vehicle_device_data user_data'
14
+ end
15
+ ```
16
+
17
+ ## License
18
+
19
+ MIT License
20
+
21
+ Copyright (c) 2024 Jamie Quint
22
+
23
+ Permission is hereby granted, free of charge, to any person obtaining a copy
24
+ of this software and associated documentation files (the "Software"), to deal
25
+ in the Software without restriction, including without limitation the rights
26
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27
+ copies of the Software, and to permit persons to whom the Software is
28
+ furnished to do so, subject to the following conditions:
29
+
30
+ The above copyright notice and this permission notice shall be included in all
31
+ copies or substantial portions of the Software.
32
+
33
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39
+ SOFTWARE.
@@ -0,0 +1,152 @@
1
+ # lib/omniauth/strategies/tesla.rb
2
+ require 'omniauth-oauth2'
3
+ require 'multi_json'
4
+
5
+ module OmniAuth
6
+ module Strategies
7
+ class Tesla < OmniAuth::Strategies::OAuth2
8
+ option :name, 'tesla'
9
+
10
+ # Tesla's OAuth endpoints:
11
+ option :client_options, {
12
+ site: 'https://auth.tesla.com',
13
+ authorize_url: 'https://auth.tesla.com/oauth2/v3/authorize',
14
+ token_url: 'https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token'
15
+ }
16
+
17
+ # Default scopes (adjust as needed for user_data, etc.):
18
+ option :scope, 'openid offline_access user_data'
19
+
20
+ # Default audience required for Tesla token exchange:
21
+ option :audience, 'https://fleet-api.prd.na.vn.cloud.tesla.com'
22
+
23
+ option :authorize_params, {
24
+ response_type: 'code'
25
+ }
26
+
27
+ # Define class-level accessors for OAuth configuration
28
+ class << self
29
+ attr_accessor :client_id, :client_secret, :site, :authorize_url, :token_url, :audience
30
+ end
31
+
32
+ # Initialize class-level accessors with instance-level options
33
+ def initialize(*args, &block)
34
+ super
35
+ self.class.client_id ||= options.client_id
36
+ self.class.client_secret ||= options.client_secret
37
+ self.class.site ||= options.client_options.site
38
+ self.class.authorize_url ||= options.client_options.authorize_url
39
+ self.class.token_url ||= options.client_options.token_url
40
+ self.class.audience ||= options.audience
41
+ end
42
+
43
+ # Override authorize_params to include necessary parameters
44
+ def authorize_params
45
+ super.tap do |params|
46
+ # In case someone sets a custom scope in the provider config.
47
+ params[:scope] ||= options[:scope]
48
+ # Explicitly include client_id
49
+ params[:client_id] = options.client_id
50
+ end
51
+ end
52
+
53
+ # Add audience into the token request
54
+ def token_params
55
+ super.tap do |params|
56
+ # Required parameters for Tesla token exchange
57
+ params[:grant_type] = 'authorization_code'
58
+ params[:code] = request.params['code']
59
+ params[:client_id] = options.client_id
60
+ params[:client_secret] = options.client_secret
61
+ params[:audience] = options[:audience]
62
+ params[:redirect_uri] = callback_url
63
+ end
64
+ end
65
+
66
+ # Override build_access_token if necessary
67
+ def build_access_token
68
+ verifier = request.params['code']
69
+ client.auth_code.get_token(
70
+ verifier,
71
+ token_params,
72
+ deep_symbolize(options.auth_token_params || {})
73
+ )
74
+ end
75
+
76
+ # UID is vault_uuid from the user info response
77
+ uid { raw_info.dig('response', 'vault_uuid') }
78
+
79
+ # User info retrieved from Tesla's API
80
+ info do
81
+ response_data = raw_info['response'] || {}
82
+ {
83
+ email: response_data['email'],
84
+ full_name: response_data['full_name'],
85
+ profile_image_url: response_data['profile_image_url']
86
+ }
87
+ end
88
+
89
+ # Extra information (raw user info)
90
+ extra do
91
+ { raw_info: raw_info }
92
+ end
93
+
94
+ # Fetch user info from /api/1/users/me
95
+ def raw_info
96
+ @raw_info ||= begin
97
+ url = 'https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/users/me'
98
+
99
+ response = access_token.get(url, headers: {
100
+ 'Content-Type' => 'application/json'
101
+ })
102
+
103
+ MultiJson.load(response.body)
104
+ rescue ::OAuth2::Error => e
105
+ warn "OmniAuth Tesla raw_info error: #{e.response&.body}"
106
+ {}
107
+ end
108
+ end
109
+
110
+ # Store access_token info (token, refresh_token, expires, etc.)
111
+ credentials do
112
+ hash = { 'token' => access_token.token }
113
+ hash['refresh_token'] = access_token.refresh_token if access_token.refresh_token
114
+ hash['expires_at'] = access_token.expires_at if access_token.expires?
115
+ hash['expires'] = access_token.expires?
116
+ hash
117
+ end
118
+
119
+ # Override callback_url if necessary
120
+ def callback_url
121
+ full_host + script_name + callback_path
122
+ end
123
+
124
+ # Optionally log the request phase
125
+ def request_phase
126
+ super
127
+ end
128
+
129
+ # Class-level helper to refresh an access token using a saved refresh_token
130
+ def self.refresh_with(refresh_token)
131
+ client = ::OAuth2::Client.new(
132
+ self.client_id,
133
+ self.client_secret,
134
+ site: self.site,
135
+ authorize_url: self.authorize_url,
136
+ token_url: self.token_url
137
+ )
138
+
139
+ token_obj = ::OAuth2::AccessToken.new(client, '', refresh_token: refresh_token)
140
+ new_token = token_obj.refresh!
141
+ new_token
142
+ rescue ::OAuth2::Error => e
143
+ # Use a generic warning for logging since Rails.logger may not be available
144
+ warn "Tesla refresh error: #{e.message}"
145
+ nil
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ # Add camelization for the Tesla strategy
152
+ OmniAuth.config.add_camelization 'tesla', 'Tesla'
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Tesla
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'omniauth-tesla/version'
2
+ require 'omniauth/strategies/tesla'
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+ require 'omniauth-tesla/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'omniauth-tesla'
7
+ spec.version = Omniauth::Tesla::VERSION
8
+ spec.authors = ['Jamie Quint']
9
+ spec.email = ['jamiequint@gmail.com']
10
+ spec.summary = 'OmniAuth strategy for Tesla OAuth (authorization_code flow)'
11
+ spec.description = 'An OmniAuth strategy that supports Tesla’s Fleet API OAuth flow.'
12
+ spec.homepage = 'https://github.com/YourUserName/omniauth-tesla'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.require_paths = ['lib']
19
+
20
+ # Adjust your runtime dependency version constraints as needed:
21
+ spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.5'
22
+
23
+ # Common development dependencies (optional):
24
+ spec.add_development_dependency 'bundler', '~> 2.0'
25
+ spec.add_development_dependency 'rake', '~> 12.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+
28
+ # If you have a bin/ directory with executables, you can do:
29
+ # spec.bindir = 'exe'
30
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-tesla
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Quint
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: An OmniAuth strategy that supports Tesla’s Fleet API OAuth flow.
70
+ email:
71
+ - jamiequint@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - README.md
79
+ - lib/omniauth-tesla.rb
80
+ - lib/omniauth-tesla/version.rb
81
+ - lib/omniauth/strategies/tesla.rb
82
+ - omniauth-tesla.gemspec
83
+ homepage: https://github.com/YourUserName/omniauth-tesla
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.4.10
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: OmniAuth strategy for Tesla OAuth (authorization_code flow)
106
+ test_files: []