angel3_auth_server 10.0.1
angel3_auth_server: ^10.0.1 copied to clipboard
A class containing handlers that can be used within Angel3 to build a spec-compliant OAuth 2.0 server.
Angel3 Auth Server #
Build a spec-compliant OAuth 2.0 authorization server with Angel3. You subclass AuthorizationServer, fill in the parts that touch your data (clients, users, codes and tokens), and route its endpoints. The server handles the protocol: parameter checks, PKCE, client authentication, error responses and headers.
It covers the authorization code grant with PKCE, refresh tokens, the password and client credentials grants, the device authorization grant, extension grants, protected resources, token revocation and introspection, server metadata, and dynamic client registration. angel3_oidc_server builds OpenID Connect on top of it.
- Angel3 Auth Server
Installation #
In your pubspec.yaml:
dependencies:
angel3_framework: ^10.1.0
angel3_auth_server: ^10.0.0
Usage #
The server is generic over two types of your own:
Client: a third-party application that asks to access a user's account.User: a user who is signed in to your application.
Clients #
Subclass AuthorizationServer and implement findClient and verifyClient. findClient looks a client up by its client_id. verifyClient checks its client_secret.
import 'dart:async';
import 'package:angel3_auth_server/angel3_auth_server.dart' as oauth2;
class MyServer extends oauth2.AuthorizationServer<Client, User> {
@override
FutureOr<Client>? findClient(String? clientId) => findClientSomehow(clientId);
@override
FutureOr<bool> verifyClient(Client client, String? clientSecret) =>
verifySecretSomehow(client, clientSecret);
}
A public client, such as a mobile app, has no secret. verifyClient is then given null, and also when the secret is empty. Return true for null only for clients that are allowed to have no secret:
class _Server extends AuthorizationServer<PseudoApplication, Map> {
final Uuid _uuid = Uuid();
@override
FutureOr<PseudoApplication>? findClient(String? clientId) {
return clientId == pseudoApplication.id ? pseudoApplication : null;
}
@override
Future<bool> verifyClient(
PseudoApplication client, String? clientSecret) async {
// A public client, stored without a secret, gives none: `null`.
if (client.secret.isEmpty) return clientSecret == null;
return client.secret == clientSecret;
}
}
See Client Authentication for how the token endpoint reads the client's credentials.
The authorization endpoint #
When a user arrives at the authorization endpoint, the server checks the request and calls requestAuthorizationCode. This is usually where you show a consent dialog. implicit is true when the client asks for an implicit grant, which only happens if allowsImplicitGrant allows it (see Implicit grant).
@override
Future<void> requestAuthorizationCode(
PseudoApplication client,
String? redirectUri,
Iterable<String> scopes,
String state,
RequestContext req,
ResponseContext res,
bool implicit) async {
await res.render('dialog');
}
Once the user grants access:
- Issue a code.
- Store it with the client, the
redirect_uri, the scopes and the PKCE challenge (see PKCE). - Redirect the user with
completeAuthorizationCode. It adds thecodeand thestateto the redirect URI and keeps the URI's own query (RFC 6749 §4.1.2).
await res.redirect(
completeAuthorizationCode(code, Uri.parse(redirectUri!), state: state),
);
For an implicit grant, completeImplicitGrant does the same with a token, which goes in the fragment.
The token endpoint #
The client then exchanges the code for an access token, and optionally a refresh token, in exchangeAuthorizationCodeForToken. Two things are up to you:
- The redirect URI.
redirectUriisnullif the token request gives none. If the authorization request gave one, the token request must give the same one (RFC 6749 §4.1.3), so compare it with the one stored with the code. - The PKCE verifier. The server only checks that the request gives a
code_verifier. Compare it with the challenge stored with the code, or PKCE protects nothing. Also refuse a verifier for a code that was issued without a challenge, because the challenge may have been stripped from the authorization request (RFC 9700 §4.8.2).
@override
Future<AuthorizationTokenResponse> exchangeAuthorizationCodeForToken(
PseudoApplication? client,
String? authCode,
String? redirectUri,
RequestContext req,
ResponseContext res) async {
var stored = await findCodeSomehow(authCode);
// Stored for every code, unless `requiresPkce` exempts its client and it
// sent no challenge.
if (stored.codeChallenge != null) {
Pkce(stored.codeChallengeMethod, stored.codeChallenge)
.validate(await getPkceCodeVerifier(req));
} else if (req.bodyAsMap['code_verifier'] != null) {
// A verifier for a code without a challenge: the challenge may have
// been stripped from its request (RFC 9700 §4.8.2).
throw AuthorizationException(
ErrorResponse(
ErrorResponse.invalidGrant,
'The code was not issued with a "code_challenge".',
'',
),
);
}
return AuthorizationTokenResponse('foo', refreshToken: 'bar');
}
Routes #
Route the two endpoints:
void pseudoCode() {
app.group('/oauth2', (router) {
router
..get('/authorize', server.authorizationEndpoint)
..post('/authorize', server.authorizationEndpoint)
..post('/token', server.tokenEndpoint);
});
}
- The
authorizationEndpointreads its parameters from the query, or from the form of a POST (RFC 6749 §3.1). - Between them,
authorizationEndpointandtokenEndpointhandle every grant type. - Responses from the
tokenEndpointhaveCache-Control: no-storeandPragma: no-cache. A token'sscopeis a single string of space-separated scopes (RFC 6749 §5.1).
Client Authentication #
At the token endpoint, a client authenticates in one of two ways (RFC 6749 §2.3.1):
- with an
Authorization: Basicheader; - without that header, with
client_idandclient_secretin the request's form. AnAuthorizationheader with another scheme is ignored.
The rules:
- A public client, such as a mobile app using PKCE, sends only its
client_id.verifyClientis given anullsecret. - A client that fails authentication gets
invalid_client, with status 401. - If the request has the header and also a
client_idorclient_secretin the form, those must match the header. - A client without a secret may use the authorization code, password and refresh token grants. It may not use the client credentials grant, which is only for confidential clients. It gets
unauthorized_clientinstead.
The device authorization, revocation and introspection endpoints authenticate clients the same way.
Redirect URIs #
By default, the server does not know your clients' redirect URIs. So an authorization request must give a redirect_uri, and requestAuthorizationCode must check it. Errors are thrown rather than redirected, because the URI might belong to an attacker.
To have the server check the URI instead, implement registeredRedirectUri:
- If the request gives a
redirect_urithat is registered for the client, return it. - If the request gives none, return the client's registered URI.
- If the URI is not registered, throw an
AuthorizationException. This error is not redirected.
@override
Uri? registeredRedirectUri(PseudoApplication client, String? redirectUri) {
var registered = Uri.parse(client.redirectUri);
if (redirectUri == null || redirectUri == client.redirectUri) {
return registered;
}
throw AuthorizationException(
ErrorResponse(
ErrorResponse.invalidRequest,
'The "redirect_uri" is not registered.',
'',
),
);
}
Once registeredRedirectUri returns a URI:
- the request may leave out its
redirect_uri; - errors are redirected to the URI. This includes an
access_deniedthrown byrequestAuthorizationCodeand an unsupportedresponse_type. Any other error is logged and redirected asserver_error. - The redirect carries
error,error_descriptionand the request'sstate. They go in the query, or in the fragment for an implicit grant (RFC 6749 §4.1.2.1, §4.2.2.1).
Repeated parameters #
A request that gives the same parameter more than once in its query or its form, such as two redirect_uris, gets invalid_request (RFC 6749 §3.1). An authorization request refused for this reason is never redirected. Without this check, the server would use the last value while a proxy or filter in front of it might read the first.
- Forms are checked with
RequestContext.bodyFieldsAll, which is whyangel3_framework10.1.0 is needed. requireTokenrefuses only a repeatedaccess_token, because the rest of a protected endpoint's form belongs to your app.- A JSON body can't be checked, because the JSON parser keeps only the last value of a repeated key.
Other Grants #
By default, every grant method throws a 400 Bad Request. At the token endpoint the error is unsupported_grant_type; for an authorization request it is unsupported_response_type. To support a grant, implement its method. Apart from the authorization code grant described above, these are available:
refreshAuthorizationTokenresourceOwnerPasswordCredentialsGrantclientCredentialsGrant, which is given the request'sscoperequestDeviceCodeandexchangeDeviceCodeForToken, for the device authorization grant
The OAuth 2.0 specification describes each grant type in depth.
Implicit grant #
An implicit grant (response_type=token) is refused by default with unsupported_response_type. OAuth 2.1 removes it, and RFC 9700 advises against it. Its access token is sent in the fragment of the redirect URI, where it can leak, for example through the browser history. Clients should use the authorization code grant with PKCE instead.
For clients that still need it:
- Override
allowsImplicitGrant. The grant then goes throughrequestAuthorizationCodewithimplicitset totrue. - List
tokenin theresponseTypesof yourServerMetadata.
@override
bool allowsImplicitGrant(PseudoApplication client) => client.id == 'legacy-spa';
Extension grants #
extensionGrants maps other grant types to handlers. A handler runs only after the client has been authenticated, as for the other grants, by Authorization: Basic or by client_id and client_secret in the form. This means a grant that authenticates the client another way, such as a client_assertion (RFC 7523), is refused before the handler runs.
A handler is given the request and the response, but not the client. Call findClient if you need it.
A key that names a grant the server implements itself, such as password, is never called; a warning is logged once. Override that grant's method instead.
Device Authorization #
A device without a browser, such as a TV or a command line tool, asks the deviceAuthorizationEndpoint for a device code (RFC 8628). Route a POST to it:
router.post('/device', server.deviceAuthorizationEndpoint);
The flow:
- The endpoint authenticates the client as the
tokenEndpointdoes. A device is usually a public client, so it gives only itsclient_id, andverifyClientis given anullsecret. - It returns the
DeviceCodeResponsefromrequestDeviceCode, which is given the request'sscope. The device shows the user itsuserCodeand theverificationUri, where the user authorizes the device from another device. - The device polls the
tokenEndpointwithgrant_type=urn:ietf:params:oauth:grant-type:device_codeand itsdevice_code. exchangeDeviceCodeForTokenanswers with a token once the user has authorized the device. Until then, it throwsauthorization_pending,slow_down,access_deniedorexpired_token(ErrorResponse.authorizationPendingand so on).
If requestDeviceCode is not implemented, the endpoint answers unsupported_grant_type.
Earlier versions accepted a device code request at the tokenEndpoint without a grant_type. That request, like any token request without a grant_type, is now invalid_request. Route a deviceAuthorizationEndpoint and point device clients at it.
PKCE #
By default, an authorization code grant must use PKCE with the S256 method, as OAuth 2.1 and RFC 9700 require. This means an intercepted authorization code cannot be redeemed.
- The authorization request must give a
code_challengeof 43 characters (the base64url of a SHA-256 hash) andcode_challenge_method=S256. Otherwise it getsinvalid_request. A request without a method meansplain, which is refused. - The token request must give a
code_verifierof 43 to 128 characters. Otherwise it getsinvalid_request.
package:oauth2 and other standard clients already do this. A public client has no secret, so it sends its client_id in the form of its token request, and verifyClient is given null.
The server only checks that PKCE is present. You compare the verifier with the stored challenge in exchangeAuthorizationCodeForToken, as below. For clients that can't use PKCE, or can't use S256 yet, relax the rules:
@override
bool requiresPkce(PseudoApplication client) => client.id != 'legacy-app';
@override
bool get allowsPlainPkce => true;
For a code request, the authorizationEndpoint puts a Pkce, built from the request's code_challenge and code_challenge_method, in the request's container:
- An implicit request has no PKCE.
- If the request has no challenge, because
requiresPkceexempts the client, making thePkcethrowsinvalid_request.
The tokenEndpoint puts no Pkce in the container, because its request has a code_verifier but no challenge. Build a Pkce from the stored challenge instead:
@override
Future<void> requestAuthorizationCode(
PseudoApplication client,
String? redirectUri,
Iterable<String> scopes,
String state,
RequestContext req,
ResponseContext res,
bool implicit) async {
// An implicit grant has no PKCE.
if (implicit) return handleImplicitGrantSomehow(client, redirectUri, state);
// Checked by the server, for a code request. A client that `requiresPkce`
// exempts may send no challenge, and then this throws.
var pkce = req.container!.make<Pkce>();
// Store the challenge and its method, so that the token request can be
// checked against them later.
await storeChallengeSomehow(client, pkce.codeChallenge, pkce.codeChallengeMethod);
}
@override
Future<AuthorizationTokenResponse> exchangeAuthorizationCodeForToken(
PseudoApplication? client,
String? authCode,
String? redirectUri,
RequestContext req,
ResponseContext res) async {
// The `code_verifier` of the request. If there is none, this throws an
// OAuth2 error.
var codeVerifier = await getPkceCodeVerifier(req);
// The challenge and method stored with the code.
var codeChallenge = await getTheChallenge();
var codeChallengeMethod = await getTheChallengeMethod();
// `validate` throws an OAuth2 error if the verifier doesn't match.
Pkce(codeChallengeMethod, codeChallenge).validate(codeVerifier);
// The verifier is valid, so issue the token as usual.
return AuthorizationTokenResponse('...');
}
Protecting Resources #
Once a client has an access token, it sends the token to your API in an Authorization: Bearer header (RFC 6750). requireToken protects the handlers that come after it.
Implement findAccessToken to look up the tokens your grant methods issue, with their client, user, scopes and expiry. Return null for a token that is unknown or revoked.
@override
Future<AccessToken<PseudoApplication, PseudoUser>?> findAccessToken(
String token, RequestContext req) async {
var stored = await lookUpTokenSomehow(token);
if (stored == null) return null;
return AccessToken(
client: stored.client,
user: stored.user, // `null` for a client credentials grant
scopes: stored.scopes,
expiresAt: stored.expiresAt,
);
}
The handlers after requireToken find the AccessToken in the request's container:
app.get('/me', chain([
server.requireToken(scopes: ['profile']),
(req, res) {
var token =
req.container!.make<AccessToken<PseudoApplication, PseudoUser>>();
return token.user;
},
]));
A refused request gets a WWW-Authenticate: Bearer realm="oauth2" challenge. The realm can be changed with realm.
| Request | Response |
|---|---|
| No token | 401, with no error, because the client did not try to authenticate |
| An unknown, revoked or expired token | invalid_token, 401 |
A token that doesn't grant the scopes |
insufficient_scope, 403, with the required scopes |
| A malformed token, or more than one | invalid_request, 400 |
Where the token is read from:
- By default, only from the header.
- With
allowFormParameter: true, also from theaccess_tokenfield of a URL-encoded form, on any request that is not a GET. - Never from the query, where it may be logged. A token in the query is ignored, and a warning is logged.
Several requireTokens can check one request, for example one on a group and one on a route, each for its own scopes.
Revoking Tokens #
A client revokes an access or refresh token, for example when its user logs out, at the revocationEndpoint (RFC 7009). Route a POST to it and implement revokeToken:
router.post('/revoke', server.revocationEndpoint);
@override
Future<void> revokeToken(
PseudoApplication client,
String token,
String? tokenTypeHint, // `access_token`, `refresh_token`, or `null`
RequestContext req,
ResponseContext res) async {
var stored = await lookUpTokenSomehow(token, tokenTypeHint);
// Only a token of this client; nothing for an unknown one.
if (stored != null && stored.clientId == client.id) {
await revokeTokenAndItsGrantSomehow(stored);
}
}
How it behaves:
- It authenticates the client as the
tokenEndpointdoes. A public client gives only itsclient_id. - It answers 200 with no body, whether or not the token was revoked, so a caller learns nothing about tokens that aren't its own.
- A request without a
tokengetsinvalid_request.
In revokeToken:
- Look the token up by its
token_type_hintfirst. If it isn't found, try the other type. - Also revoke the other tokens of the same grant, such as the access tokens issued from a refresh token.
- Throw
unsupported_token_type(ErrorResponse.unsupportedTokenType) for a kind of token the server cannot revoke. IfrevokeTokenis not implemented, every request gets this error.
Introspecting Tokens #
A resource server that runs separately from the authorization server, and receives opaque tokens, asks the introspectionEndpoint whether a token is active and what it grants (RFC 7662). Route a POST to it:
router.post('/introspect', server.introspectionEndpoint);
Who may call it:
- The caller authenticates as a client, as at the
tokenEndpoint, but it must give a secret, since anyone can give a public client's id; without one it getsinvalid_client. Register each resource server as a confidential client. - A client may introspect only if
mayIntrospectreturnstruefor it. This stops a client that isn't a resource server, such as a web app, from learning about other clients' tokens. A client that may not introspect getsaccess_denied, with status 403.
/// The ids of the clients that are resource servers.
const resourceServers = {'orders-api', 'billing-api'};
@override
bool mayIntrospect(PseudoApplication caller, RequestContext req) =>
resourceServers.contains(caller.id);
What it answers:
- It looks the
tokenup withintrospectToken, which callsfindAccessTokenby default. Override it to find refresh tokens as well, using thetoken_type_hint. - A token that is unknown, revoked or expired gives
{"active": false}and nothing else. - An active token gives
active, itsscope, and itsexpandiat(from theexpiresAtandissuedAtof itsAccessToken), plus whateverintrospectionClaimsadds. IfintrospectionClaimsreturnsnull, the token is reported as inactive to that caller.
@override
Map<String, dynamic>? introspectionClaims(
PseudoApplication caller, AccessToken<PseudoApplication, PseudoUser> token) {
// A token that is not meant for the caller is inactive to it.
if (!isAudienceSomehow(caller, token)) return null;
return {
'client_id': token.client?.id,
'username': ?token.user?.username,
'token_type': 'Bearer',
};
}
Server Metadata #
Clients and SDKs discover the server's endpoints, and what it supports, from its metadata at /.well-known/oauth-authorization-server (RFC 8414). Give ServerMetadata the URLs where you route the endpoints and the grants you implement, then route metadataEndpoint:
var issuer = Uri.parse('https://example.com');
var metadata = ServerMetadata(
issuer: issuer,
authorizationEndpoint: issuer.resolve('/oauth2/authorize'),
tokenEndpoint: issuer.resolve('/oauth2/token'),
deviceAuthorizationEndpoint: issuer.resolve('/oauth2/device'),
revocationEndpoint: issuer.resolve('/oauth2/revoke'),
introspectionEndpoint: issuer.resolve('/oauth2/introspect'),
registrationEndpoint: issuer.resolve('/oauth2/register'),
grantTypes: ['authorization_code', 'refresh_token'],
// Add `none` if the server has public clients.
clientAuthMethods: ['client_secret_basic', 'client_secret_post', 'none'],
scopes: ['read', 'write'],
);
app.get(metadata.wellKnownPath, server.metadataEndpoint(metadata));
- What the server adds. It adds what it knows about itself: the device code grant if a
deviceAuthorizationEndpointis given, itsextensionGrants, and its PKCE methods (S256, plusplainifallowsPlainPkce). extra. It adds other metadata. It is applied last, so it can replace any value.- The
issuer. It must be anhttpsURL, or anhttpURL onlocalhost, with no query or fragment. Otherwise the constructor throws anArgumentError. - An issuer with a path. This happens with one of several servers on a host.
wellKnownPaththen puts the path after the well-known path, as in/.well-known/oauth-authorization-server/tenant1. metadataOf. It gives the metadata as aMap, for example to sign it or to serve it some other way.ServerMetadata.copyWith. See Extending.
Client Registration #
Clients that aren't known in advance can register themselves at the registrationEndpoint (RFC 7591). Each one gets a client id, plus a secret unless it is public. The endpoint checks the client's metadata against the server's ServerMetadata, which also supplies the endpoint's URL in the server metadata:
router.post('/register', server.registrationEndpoint(metadata));
Implement two methods:
mayRegisterClient. No client may register unless it accepts the initial access token from the request'sAuthorization: Bearerheader. You give this token to the client's developer. For open registration, returntruefor any token, or for none. Anyone can then register, so limit how often.registerClient. It stores the client, so thatfindClient,verifyClientandregisteredRedirectUrican find it.
@override
bool mayRegisterClient(String? initialAccessToken, RequestContext req) =>
initialAccessToken != null && isInitialAccessTokenSomehow(initialAccessToken);
@override
Future<void> registerClient(
ClientRegistration registration, RequestContext req) async {
await storeClientSomehow(
id: registration.clientId,
// `null` for a public client. Store it hashed, like a password.
secretHash: hashSomehow(registration.clientSecret),
redirectUris: registration.redirectUris,
metadata: registration.metadata,
);
}
registerClient may also:
- throw
invalid_client_metadataorinvalid_redirect_uriitself; - change
registration.metadatato register other values, such as a differentclient_name.
Don't change a value the server checks, such as a grant type, in metadata; refuse it instead. The response gives those values from the registration's own fields (grantTypes and so on), which are what is stored.
The response is a 201 containing:
- the metadata;
- the
client_id; - the
client_secret, which does not expire; - when they were issued.
Metadata checks #
The metadata is checked before registerClient is called. A refusal is a 400 with invalid_client_metadata or invalid_redirect_uri.
-
Supported values.
grant_types,response_types,token_endpoint_auth_methodandscopemust be among the server's own, which are:grantTypes, plus the device code and extension grants;responseTypes;clientAuthMethods;scopes.
A server that supports the
tokenresponse type also supports theimplicitgrant type. -
Defaults.
grant_types:authorization_code. With thetokenresponse type it isimplicit, plusauthorization_codeifcodeis also given.response_types: the ones the grant types need.token_endpoint_auth_method:client_secret_basic.scope: none.
-
Grant types and response types must match.
authorization_codegoes withcode, andimplicitwithtoken(RFC 7591 §2.1). A public client (none) may not useclient_credentials. -
Redirect URIs. A client with a response type must register
redirect_uris. Each must be absolute, with no fragment, and one of:https;http, only onlocalhost;- a native app's private-use scheme, which is a reverse domain name such as
com.example.app:/cb(RFC 8252 §7.1).
Any other scheme, such as
myapp:orjavascript:, is refused. -
Other metadata. Other RFC 7591 metadata must have the right types. Unknown metadata is kept. Values the server issues itself, such as a
client_id, are dropped, and so is asoftware_statement, which the server does not verify.
The server checks the metadata only at registration. It doesn't know which registration a Client came from, so the token and authorization endpoints don't enforce it. In your grant methods, such as clientCredentialsGrant and requestAuthorizationCode, check the client's stored grantTypes, responseTypes and tokenEndpointAuthMethod. Throw unauthorized_client (ErrorResponse.unauthorizedClient) for anything it didn't register.
Managing Registrations #
If managesClientRegistrations is true, a client can also read, update and delete its registration (RFC 7592, which is Experimental). The registrationEndpoint then also issues the client:
- a
registration_access_token; - a
registration_client_uri: theregistrationEndpointof theServerMetadatafollowed by theclient_id.
registrationManagementEndpoint serves that URI:
var manage = server.registrationManagementEndpoint(metadata);
router
..get('/register/:client_id', manage)
..put('/register/:client_id', manage)
..delete('/register/:client_id', manage);
In registerClient, store each registration's registrationAccessToken hashed, like its secret.
Implement findRegistration:
- It is given the client id and the token from the
Authorization: Bearerheader. Compare the token with the stored hash in constant time. - If the token matches, return the stored registration. Its
clientSecretandregistrationAccessTokenarenull, because only their hashes are kept. - Otherwise return
null. The request then getsinvalid_token, with status 401, whether the client exists or not, so the response reveals nothing about other clients.
The three methods:
- GET. It returns the registration with its registration access token, which the response must include (RFC 7592 §3). The secret is not returned, because only its hash is kept.
- PUT. It replaces the registration, with all of its metadata, and checks it like a new registration, so anything left out is no longer registered.
- The request must give the client's
client_id. - It may give the
client_secret, whichverifyClientchecks. - It may not give values the server issues, such as the
registration_access_token; that getsinvalid_client_metadata. updateClientstores the update. The update's secret and token arenullunless new ones were issued: for a client that is no longer public, or becauserotatesClientSecretOnUpdateorrotatesRegistrationAccessTokenissues new ones on each update.- The response includes the token, whether new or not.
- The request must give the client's
- DELETE. It passes the registration to
deleteClient, which deletes the client and revokes its tokens. The response is a 204.
By default, updateClient and deleteClient refuse with access_denied, a 403, so a server can offer reads only.
Extending #
A protocol built on OAuth 2.0, such as OpenID Connect (angel3_oidc_server), can check parameters and metadata the server doesn't know about through these hooks. Call super when you override one, so that other extensions still run.
checkAuthorizationRequest is given the parameters of each authorization request, from the query or from the form of a POST:
- When it runs. After the server has checked the client, the response type and PKCE, and before
requestAuthorizationCode. - To refuse. Throw an
AuthorizationException, such asinvalid_request. - Where the error goes. If the client has a registered redirect URI (from
registeredRedirectUri), the error is redirected there like the endpoint's other errors. Otherwise it is thrown.
@override
FutureOr<void> checkAuthorizationRequest(PseudoApplication client,
Map<String, dynamic> params, String state, RequestContext req) {
if (params['x_mode'] case var mode? when mode != 'fast') {
throw AuthorizationException(ErrorResponse(
ErrorResponse.invalidRequest, '"x_mode" must be "fast".', state));
}
return super.checkAuthorizationRequest(client, params, state, req);
}
checkClientMetadata is given the metadata of a new registration, or of a PUT that updates one:
- When it runs. After the request is authenticated, and before the server checks its own metadata.
- What it returns. The metadata, with any defaults added.
- To refuse. Throw an
AuthorizationExceptionwithinvalid_client_metadataorinvalid_redirect_uriand status 400. current. On an update it is the registration as it is now, so you can refuse a change. On a new registration it isnull.
@override
FutureOr<Map<String, dynamic>> checkClientMetadata(
Map<String, dynamic> metadata, RequestContext req,
{ClientRegistration? current}) async {
var checked =
await super.checkClientMetadata(metadata, req, current: current);
return {'x_tier': 'free', ...checked};
}
Helpers:
redirectUriProblem. It returns why a URI can't be a redirect URI under the registration rules, ornullif it can. Use it so that other redirect URIs, such aspost_logout_redirect_uris, follow the same rules.loopbackHosts. It lists the hosts on whichhttpis allowed:localhost,127.0.0.1and::1.ServerMetadata.copyWith. It changes some of the metadata, for example to add a scope. Anullargument keeps the current value, so to remove one, build a newServerMetadata.