omniauth-atproto 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e79046605d1235915ec27cb5f200e0744f1a645c630efcf5792b7ee3563d86f8
4
- data.tar.gz: b36566fae3041251882c97e10c9d110fc5893edcf2da9e1f779d1c9aee525d42
3
+ metadata.gz: bc86c1376053df62fcdc24db7d18eb370727215726af22bb2780a6c36f4f85f3
4
+ data.tar.gz: 28c3f9a7e1240b04121e859e913f9feb21fe69394d3e745d6fe4960e71d3ed88
5
5
  SHA512:
6
- metadata.gz: a2bffaa5781a042deb22166582c8d3d0301abee04eccd1c01ccfee320a5cd3af590a0e1a41e97e94a4e1e70b16bb3421ec5b7102334caf83f9421df76111a1e0
7
- data.tar.gz: babee933a42f7d584dd7899aab66434d9b5c4438dc524ded0f4030b401bc1205a00d01547000787ede9835410fdaa52118b10ff6379f396e644080dd371da1e1
6
+ metadata.gz: 4941f9b4bf7355f2881a779210d65070311d920fa708e9afbb1201510c114d13c2c4d41fa9e55f6631795090d73062d446db42be816c6b4d33316057e73ce164
7
+ data.tar.gz: 60e567ef2f1fba250cf59211f2e9b1f174741103527f74c989dd2807236871ea513633c8d60bb36a793cf9d0e84bc9b4436d8c18ef25797a4eb5b3dd12fbd7f0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 François Brault
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -19,103 +19,54 @@ module OmniAuth
19
19
  }
20
20
  end
21
21
 
22
- def request_phase
23
- unless has_default_client_options?
24
- @handle = request.params['handle']
25
-
26
- unless @handle
27
- fail!(:missing_handle,
28
- OmniAuth::Error.new(
29
- 'Handle parameter is required if no client options are set'
30
- ))
22
+ def self.setup
23
+ lambda do |env|
24
+ session = env['rack.session']
25
+
26
+ if env['rack.request.form_hash'] && handle = env['rack.request.form_hash']['handle']
27
+ resolver = DIDKit::Resolver.new
28
+ did = resolver.resolve_handle(handle)
29
+
30
+ unless did
31
+ env['omniauth.strategy'].fail!(:unknown_handle,
32
+ OmniAuth::Error.new(
33
+ 'Handle parameter did not resolve to a did'
34
+ ))
35
+ end
36
+
37
+ endpoint = resolver.resolve_did(did).pds_endpoint
38
+ auth_server = get_authorization_server(endpoint)
39
+ session['authorization_info'] = authorization_info = get_authorization_data(auth_server)
31
40
  end
32
41
 
33
- set_client_options
42
+ if authorization_info ||= session.delete('authorization_info')
43
+ env['omniauth.strategy'].options['client_options']['site'] = authorization_info['issuer']
44
+ env['omniauth.strategy'].options['client_options']['authorize_url'] =
45
+ authorization_info['authorization_endpoint']
46
+ env['omniauth.strategy'].options['client_options']['token_url'] = authorization_info['token_endpoint']
47
+ end
34
48
  end
35
- super
36
- end
37
-
38
- private
39
-
40
- def has_default_client_options?
41
- %i[site authorize_url token_url].all? { |k| options.client_options.key? k }
42
49
  end
43
50
 
44
- def set_client_options
45
- options.client_options[:site] = authorization_info['issuer']
46
- options.client_options[:authorize_url] = authorization_info['authorization_endpoint']
47
- options.client_options[:token_url] = authorization_info['token_endpoint']
48
- end
51
+ option :setup, setup
49
52
 
50
- def authorization_info
51
- session['omniauth.auth_info'] ||= begin
52
- resolver = DIDKit::Resolver.new
53
- did = resolver.resolve_handle(@handle)
54
- endpoint = resolver.resolve_did(did).pds_endpoint
55
- auth_server = get_authorization_server(endpoint)
56
- auth_info = get_authorization_data(auth_server)
57
- end
58
- end
53
+ private
59
54
 
60
55
  def build_access_token
61
- set_client_options unless has_default_client_options?
62
-
63
- new_token_params = token_params.merge(
64
- {
65
- grant_type: 'authorization_code',
66
- redirect_uri: full_host + callback_path,
67
- code: request.params['code'],
68
- client_id: options.client_id,
69
- client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
70
- client_assertion: generate_client_assertion
71
- }
72
- )
73
- dpop_handler = AtProto::DpopHandler.new(options.private_key)
74
- response = dpop_handler.make_request(
75
- client.token_url,
76
- :post,
77
- headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' },
78
- body: new_token_params
56
+ response = AtProto::Client.new(private_key: options.private_key).get_token!(
57
+ **token_params.merge({
58
+ code: request.params['code'],
59
+ jwk: options.client_jwk,
60
+ client_id: options.client_id,
61
+ redirect_uri: full_host + callback_path,
62
+ site: options.client_options.site,
63
+ endpoint: options.client_options.token_url
64
+ }).to_h.symbolize_keys
79
65
  )
80
-
81
66
  ::OAuth2::AccessToken.from_hash(client, response)
82
67
  end
83
68
 
84
- def generate_client_assertion
85
- # Should return a JWT signed with the private key corresponding to the one in client-metadata.json
86
-
87
- raise 'Client ID is required' unless options.client_id
88
- raise 'Client JWK is required' unless options.client_jwk
89
-
90
- private_key = if options.private_key.is_a?(String)
91
- OpenSSL::PKey::EC.new(options.private_key)
92
- elsif options.private_key.is_a?(OpenSSL::PKey::EC)
93
- options.private_key
94
- else
95
- raise 'Invalid private_key format'
96
- end
97
- jwt_payload = {
98
- iss: options.client_id,
99
- sub: options.client_id,
100
- aud: options.client_options.site,
101
- jti: SecureRandom.uuid,
102
- iat: Time.now.to_i,
103
- exp: Time.now.to_i + 300
104
- }
105
-
106
- JWT.encode(
107
- jwt_payload,
108
- private_key,
109
- 'ES256',
110
- {
111
- typ: 'jwt',
112
- alg: 'ES256',
113
- kid: options.client_jwk[:kid]
114
- }
115
- )
116
- end
117
-
118
- def get_authorization_server(pds_endpoint)
69
+ def self.get_authorization_server(pds_endpoint)
119
70
  response = Faraday.get("#{pds_endpoint}/.well-known/oauth-protected-resource")
120
71
 
121
72
  unless response.success?
@@ -135,7 +86,7 @@ module OmniAuth
135
86
  auth_server
136
87
  end
137
88
 
138
- def get_authorization_data(issuer)
89
+ def self.get_authorization_data(issuer)
139
90
  response = Faraday.get("#{issuer}/.well-known/oauth-authorization-server")
140
91
 
141
92
  unless response.success?
@@ -48,11 +48,11 @@ module OmniAuth
48
48
  def rotate_keys
49
49
  # Backup current keys if they exist
50
50
  if File.exist?(KEY_PATH)
51
- # File.write(KEY_PATH, 'config/old_atproto_private_key.pem')
51
+ File.write(KEY_PATH, 'config/old_atproto_private_key.pem')
52
52
  FileUtils.rm(KEY_PATH)
53
53
  end
54
54
  if File.exist?(JWK_PATH)
55
- # File.write(JWK_PATH, 'config/old_atproto_jwk.json')
55
+ File.write(JWK_PATH, 'config/old_atproto_jwk.json')
56
56
  FileUtils.rm(JWK_PATH)
57
57
  end
58
58
  load_or_generate_keys
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module Atproto
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-atproto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - frabr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-29 00:00:00.000000000 Z
11
+ date: 2024-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: atproto_client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.1.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.1.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: didkit
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -129,6 +129,7 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
+ - LICENSE
132
133
  - README.md
133
134
  - lib/omniauth-atproto.rb
134
135
  - lib/omniauth-atproto/key_manager.rb