Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class JdbcOAuth2AuthorizationService

  1. Object
  2. JdbcOAuth2AuthorizationService

ImplementsOAuth2AuthorizationService

Grants kept in the server’s database, in the cn1_oauth2_authorization and cn1_oauth2_token tables of SecuritySchema: what lets tokens outlive a restart, and several processes be one authorization server.

@Bean
OAuth2AuthorizationService authorizations(DataSource dataSource) {
    return new JdbcOAuth2AuthorizationService(dataSource);
}

A secret is one row, keyed by its SHA-256. Using one up is

UPDATE cn1_oauth2_token SET used = 1, polled_at = ?
 WHERE token_hash = ? AND kind = ? AND used = 0 AND expires_at > ?

and the call that sees one row changed is the one that used it: the database decides between two processes, and no lock is held here. The row stays, marked, until it expires – that is how a code or a refresh token presented a second time is told from one that never existed.

Constructors

public JdbcOAuth2AuthorizationService(DataSource dataSource)

Methods

public void save(OAuth2Authorization a)Stores authorization, in place of the one with its id if there is one.
public OAuth2Authorization findById(String id)The grant with this id, or null: one that was removed is revoked.
public void remove(String id)Removes the grant and every secret issued under it.
public void addToken(String authorizationId, String kind, String tokenHash, long expiresAt)Records a secret issued under a grant.
public boolean issueTokens(String authorizationId, long now, long expiresAt, String refreshTokenHash, boolean reuse)Issues tokens only while an active, unexpired grant still exists.
public OAuth2AuthorizationService.StoredToken findToken(String kind, String tokenHash)What is stored for a secret, whatever its state; null when nothing is.
public boolean consumeToken(String kind, String tokenHash, long now)Uses a secret up, if it is there, unused and has not expired at now, and records now as when.
public void touchToken(String kind, String tokenHash, long now)Records that a secret was presented at now, without checking its previous timestamp.
public boolean pollToken(String kind, String tokenHash, long now, long intervalMillis)Claims a polling interval on an unused, unexpired token.
public boolean extendToken(String kind, String tokenHash, long now, long expiresAt)Extends an unused, unexpired token to at least expiresAt, atomically.
public boolean decide(String id, boolean approved, String principalName, Map<String, Object> attributes)Answers a device grant that is OAuth2Authorization.PENDING: makes it OAuth2Authorization.ACTIVE for principalName, or OAuth2Authorization.DENIED.
public int purgeExpired(long now, int limit)Forgets up to limit grants and secrets that expired before now.

Inherited nested types

Inherited fields

Inherited methods

Constructor details

JdbcOAuth2AuthorizationService

public JdbcOAuth2AuthorizationService(DataSource dataSource)

Method details

save

public void save(OAuth2Authorization a)
Stores authorization, in place of the one with its id if there is one.

findById

public OAuth2Authorization findById(String id)
The grant with this id, or null: one that was removed is revoked.

remove

public void remove(String id)
Removes the grant and every secret issued under it.

addToken

public void addToken(String authorizationId, String kind, String tokenHash, long expiresAt)
Records a secret issued under a grant.

Parameters

authorizationId String
Not documented.
kind String
CODE, REFRESH_TOKEN, DEVICE_CODE or USER_CODE
tokenHash String
the SHA-256 of the secret
expiresAt long
epoch milliseconds

issueTokens

public 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 to remove, 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

authorizationId String
Not documented.
now long
Not documented.
expiresAt long
Not documented.
refreshTokenHash String
null when no refresh token is issued
reuse boolean
whether the hash names an existing unused, unexpired refresh token

Returns

false when the grant or reused refresh token is no longer valid

findToken

public OAuth2AuthorizationService.StoredToken findToken(String kind, String tokenHash)
What is stored for a secret, whatever its state; null when nothing is.

consumeToken

public boolean consumeToken(String kind, String tokenHash, long now)
Uses a secret up, if it is there, unused and has not expired at now, and records now as when.

Returns

whether THIS call used it up

touchToken

public void touchToken(String kind, String tokenHash, long now)
Records that a secret was presented at now, without checking its previous timestamp. Use pollToken to enforce a polling interval.

pollToken

public boolean pollToken(String kind, String tokenHash, long now, long intervalMillis)
Claims a polling interval on an unused, unexpired token. Checking the previous poll and recording now must 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

kind String
Not documented.
tokenHash String
Not documented.
now long
Not documented.
intervalMillis long
the minimum time between accepted polls, greater than zero

Returns

whether this call claimed the interval

extendToken

public boolean extendToken(String kind, String tokenHash, long now, long expiresAt)
Extends an unused, unexpired token to at least expiresAt, atomically. Returns false if it is missing, used or expired at now. Custom stores must implement this before enabling refresh token reuse.

decide

public boolean decide(String id, boolean approved, String principalName, Map<String, Object> attributes)
Answers a device grant that is OAuth2Authorization.PENDING: makes it OAuth2Authorization.ACTIVE for principalName, or OAuth2Authorization.DENIED.

Parameters

id String
Not documented.
approved boolean
Not documented.
principalName String
Not documented.
attributes Map<String, Object>
what to remember of the user who approved, merged into the grant’s own

Returns

whether THIS call answered it

purgeExpired

public int purgeExpired(long now, int limit)
Forgets up to limit grants and secrets that expired before now.

Returns

how many were forgotten