token_authority 0.2.1 → 0.3.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.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +42 -1
  3. data/README.md +52 -14
  4. data/app/controllers/concerns/token_authority/initial_access_token_authentication.rb +2 -2
  5. data/app/controllers/token_authority/protected_resource_metadata_controller.rb +39 -0
  6. data/app/helpers/token_authority/authorization_grants_helper.rb +2 -3
  7. data/app/models/concerns/token_authority/claim_validatable.rb +2 -2
  8. data/app/models/concerns/token_authority/resourceable.rb +2 -2
  9. data/app/models/token_authority/access_token.rb +2 -2
  10. data/app/models/token_authority/access_token_request.rb +1 -1
  11. data/app/models/token_authority/authorization_request.rb +2 -2
  12. data/app/models/token_authority/authorization_server_metadata.rb +4 -4
  13. data/app/models/token_authority/client.rb +4 -4
  14. data/app/models/token_authority/client_metadata_document.rb +2 -2
  15. data/app/models/token_authority/client_registration_request.rb +5 -5
  16. data/app/models/token_authority/jwks_fetcher.rb +1 -1
  17. data/app/models/token_authority/protected_resource_metadata.rb +110 -31
  18. data/app/models/token_authority/refresh_token.rb +2 -2
  19. data/app/models/token_authority/refresh_token_request.rb +1 -1
  20. data/lib/generators/token_authority/install/templates/token_authority.rb +100 -114
  21. data/lib/token_authority/configuration.rb +345 -175
  22. data/lib/token_authority/errors.rb +29 -0
  23. data/lib/token_authority/routing/constraints.rb +2 -2
  24. data/lib/token_authority/routing/routes.rb +74 -16
  25. data/lib/token_authority/version.rb +1 -1
  26. data/lib/token_authority.rb +2 -2
  27. metadata +2 -2
  28. data/app/controllers/token_authority/resource_metadata_controller.rb +0 -12
@@ -58,7 +58,7 @@ module TokenAuthority
58
58
  return if resources.empty?
59
59
 
60
60
  # If resources are provided but feature is disabled, reject them
61
- unless TokenAuthority.config.rfc_8707_enabled?
61
+ unless TokenAuthority.config.resources_enabled?
62
62
  errors.add(:resources, :not_allowed)
63
63
  return
64
64
  end
@@ -10,25 +10,19 @@ TokenAuthority.configure do |config|
10
10
  # secret_key_base from credentials or configuration.
11
11
  config.secret_key = Rails.application.credentials.secret_key_base || Rails.application.secret_key_base
12
12
 
13
- # ==========================================================================
14
- # JWT Access Tokens (RFC 9068)
15
- # ==========================================================================
16
-
17
- # The audience URL for JWT tokens. This is typically your API's base URL.
18
- # Used as the "aud" (audience) claim in issued tokens.
19
- config.rfc_9068_audience_url = ENV.fetch("TOKEN_AUTHORITY_AUDIENCE_URL", "http://localhost:3000/api/")
20
-
21
- # The issuer URL for JWT tokens. This is typically your application's base URL.
22
- # Used as the "iss" (issuer) claim in issued tokens.
23
- config.rfc_9068_issuer_url = ENV.fetch("TOKEN_AUTHORITY_ISSUER_URL", "http://localhost:3000/")
13
+ # Enable event logging (default: true).
14
+ # When enabled, events are emitted and logged to Rails.logger.
15
+ # Events include: authorization requests, consent actions, token exchanges,
16
+ # token refreshes, revocations, client authentication, and security events.
17
+ # config.event_logging_enabled = true
24
18
 
25
- # Default duration for access tokens in seconds (5 minutes).
26
- # This value is used when creating new clients without explicit durations.
27
- # config.rfc_9068_default_access_token_duration = 300
19
+ # Enable debug events (default: false).
20
+ # Debug events provide detailed information useful during development,
21
+ # such as PKCE validation steps and cache hits/misses.
22
+ # config.event_logging_debug_events = false
28
23
 
29
- # Default duration for refresh tokens in seconds (14 days).
30
- # This value is used when creating new clients without explicit durations.
31
- # config.rfc_9068_default_refresh_token_duration = 1_209_600
24
+ # Enable ActiveSupport::Notifications instrumentation (default: true).
25
+ # config.instrumentation_enabled = true
32
26
 
33
27
  # ==========================================================================
34
28
  # User Authentication
@@ -62,131 +56,143 @@ TokenAuthority.configure do |config|
62
56
  config.error_page_layout = "application"
63
57
 
64
58
  # ==========================================================================
65
- # Server Metadata (RFC 8414)
59
+ # Scopes
66
60
  # ==========================================================================
67
61
 
68
- # URL to developer documentation for your OAuth server.
69
- # Included in the /.well-known/oauth-authorization-server response.
70
- # config.rfc_8414_service_documentation = "https://example.com/docs/oauth"
62
+ # Configure allowed scopes with human-friendly display names.
63
+ # Keys are the scope strings (used as the allowlist), values are display names
64
+ # shown on the consent screen.
65
+ #
66
+ # IMPORTANT: You must configure at least one scope since require_scope is true by default.
67
+ config.scopes = {
68
+ "read" => "Read access to your data",
69
+ "write" => "Write access to your data"
70
+ }
71
71
 
72
- # Note: scopes_supported in the metadata response is automatically derived
73
- # from the keys of config.scopes (see Scopes section below).
72
+ # Require the scope parameter in authorization requests (default: true).
73
+ # When true, clients must specify at least one scope.
74
+ # config.require_scope = true
74
75
 
75
76
  # ==========================================================================
76
- # Protected Resource Metadata (RFC 9728)
77
+ # Resources (RFC 9728 / RFC 8707)
77
78
  # ==========================================================================
78
79
 
79
- # The protected resource's identifier URL.
80
- # Defaults to rfc_9068_issuer_url if not set.
81
- # config.rfc_9728_resource = "https://api.example.com/"
82
-
83
- # Scopes accepted by the protected resource.
84
- # Falls back to config.scopes keys if not set.
85
- # config.rfc_9728_scopes_supported = ["api:read", "api:write"]
86
-
87
- # List of authorization server issuer URLs that can issue tokens for this resource.
88
- # Defaults to the local authorization server (rfc_9068_issuer_url) if not set.
89
- # config.rfc_9728_authorization_servers = ["https://auth.example.com"]
90
-
91
- # Token presentation methods supported by the resource (e.g., "header", "body", "query").
92
- # config.rfc_9728_bearer_methods_supported = ["header"]
93
-
94
- # URL to the resource's JSON Web Key Set (JWKS).
95
- # config.rfc_9728_jwks_uri = "https://api.example.com/.well-known/jwks.json"
96
-
97
- # Human-readable name for the protected resource.
98
- # config.rfc_9728_resource_name = "Example API"
99
-
100
- # URL to developer documentation for the protected resource.
101
- # config.rfc_9728_resource_documentation = "https://example.com/docs/api"
102
-
103
- # URL to the resource's privacy policy.
104
- # config.rfc_9728_resource_policy_uri = "https://example.com/privacy"
105
-
106
- # URL to the resource's terms of service.
107
- # config.rfc_9728_resource_tos_uri = "https://example.com/tos"
80
+ # Protected resources keyed by identifier. For single-resource deployments,
81
+ # just configure one entry - it will be used for all requests.
82
+ # For multi-resource deployments, add entries for each subdomain.
83
+ # The first entry is used as the default when no subdomain matches.
84
+ #
85
+ # Each entry must include the :resource field (required per RFC 9728).
86
+ # The :resource URL is used as the audience (aud) claim in access tokens.
87
+ # The first :authorization_servers entry is used as the issuer (iss) claim.
88
+ #
89
+ # Resource indicators (RFC 8707) are automatically enabled when resources are
90
+ # configured. The allowlist of valid resource URIs is derived from the :resource
91
+ # key in the configuration below.
92
+ #
93
+ # IMPORTANT: You must configure at least one resource since require_resource is true by default.
94
+ #
95
+ # Available options:
96
+ # resource (required) - The protected resource URI (used as aud claim)
97
+ # resource_name - Human-readable name shown on consent screen
98
+ # scopes_supported - Array of scopes this resource accepts
99
+ # authorization_servers - Array of auth server URLs (first used as iss claim)
100
+ # bearer_methods_supported - Array of supported bearer token methods
101
+ # jwks_uri - URI for JSON Web Key Set endpoint
102
+ # resource_documentation - URL for API documentation
103
+ # resource_policy_uri - URL for privacy policy
104
+ # resource_tos_uri - URL for terms of service
105
+ config.resources = {
106
+ api: {
107
+ resource: ENV.fetch("TOKEN_AUTHORITY_RESOURCE_URL", "http://localhost:3000/api"),
108
+ resource_name: "My API",
109
+ scopes_supported: %w[read write],
110
+ authorization_servers: [ENV.fetch("TOKEN_AUTHORITY_BASE_URL", "http://localhost:3000")],
111
+ bearer_methods_supported: ["header"],
112
+ resource_documentation: "http://localhost:3000/docs/api"
113
+ }
114
+ }
115
+
116
+ # Require the resource parameter in authorization requests (default: true).
117
+ # When true, clients must specify at least one resource.
118
+ # config.require_resource = true
108
119
 
109
120
  # ==========================================================================
110
- # Scopes
121
+ # JWT Access Tokens (RFC 9068)
111
122
  # ==========================================================================
112
123
 
113
- # Configure allowed scopes with human-friendly display names.
114
- # Keys are the scope strings (used as the allowlist), values are display names
115
- # shown on the consent screen.
116
- #
117
- # Set to nil or {} to disable scope validation entirely.
118
- # When configured, only these scopes are allowed in authorization requests.
119
- # config.scopes = {
120
- # "read" => "Read access to your data",
121
- # "write" => "Write access to your data"
122
- # }
124
+ # The issuer URL for JWT tokens (default: nil).
125
+ # Used as the "iss" (issuer) claim in issued tokens.
126
+ # If nil, the issuer is derived from the first resource's :authorization_servers.
127
+ # You must configure either this OR :authorization_servers on at least one resource.
128
+ # config.token_issuer_url = nil
123
129
 
124
- # Require the scope parameter in authorization requests.
125
- # When true, clients must specify at least one scope.
126
- # config.require_scope = false
130
+ # The audience URL for JWT tokens (default: nil).
131
+ # When set, this value is used as the "aud" claim for all tokens.
132
+ # When nil (recommended), the audience is the resource URL from config.resources.
133
+ # config.token_audience_url = nil
134
+
135
+ # Default duration for access tokens in seconds (5 minutes).
136
+ # This value is used when creating new clients without explicit durations.
137
+ # config.default_access_token_duration = 300
138
+
139
+ # Default duration for refresh tokens in seconds (14 days).
140
+ # This value is used when creating new clients without explicit durations.
141
+ # config.default_refresh_token_duration = 1_209_600
127
142
 
128
143
  # ==========================================================================
129
- # Resource Indicators (RFC 8707)
144
+ # Server Metadata (RFC 8414)
130
145
  # ==========================================================================
131
146
 
132
- # Configure allowed resources with human-friendly display names.
133
- # Keys are the resource URIs (used as the allowlist), values are display names
134
- # shown on the consent screen.
135
- #
136
- # Set to nil or {} to disable resource indicators entirely.
137
- # When configured, only these resources are allowed in authorization requests.
138
- # config.rfc_8707_resources = {
139
- # "https://api.example.com/" => "Main API",
140
- # "https://billing.example.com/" => "Billing Service"
141
- # }
147
+ # URL to developer documentation for your OAuth server.
148
+ # Included in the /.well-known/oauth-authorization-server response.
149
+ # config.authorization_server_documentation = "https://example.com/docs/oauth"
142
150
 
143
- # Require the resource parameter in authorization requests.
144
- # When true, clients must specify at least one resource.
145
- # config.rfc_8707_require_resource = false
151
+ # Note: scopes_supported in the metadata response is automatically derived
152
+ # from the keys of config.scopes (see Scopes section above).
146
153
 
147
154
  # ==========================================================================
148
155
  # Dynamic Client Registration (RFC 7591)
149
156
  # ==========================================================================
150
157
 
151
- # Enable dynamic client registration endpoint (/register).
158
+ # Enable dynamic client registration endpoint (/register) (default: true).
152
159
  # When enabled, clients can register programmatically via POST requests.
153
- # Disabled by default for security - enable only if you need this feature.
154
- # config.rfc_7591_enabled = false
160
+ # config.dcr_enabled = true
155
161
 
156
- # Require an initial access token for client registration.
162
+ # Require an initial access token for client registration (default: false).
157
163
  # When enabled, registration requests must include a Bearer token.
158
- # config.rfc_7591_require_initial_access_token = false
164
+ # config.dcr_require_initial_access_token = false
159
165
 
160
166
  # Validator proc for initial access tokens.
161
167
  # Must return true if the token is valid, false otherwise.
162
168
  # Example:
163
- # config.rfc_7591_initial_access_token_validator = ->(token) {
169
+ # config.dcr_initial_access_token_validator = ->(token) {
164
170
  # token == ENV["REGISTRATION_ACCESS_TOKEN"]
165
171
  # }
166
172
 
167
173
  # Allowed grant types for dynamically registered clients.
168
- # config.rfc_7591_allowed_grant_types = %w[authorization_code refresh_token]
174
+ # config.dcr_allowed_grant_types = %w[authorization_code refresh_token]
169
175
 
170
176
  # Allowed response types for dynamically registered clients.
171
- # config.rfc_7591_allowed_response_types = %w[code]
177
+ # config.dcr_allowed_response_types = %w[code]
172
178
 
173
179
  # Allowed token endpoint authentication methods.
174
180
  # Options: none, client_secret_basic, client_secret_post, client_secret_jwt, private_key_jwt
175
- # config.rfc_7591_allowed_token_endpoint_auth_methods = %w[none client_secret_basic client_secret_post client_secret_jwt private_key_jwt]
181
+ # config.dcr_allowed_token_endpoint_auth_methods = %w[none client_secret_basic client_secret_post client_secret_jwt private_key_jwt]
176
182
 
177
183
  # Client secret expiration duration in seconds (nil = never expires).
178
- # config.rfc_7591_client_secret_expiration = nil
184
+ # config.dcr_client_secret_expiration = nil
179
185
 
180
186
  # JWKS for verifying software statements (signed JWTs with client metadata).
181
187
  # Set to a JWKS hash to enable software statement verification.
182
- # config.rfc_7591_software_statement_jwks = nil
188
+ # config.dcr_software_statement_jwks = nil
183
189
 
184
190
  # Require software statements for client registration.
185
191
  # When enabled, registration requests must include a valid software_statement.
186
- # config.rfc_7591_software_statement_required = false
192
+ # config.dcr_software_statement_required = false
187
193
 
188
194
  # Cache TTL in seconds for fetched JWKS from jwks_uri (default: 1 hour).
189
- # config.rfc_7591_jwks_cache_ttl = 3600
195
+ # config.dcr_jwks_cache_ttl = 3600
190
196
 
191
197
  # ==========================================================================
192
198
  # Client Metadata Document (draft-ietf-oauth-client-id-metadata-document)
@@ -224,24 +230,4 @@ TokenAuthority.configure do |config|
224
230
  # Connection and read timeouts in seconds (default: 5 each).
225
231
  # config.client_metadata_document_connect_timeout = 5
226
232
  # config.client_metadata_document_read_timeout = 5
227
-
228
- # ==========================================================================
229
- # Event Logging
230
- # ==========================================================================
231
-
232
- # TokenAuthority emits structured events using Rails 8.1's event reporting.
233
- # Events are automatically logged to Rails.logger when enabled.
234
- #
235
- # Events include: authorization requests, consent actions, token exchanges,
236
- # token refreshes, revocations, client authentication, and security events
237
- # (e.g., token theft detection).
238
-
239
- # Enable event logging (default: true).
240
- # When enabled, events are emitted and logged to Rails.logger.
241
- # config.event_logging_enabled = true
242
-
243
- # Enable debug events (default: false).
244
- # Debug events provide detailed information useful during development,
245
- # such as PKCE validation steps and cache hits/misses.
246
- # config.event_logging_debug_events = false
247
233
  end