Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class AccessTokenProvider
- Object
- AccessTokenProvider
An access token for calling another service, kept fresh.
Wraps an AccessTokenGrant and holds the one token it last issued. That
token is handed out until refreshSkewSeconds before it expires (60 by
default), so a request that leaves with it still has that margin for clock
differences and time in flight; then the next caller refreshes it.
Concurrent callers share the refresh work. Exactly one runs the exchange; the others keep using the current token while it has not expired, and wait for the exchange only when there is no unexpired token to give them. A failed refresh is shared the same way: the waiters get the failure, not an expired token.
A failure is not retried on the spot. Retrying in a loop would hold the
request that hit it, and on a virtual thread hold its host, for the whole
backoff. Instead the provider backs off: after n consecutive failures no
exchange is attempted for min(maxBackoff, 2^(n-1)) seconds, jittered
downwards by up to half, and callers in that window get the token still in
hand if it has not expired, or the last failure if it has. The first success
resets the count. A refused credential (invalid_client, invalid_grant)
backs off the same way, so a revoked key cannot turn every request into an
exchange.
One provider holds one token, so its memory is bounded by construction; a server that calls several services, or one service with several scope sets, keeps one provider for each.
Constructors
public AccessTokenProvider(AccessTokenGrant grant) |
Methods
public synchronized void setClock(Clock clock) | |
public synchronized void setRefreshSkewSeconds(long seconds) | How long before expiry a token stops being handed out. |
public synchronized void setMaxBackoffSeconds(long seconds) | The longest pause between failed exchanges. |
public AccessToken getToken() | A token that has not expired. |
public String getTokenValue() | The value of the current token, for an Authorization: Bearer header. |
public synchronized void invalidate() | Forgets the current token, so the next call exchanges again. |
public synchronized int getConsecutiveFailures() | Consecutive failed exchanges since the last success. |
public String toString() | Returns a string representation of the object. |
Inherited methods
Constructor details
AccessTokenProvider
public AccessTokenProvider(AccessTokenGrant grant)Method details
setClock
public synchronized void setClock(Clock clock)setRefreshSkewSeconds
public synchronized void setRefreshSkewSeconds(long seconds)setMaxBackoffSeconds
public synchronized void setMaxBackoffSeconds(long seconds)getToken
public AccessToken getToken()Throws
TokenAcquisitionException- when none can be had: the exchange failed, or failed recently and the provider is backing off
getTokenValue
public String getTokenValue()Authorization: Bearer header.invalidate
public synchronized void invalidate()getConsecutiveFailures
public synchronized int getConsecutiveFailures()toString
public String toString()