persona-protocol 0.1.0 → 0.2.0

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: 3c3d3ff355f856b7467643bf5e39a108d11be6d4e8f471e01203b1b9059836d4
4
- data.tar.gz: 6d30790a52861e0f69f6bc0d21d1807d56de740236cf61db336d12e96d423ea2
3
+ metadata.gz: 6c1f645321fdcc44637d072a5f02026346b241566dde012079b360e1968b9b09
4
+ data.tar.gz: 4ea7754024d2f0c6076ea604502c3f9e267fb02936c6d3f4c14b344ce640b455
5
5
  SHA512:
6
- metadata.gz: 3b6a984f0354d4c0f0079921b32ab9a7a5c6e1fdc2cb4f10c790ca85dd06ef294e248937407b615a2b6ee96958adeb4db81c8f6806c003ba06b34d21e3dbd098
7
- data.tar.gz: fee2f202019c0341c2f3ef0064b3d851a8e83b1938eb6f21df3b986ba69eedf437792fd15905d072cc975ce5ddda39749f7711edc7ce545a329c9e28c6019f95
6
+ metadata.gz: 802a88535e118c8b57ae85d59a3bb4e7142b153f4f3bc3fbc5a32cb59fe55c6c0edff8a20ac09bc3dc00b420bd787b864467914d065f0171fc05a4fd123138c2
7
+ data.tar.gz: e57843cd141afded4c68f1d32dbd85f7b5f89b0947dbe502d5a3ed90fdc361439b8cc2907cd234091c24b6f6fe7fb822d0d1a630831bd25622ef4fee0fb70aa5
@@ -47,12 +47,12 @@ module Persona
47
47
  take(PENDING_PREFIX + state)
48
48
  end
49
49
 
50
- def put_result(link_token, provider:, subject:, agent_uid:)
51
- write(
52
- RESULT_PREFIX + link_token,
53
- { provider: provider, subject: subject, agent_uid: agent_uid },
54
- RESULT_TTL,
55
- )
50
+ # attributes: what the provider stated about the account (P-16), handed
51
+ # to the application once the link settles; never the access token.
52
+ def put_result(link_token, provider:, subject:, agent_uid:, attributes: nil)
53
+ data = { provider: provider, subject: subject, agent_uid: agent_uid }
54
+ data[:attributes] = attributes unless attributes.nil?
55
+ write(RESULT_PREFIX + link_token, data, RESULT_TTL)
56
56
  end
57
57
 
58
58
  # Non-destructive: the merge-preview first call must leave the token
@@ -0,0 +1,185 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'digest'
4
+ require 'base64'
5
+ require 'securerandom'
6
+
7
+ module Persona
8
+ module Oidc
9
+ # The identity-endpoint verifier (docs/spec P-16): for providers that issue
10
+ # no OpenID Connect id_token, or whose id_token subject is not the stable
11
+ # account identifier. A confidential client
12
+ # exchanges the code at the token endpoint, presents the access token to
13
+ # the provider's identity endpoint, and takes the subject from the stable
14
+ # account identifier in the answer. Both calls go over TLS; the access
15
+ # token never leaves #verify. Mirrors the TypeScript
16
+ # createOAuth2VerifierProvider (packages/server-core/src/oauth2_verifier.ts).
17
+ class OAuth2Verifier
18
+ # A stable account identifier as a string: a non-empty string, or a
19
+ # non-negative integer within the range every JSON consumer reads exactly.
20
+ def self.stable_id(value)
21
+ case value
22
+ when String then value.empty? ? nil : value
23
+ when Integer then value >= 0 && value <= 9_007_199_254_740_991 ? value.to_s : nil
24
+ end
25
+ end
26
+
27
+ READ_TOP_LEVEL_ID = lambda do |body|
28
+ subject = body.is_a?(Hash) ? stable_id(body['id']) : nil
29
+ subject && { subject: subject }
30
+ end
31
+
32
+ # uniba/auth's /api/v0/me: the member id (`mem_…`) is the subject; only
33
+ # the ids of the member's teams are kept, as of the link.
34
+ READ_UNIBA_MEMBER = lambda do |body|
35
+ next nil unless body.is_a?(Hash)
36
+
37
+ id = body['id']
38
+ next nil unless id.is_a?(String) && id.start_with?('mem_')
39
+
40
+ teams = body['teams']
41
+ next nil unless teams.is_a?(Array)
42
+
43
+ team_ids = teams.map { |t| t.is_a?(Hash) ? t['id'] : nil }
44
+ next nil unless team_ids.all? { |t| t.is_a?(String) && !t.empty? }
45
+
46
+ { subject: id, attributes: { teams: team_ids } }
47
+ end
48
+
49
+ UNIBA_AUTH = {
50
+ production: {
51
+ authorization_endpoint: 'https://auth.uniba.jp/authorize',
52
+ token_endpoint: 'https://id.uniba.jp/v1/oauth2/token',
53
+ identity_endpoint: 'https://auth.uniba.jp/api/v0/me',
54
+ },
55
+ staging: {
56
+ authorization_endpoint: 'https://auth-stg.uniba.jp/authorize',
57
+ token_endpoint: 'https://id-stg.uniba.jp/v1/oauth2/token',
58
+ identity_endpoint: 'https://auth-stg.uniba.jp/api/v0/me',
59
+ },
60
+ }.freeze
61
+
62
+ attr_reader :name
63
+
64
+ def initialize(
65
+ name:, authorization_endpoint:, token_endpoint:, identity_endpoint:,
66
+ client_id:, client_secret:, redirect_uri:,
67
+ scope: nil, pkce: true, authorize_params: {}, identity_headers: {},
68
+ read_identity: READ_TOP_LEVEL_ID, http: NetHttpClient.new
69
+ )
70
+ require_https!('token_endpoint', token_endpoint)
71
+ require_https!('identity_endpoint', identity_endpoint)
72
+ if client_secret.nil? || client_secret.to_s.empty?
73
+ raise ArgumentError, "#{name}: an identity-endpoint verifier needs a client secret"
74
+ end
75
+
76
+ @name = name
77
+ @authorization_endpoint = authorization_endpoint
78
+ @token_endpoint = token_endpoint
79
+ @identity_endpoint = identity_endpoint
80
+ @client_id = client_id
81
+ @client_secret = client_secret
82
+ @redirect_uri = redirect_uri
83
+ @scope = scope
84
+ @pkce = pkce
85
+ @authorize_params = authorize_params
86
+ @identity_headers = identity_headers
87
+ @read_identity = read_identity
88
+ @http = http
89
+ end
90
+
91
+ # GitHub OAuth app. Subject: the numeric user id (logins can be renamed
92
+ # and reused).
93
+ def self.github(client_id:, client_secret:, redirect_uri:, user_agent: 'persona-protocol',
94
+ http: NetHttpClient.new)
95
+ new(
96
+ name: 'github',
97
+ authorization_endpoint: 'https://github.com/login/oauth/authorize',
98
+ token_endpoint: 'https://github.com/login/oauth/access_token',
99
+ identity_endpoint: 'https://api.github.com/user',
100
+ client_id: client_id, client_secret: client_secret, redirect_uri: redirect_uri,
101
+ identity_headers: { 'Accept' => 'application/vnd.github+json', 'User-Agent' => user_agent },
102
+ http: http,
103
+ )
104
+ end
105
+
106
+ # Discord. Subject: the user id (snowflake), kept as the string Discord
107
+ # sends. `identify` yields the id without email or guilds.
108
+ def self.discord(client_id:, client_secret:, redirect_uri:, http: NetHttpClient.new)
109
+ new(
110
+ name: 'discord',
111
+ authorization_endpoint: 'https://discord.com/oauth2/authorize',
112
+ token_endpoint: 'https://discord.com/api/oauth2/token',
113
+ identity_endpoint: 'https://discord.com/api/users/@me',
114
+ client_id: client_id, client_secret: client_secret, redirect_uri: redirect_uri,
115
+ scope: 'identify', pkce: false, http: http,
116
+ # Show the consent screen every time, so a link is never completed
117
+ # silently by a round-trip someone else started.
118
+ authorize_params: { prompt: 'consent' },
119
+ )
120
+ end
121
+
122
+ # uniba/auth (github.com/uniba/auth). Subject: the member id from the
123
+ # versioned /api/v0/me, not the id_token `sub` (issuer-internal).
124
+ def self.uniba_auth(client_id:, client_secret:, redirect_uri:, environment: :production,
125
+ http: NetHttpClient.new)
126
+ new(
127
+ name: 'uniba-auth', **UNIBA_AUTH.fetch(environment.to_sym),
128
+ client_id: client_id, client_secret: client_secret, redirect_uri: redirect_uri,
129
+ scope: 'openid', read_identity: READ_UNIBA_MEMBER, http: http,
130
+ )
131
+ end
132
+
133
+ def authorize(state)
134
+ query = { response_type: 'code', client_id: @client_id, redirect_uri: @redirect_uri, state: state }
135
+ query[:scope] = @scope if @scope
136
+ query.merge!(@authorize_params)
137
+ stash = nil
138
+ if @pkce
139
+ code_verifier = SecureRandom.urlsafe_base64(32)
140
+ query[:code_challenge] = Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier), padding: false)
141
+ query[:code_challenge_method] = 'S256'
142
+ stash = { code_verifier: code_verifier }
143
+ end
144
+ AuthorizeStart.new(url: "#{@authorization_endpoint}?#{URI.encode_www_form(query)}", stash: stash)
145
+ end
146
+
147
+ def verify(params, stash = nil)
148
+ code = params[:code] || params['code']
149
+ return nil if code.nil? || code.to_s.empty?
150
+
151
+ access_token = exchange_code(code, stash)
152
+ return nil if access_token.nil?
153
+
154
+ body = @http.get(@identity_endpoint, @identity_headers.merge('Authorization' => "Bearer #{access_token}"))
155
+ return nil if body.nil?
156
+
157
+ read = @read_identity.call(JSON.parse(body))
158
+ return nil if read.nil?
159
+
160
+ Identity.new(provider: @name, subject: read[:subject], attributes: read[:attributes])
161
+ rescue JSON::ParserError, SystemCallError, IOError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError
162
+ nil
163
+ end
164
+
165
+ private
166
+
167
+ def exchange_code(code, stash)
168
+ form = { grant_type: 'authorization_code', code: code, redirect_uri: @redirect_uri,
169
+ client_id: @client_id, client_secret: @client_secret }
170
+ code_verifier = stash && (stash[:code_verifier] || stash['code_verifier'])
171
+ form[:code_verifier] = code_verifier if code_verifier
172
+
173
+ body = @http.post_form(@token_endpoint, form)
174
+ return nil if body.nil?
175
+
176
+ token = JSON.parse(body)['access_token']
177
+ token.is_a?(String) && !token.empty? ? token : nil
178
+ end
179
+
180
+ def require_https!(label, url)
181
+ raise ArgumentError, "#{label} must be https: #{url}" unless URI(url).scheme == 'https'
182
+ end
183
+ end
184
+ end
185
+ end
@@ -12,9 +12,11 @@ module Persona
12
12
  # the token exchange. Injectable — tests pass a fake responding to the same
13
13
  # #get / #post_form contract. Returns the response body on 2xx, nil otherwise.
14
14
  class NetHttpClient
15
- def get(url)
15
+ def get(url, headers = {})
16
16
  uri = URI(url)
17
- request(uri, Net::HTTP::Get.new(uri))
17
+ req = Net::HTTP::Get.new(uri)
18
+ headers.each { |k, v| req[k] = v }
19
+ request(uri, req)
18
20
  end
19
21
 
20
22
  def post_form(url, params)
@@ -36,7 +38,8 @@ module Persona
36
38
  # Production OIDC verifier provider (docs/spec §7, P-16): the
37
39
  # authorization-code + PKCE flow, OIDC discovery, code exchange, and
38
40
  # id_token validation against the IdP's published JWKS. Mirrors the
39
- # TypeScript createOidcVerifierProvider.
41
+ # TypeScript createOidcVerifierProvider. Providers without an id_token use
42
+ # OAuth2Verifier (the identity-endpoint verifier).
40
43
  #
41
44
  # Signature verification uses the jwt gem's OpenSSL backend and covers
42
45
  # RS256 and ES256. EdDSA (Ed25519) is intentionally not enabled here — it
@@ -55,6 +58,7 @@ module Persona
55
58
  client_secret: nil, scope: 'openid email',
56
59
  authorization_endpoint: nil, token_endpoint: nil, jwks_uri: nil,
57
60
  authorize_params: {}, require_hosted_domain: nil, allowed_email_domain: nil,
61
+ read_claims: nil,
58
62
  http: NetHttpClient.new, now: -> { Time.now.to_i }, clock_tolerance: 60
59
63
  )
60
64
  @name = name
@@ -68,6 +72,7 @@ module Persona
68
72
  @authorize_params = authorize_params
69
73
  @require_hosted_domain = require_hosted_domain
70
74
  @allowed_email_domain = allowed_email_domain
75
+ @read_claims = read_claims
71
76
  @http = http
72
77
  @now = now
73
78
  @clock_tolerance = clock_tolerance
@@ -75,18 +80,22 @@ module Persona
75
80
  @jwks = nil
76
81
  end
77
82
 
78
- # Preset for uniba/auth (github.com/uniba/auth): the uniba.jp shared OIDC
79
- # AS. Fills in the name, scope, and hd=uniba.jp upstream constraint; the
80
- # issuer and client credentials stay per-deployment. auth is not yet
81
- # published, so the token-claim domain check defaults to the email domain
82
- # (provisional). Pass allowed_email_domain: nil to disable it.
83
- def self.uniba_auth(issuer:, client_id:, redirect_uri:, client_secret: nil,
84
- allowed_email_domain: 'uniba.jp',
85
- http: NetHttpClient.new, now: -> { Time.now.to_i })
83
+ # Slack ("Sign in with Slack", OpenID Connect). A Slack user id is only
84
+ # unique within its workspace, so the subject is `<team_id>/<user_id>`
85
+ # from Slack's namespaced claims.
86
+ READ_SLACK_CLAIMS = lambda do |claims|
87
+ team = claims['https://slack.com/team_id']
88
+ user = claims['https://slack.com/user_id'] || claims['sub']
89
+ next nil unless team.is_a?(String) && !team.empty? && user.is_a?(String) && !user.empty?
90
+
91
+ { subject: "#{team}/#{user}" }
92
+ end
93
+
94
+ def self.slack(client_id:, client_secret:, redirect_uri:, http: NetHttpClient.new,
95
+ now: -> { Time.now.to_i })
86
96
  new(
87
- name: 'uniba-auth', issuer: issuer, client_id: client_id, redirect_uri: redirect_uri,
88
- client_secret: client_secret, scope: 'openid email',
89
- authorize_params: { hd: 'uniba.jp' }, allowed_email_domain: allowed_email_domain,
97
+ name: 'slack', issuer: 'https://slack.com', client_id: client_id, redirect_uri: redirect_uri,
98
+ client_secret: client_secret, scope: 'openid', read_claims: READ_SLACK_CLAIMS,
90
99
  http: http, now: now,
91
100
  )
92
101
  end
@@ -165,7 +174,10 @@ module Persona
165
174
  verify_expiration: false)
166
175
  return nil unless valid_claims?(payload, stash)
167
176
 
168
- Identity.new(provider: @name, subject: payload['sub'])
177
+ return Identity.new(provider: @name, subject: payload['sub']) if @read_claims.nil?
178
+
179
+ read = @read_claims.call(payload)
180
+ read && Identity.new(provider: @name, subject: read[:subject], attributes: read[:attributes])
169
181
  rescue JWT::DecodeError, JWT::JWKError
170
182
  nil
171
183
  end
data/lib/persona/oidc.rb CHANGED
@@ -20,10 +20,12 @@ module Persona
20
20
  # Persona::Oidc::Verifier is the production implementation (code exchange +
21
21
  # JWKS validation); StubProvider is the local stand-in.
22
22
  module Oidc
23
- # The verified outcome of an OIDC round-trip: which provider vouched for
24
- # the browser, and the stable subject the token was issued for. This
25
- # pair becomes an account binding.
26
- Identity = Struct.new(:provider, :subject, keyword_init: true)
23
+ # The verified outcome of an account-linking round-trip: which provider
24
+ # vouched for the browser, and the provider's stable identifier for the
25
+ # account. This pair becomes an account binding. `attributes` are facts the
26
+ # provider stated about the account during the round-trip (P-16): not part
27
+ # of the binding's key, and true only as of the link.
28
+ Identity = Struct.new(:provider, :subject, :attributes, keyword_init: true)
27
29
 
28
30
  # What #authorize returns: where to send the browser, and the per-flow
29
31
  # secrets to stash with the state and hand back to #verify on the
@@ -1,3 +1,3 @@
1
1
  module Persona
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/persona.rb CHANGED
@@ -4,6 +4,7 @@ require_relative 'persona/version'
4
4
  require_relative 'persona/claim_code'
5
5
  require_relative 'persona/oidc'
6
6
  require_relative 'persona/oidc/verifier'
7
+ require_relative 'persona/oidc/oauth2_verifier'
7
8
  require_relative 'persona/oidc/link_store'
8
9
  require_relative 'persona/account_link'
9
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: persona-protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - UNIBA COMMONS Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-07-07 00:00:00.000000000 Z
11
+ date: 2026-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -40,6 +40,7 @@ files:
40
40
  - lib/persona/claim_code.rb
41
41
  - lib/persona/oidc.rb
42
42
  - lib/persona/oidc/link_store.rb
43
+ - lib/persona/oidc/oauth2_verifier.rb
43
44
  - lib/persona/oidc/verifier.rb
44
45
  - lib/persona/version.rb
45
46
  homepage: https://github.com/uniba-commons/persona-protocol