Class: Aspera::OAuth::Server::Pkce
- Inherits:
-
Object
- Object
- Aspera::OAuth::Server::Pkce
- Includes:
- ServletHelpers
- Defined in:
- lib/aspera/oauth/server/pkce.rb
Overview
Minimal OAuth 2.0 authorization server supporting:
- Dynamic Client Registration (RFC 7591)
- Authorization Code + PKCE (S256) flow (RFC 7636)
- Refresh Token rotation
- RFC 8414 authorization server metadata discovery
Designed to be embedded inside a WEBrick servlet. The caller is responsible for:
1. Creating an instance (once, before the server starts).
2. Routing the five OAuth paths to the handle_* methods.
3. Calling access_token_valid?(bearer_token) to gate every other request.
All in-memory stores are protected by an internal Mutex.
Class Method Summary collapse
Instance Method Summary collapse
-
#access_token_valid?(access_token) ⇒ Boolean
True if it is an access token issued by this server, not expired.
-
#handle_authorize(req, res) ⇒ Object
GET or POST /oauth/authorize — dispatch based on request method.
-
#handle_discovery(_req, res) ⇒ Object
GET /.well-known/oauth-authorization-server.
-
#handle_register(req, res) ⇒ Object
POST /oauth/register.
-
#handle_token(req, res) ⇒ Object
POST /oauth/token.
-
#initialize(base_url:, access_token_ttl: ACCESS_TOKEN_TTL, refresh_token_ttl: REFRESH_TOKEN_TTL, title: 'Authorization request', description: 'this server', consent_secret: nil, authorize_html_cb: nil) ⇒ Pkce
constructor
A new instance of Pkce.
-
#metadata ⇒ Hash
Metadata hash (RFC 8414).
-
#www_authenticate_header ⇒ String
Value for the WWW-Authenticate header.
Methods included from ServletHelpers
#authorization, #basic_authorized?, #check_request, host_name, #json_error, #json_response, origin, same_origin?
Constructor Details
#initialize(base_url:, access_token_ttl: ACCESS_TOKEN_TTL, refresh_token_ttl: REFRESH_TOKEN_TTL, title: 'Authorization request', description: 'this server', consent_secret: nil, authorize_html_cb: nil) ⇒ Pkce
Returns a new instance of Pkce.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/aspera/oauth/server/pkce.rb', line 75 def initialize(base_url:, access_token_ttl: ACCESS_TOKEN_TTL, refresh_token_ttl: REFRESH_TOKEN_TTL, title: 'Authorization request', description: 'this server', consent_secret: nil, authorize_html_cb: nil) require 'openssl' Aspera.assert(.nil? || !.to_s.empty?) { 'consent_secret must not be empty' } @base_url = base_url @access_token_ttl = access_token_ttl @refresh_token_ttl = refresh_token_ttl @title = title @description = description @consent_secret = &.to_s @authorize_html_cb = @store_mutex = Mutex.new # dynamically registered clients: client_id => redirect_uris, least recently used first @clients = {} # pending authorization codes: code => {client_id:, redirect_uri:, code_challenge:, expires_at:} @pending_codes = {} # valid access tokens: token => expires_at (Time) @access_tokens = {} # refresh tokens: token => {access_token:, expires_at:} @refresh_tokens = {} end |
Class Method Details
.default_access_token_ttl ⇒ Object
61 |
# File 'lib/aspera/oauth/server/pkce.rb', line 61 def default_access_token_ttl = ACCESS_TOKEN_TTL |
.default_refresh_token_ttl ⇒ Object
62 |
# File 'lib/aspera/oauth/server/pkce.rb', line 62 def default_refresh_token_ttl = REFRESH_TOKEN_TTL |
Instance Method Details
#access_token_valid?(access_token) ⇒ Boolean
Returns true if it is an access token issued by this server, not expired.
104 105 106 107 108 109 110 |
# File 'lib/aspera/oauth/server/pkce.rb', line 104 def access_token_valid?(access_token) return false if access_token.nil? @store_mutex.synchronize do expire_at = @access_tokens[access_token] !expire_at.nil? && expire_at > Time.now end end |
#handle_authorize(req, res) ⇒ Object
GET or POST /oauth/authorize — dispatch based on request method.
175 176 177 178 179 180 181 182 183 |
# File 'lib/aspera/oauth/server/pkce.rb', line 175 def (req, res) if req.request_method == 'GET' (req, res) elsif req.request_method == 'POST' (req, res) else json_error(res, 405, 'method_not_allowed') end end |
#handle_discovery(_req, res) ⇒ Object
GET /.well-known/oauth-authorization-server
132 133 134 |
# File 'lib/aspera/oauth/server/pkce.rb', line 132 def handle_discovery(_req, res) json_response(res, 200, ) end |
#handle_register(req, res) ⇒ Object
POST /oauth/register
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/aspera/oauth/server/pkce.rb', line 141 def handle_register(req, res) body = req.body.to_s.empty? ? {} : (JSON.parse(req.body.to_s) rescue {}) body = {} unless body.is_a?(Hash) redirect_uris = body['redirect_uris'] error = redirect_uris_error(redirect_uris) unless error.nil? json_error(res, 400, 'invalid_redirect_uri', error) return end client_id = SecureRandom.urlsafe_base64(16) @store_mutex.synchronize do while @clients.size >= MAX_CLIENTS removed_id, = @clients.shift Log.log.info { "OAuth client removed, limit of #{MAX_CLIENTS} clients reached: client_id=#{removed_id}" } end @clients[client_id] = redirect_uris end Log.log.info { "OAuth dynamic registration: client_id=#{client_id} name=#{body['client_name'].to_s[0, 64].inspect}" } registration = { client_id: client_id, client_id_issued_at: Time.now.to_i, grant_types: %w[authorization_code refresh_token], response_types: ['code'], token_endpoint_auth_method: 'none', redirect_uris: redirect_uris } json_response(res, 201, registration, headers: {'Cache-Control' => 'no-store'}) end |
#handle_token(req, res) ⇒ Object
POST /oauth/token
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/aspera/oauth/server/pkce.rb', line 190 def handle_token(req, res) require 'webrick' params = WEBrick::HTTPUtils.parse_query(req.body.to_s) grant_type = params['grant_type'] case grant_type when 'authorization_code' code = params['code'] code_verifier = params['code_verifier'] stored = @store_mutex.synchronize { @pending_codes.delete(code) } if stored.nil? || stored[:expires_at] < Time.now json_error(res, 400, 'invalid_grant', 'Authorization code expired or unknown') return end # RFC 6749 §4.1.3: the code must have been issued to this client (client_id is optional with PKCE) unless params['client_id'].nil? || params['client_id'] == stored[:client_id] json_error(res, 400, 'invalid_grant', 'client_id mismatch') return end # RFC 6749 §4.1.3: redirect_uri must match the one used in the authorization request unless params['redirect_uri'] == stored[:redirect_uri] json_error(res, 400, 'invalid_grant', 'redirect_uri mismatch') return end unless pkce_valid?(code_verifier.to_s, stored[:code_challenge]) json_error(res, 400, 'invalid_grant', 'PKCE verification failed') return end token_response(res) when 'refresh_token' rt_value = params['refresh_token'] stored = @store_mutex.synchronize do # Single use: removed even if expired entry = @refresh_tokens.delete(rt_value) next nil if entry.nil? || entry[:expires_at] < Time.now # Rotate: revoke old access token and issue fresh pair @access_tokens.delete(entry[:access_token]) entry end if stored.nil? json_error(res, 400, 'invalid_grant', 'Refresh token expired or unknown') return end token_response(res) else json_error(res, 400, 'unsupported_grant_type', "Unsupported grant_type: #{grant_type}") end end |
#metadata ⇒ Hash
Metadata hash (RFC 8414).
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/aspera/oauth/server/pkce.rb', line 118 def { issuer: @base_url, authorization_endpoint: "#{@base_url}/oauth/authorize", token_endpoint: "#{@base_url}/oauth/token", registration_endpoint: "#{@base_url}/oauth/register", response_types_supported: ['code'], grant_types_supported: %w[authorization_code refresh_token], code_challenge_methods_supported: ['S256'], token_endpoint_auth_methods_supported: ['none'] } end |
#www_authenticate_header ⇒ String
Returns value for the WWW-Authenticate header.
243 244 245 246 |
# File 'lib/aspera/oauth/server/pkce.rb', line 243 def www_authenticate_header "Bearer realm=\"#{@base_url}\", " \ "resource_metadata=\"#{@base_url}/.well-known/oauth-authorization-server\"" end |