Interface OAuth2AuthorizationService
- All Known Implementing Classes:
InMemoryOAuth2AuthorizationService, JdbcOAuth2AuthorizationService
public interface OAuth2AuthorizationService
Where an authorization server keeps the grants it has made and the secrets it issued under them.
A secret -- an authorization code, a refresh token, a device code, a user
code -- is never stored: its SHA-256 is, see
OAuth2Parameters.sha256(String). And a
secret that works once is used up by consumeToken(String, String, long), which an
implementation must make one conditional statement on one row, never a read
followed by a write: several processes may share the store, and of two that
present the same code at the same moment exactly one may be told yes.
This is not the interface of Spring Authorization Server's service of the
same name, whose save and findByToken leave that atomicity to the
caller.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classOne stored secret, asfindToken(String, String)reports it. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe kind of an opaque access token, one stored with its claims.static final StringThe kind of thejtiof a client assertion already accepted.static final StringThe kind of an authorization code.static final StringThe kind of an RFC 8628 device code.static final StringThe kind of a pushed authorization request'srequest_uri(RFC 9126).static final StringThe kind of a refresh token.static final StringThe kind of an RFC 8628 user code. -
Method Summary
Modifier and TypeMethodDescriptionvoidRecords a secret issued under a grant.default voidaddTokenData(String kind, String authorizationId, String tokenHash, long expiresAt, String data) Records a secret together with what it stands for: an opaque access token (ACCESS_TOKEN) with the claims introspection answers for it, or a pushed authorization request (PUSHED_REQUEST) with its parameters.default booleanRecords a value that may be presented only once -- thejtiof a client assertion -- unless it is already recorded.booleanconsumeToken(String kind, String tokenHash, long now) Uses a secret up, if it is there, unused and has not expired atnow, and recordsnowas when.booleanAnswers a device grant that isOAuth2Authorization.PENDING: makes itOAuth2Authorization.ACTIVEforprincipalName, orOAuth2Authorization.DENIED.default booleanextendToken(String kind, String tokenHash, long now, long expiresAt) Extends an unused, unexpired token to at leastexpiresAt, atomically.The grant with this id, or null: one that was removed is revoked.What is stored for a secret, whatever its state; null when nothing is.default String[]findTokenData(String kind, String tokenHash, long now) WhataddTokenData(String, String, String, long, String)stored for a secret that is not used up and unexpired atnow; null otherwise.default booleanissueTokens(String authorizationId, long now, long expiresAt, String refreshTokenHash, boolean reuse) Issues tokens only while an active, unexpired grant still exists.default booleanClaims a polling interval on an unused, unexpired token.intpurgeExpired(long now, int limit) Forgets up tolimitgrants and secrets that expired beforenow.voidRemoves the grant and every secret issued under it.voidsave(OAuth2Authorization authorization) Storesauthorization, in place of the one with its id if there is one.voidtouchToken(String kind, String tokenHash, long now) Records that a secret was presented atnow, without checking its previous timestamp.
-
Field Details
-
CODE
-
REFRESH_TOKEN
-
DEVICE_CODE
-
USER_CODE
-
ACCESS_TOKEN
The kind of an opaque access token, one stored with its claims.- See Also:
-
PUSHED_REQUEST
The kind of a pushed authorization request'srequest_uri(RFC 9126).- See Also:
-
CLIENT_ASSERTION
The kind of thejtiof a client assertion already accepted.- See Also:
-
-
Method Details
-
save
Storesauthorization, in place of the one with its id if there is one. -
findById
The grant with this id, or null: one that was removed is revoked. -
remove
Removes the grant and every secret issued under it. -
addToken
Records a secret issued under a grant.- Parameters:
kind-CODE,REFRESH_TOKEN,DEVICE_CODEorUSER_CODEtokenHash- the SHA-256 of the secretexpiresAt- epoch milliseconds
-
issueTokens
default boolean issueTokens(String authorizationId, long now, long expiresAt, String refreshTokenHash, boolean reuse) Issues tokens only while an active, unexpired grant still exists. Extending the grant and adding or extending its refresh token must be one atomic operation with respect toremove(String), including across server processes. A removed grant must never be recreated. Custom stores must implement this operation before issuing user tokens; the default fails closed.- Parameters:
refreshTokenHash- null when no refresh token is issuedreuse- whether the hash names an existing unused, unexpired refresh token- Returns:
- false when the grant or reused refresh token is no longer valid
-
claimOnce
Records a value that may be presented only once -- thejtiof a client assertion -- unless it is already recorded. Checking and recording must be one atomic operation, including across processes sharing the store. The record may be forgotten onceexpiresAthas passed. Custom stores must implement this before acceptingprivate_key_jwt; the default fails closed.- Parameters:
kind- what the value is, such asCLIENT_ASSERTIONvalueHash- the SHA-256 of the valueexpiresAt- epoch milliseconds- Returns:
- whether THIS call recorded it; false when it was already recorded
-
addTokenData
default void addTokenData(String kind, String authorizationId, String tokenHash, long expiresAt, String data) Records a secret together with what it stands for: an opaque access token (ACCESS_TOKEN) with the claims introspection answers for it, or a pushed authorization request (PUSHED_REQUEST) with its parameters. Custom stores must implement this, andfindTokenData(String, String, long), before either is used; the default fails closed.- Parameters:
authorizationId- the grant it belongs to, or the empty string when there is none (a client_credentials token, a pushed request)tokenHash- the SHA-256 of the secretexpiresAt- epoch millisecondsdata- what to hand back fromfindTokenData(String, String, long)
-
findTokenData
WhataddTokenData(String, String, String, long, String)stored for a secret that is not used up and unexpired atnow; null otherwise. Whether the grant it names is still live is the caller's to ask.- Returns:
- the data, and the grant's id: a two-element array, or null
-
findToken
What is stored for a secret, whatever its state; null when nothing is. -
consumeToken
-
extendToken
-
touchToken
Records that a secret was presented atnow, without checking its previous timestamp. UsepollToken(String, String, long, long)to enforce a polling interval. -
pollToken
Claims a polling interval on an unused, unexpired token. Checking the previous poll and recordingnowmust be one atomic operation, including across processes sharing a database. Rejected polls do not claim an interval. Custom stores must implement this before enabling the device grant.- Parameters:
intervalMillis- the minimum time between accepted polls, greater than zero- Returns:
- whether this call claimed the interval
-
decide
Answers a device grant that isOAuth2Authorization.PENDING: makes itOAuth2Authorization.ACTIVEforprincipalName, orOAuth2Authorization.DENIED.- Parameters:
attributes- what to remember of the user who approved, merged into the grant's own- Returns:
- whether THIS call answered it
-
purgeExpired
int purgeExpired(long now, int limit) Forgets up tolimitgrants and secrets that expired beforenow.- Returns:
- how many were forgotten
-