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

public final class OAuth2AuthorizationServer

  1. Object
  2. OAuth2AuthorizationServer
The endpoints of an authorization server, each a method that takes a request and answers it. The chain’s filters decide which request goes to which; see AuthorizationServerConfigurer, which is also where every rule these endpoints apply is described.

Fields

public static final String AUTH_TIME = "cn1.security.oauth2.authTime"Session attribute holding the last completed credential check, in epoch seconds.
public static final String AUTH_METHODS = "cn1.security.oauth2.authMethods"Session attribute holding how the last completed credential check was made: RFC 8176 method names separated by spaces, such as pwd otp mfa.

Constructors

public OAuth2AuthorizationServer(AuthorizationServerSettings settings, String fixedIssuer, String defaultAudience, RegisteredClientRepository clients, OAuth2AuthorizationService authorizations, JwkSource keys, JwtEncoder encoder, PasswordEncoder secrets, OAuth2TokenCustomizer customizer, OidcUserInfoMapper userInfo, RateLimiter verificationLimiter, int verificationAttempts, long verificationWindowSeconds, Clock clock)

Methods

public AuthorizationServerSettings getSettings()
public HttpServer.Response handle(HttpServer.Request request, String path)Answers a request to one of the endpoints that need no signed-in user: the token, revocation, device authorization, JWK Set, user info and metadata endpoints.
public boolean isUserEndpoint(String path)Whether path is an endpoint a user must be signed in for: the authorization endpoint or the device verification page.
public HttpServer.Response authorize(HttpServer.Request request, Authentication authentication)Answers the authorization endpoint for a browser whose user is authentication.
public static String authMethods(Authentication authentication, boolean secondFactor)The RFC 8176 amr values for a completed sign-in, space separated: hwk for a passkey (and user plus mfa when the authenticator verified the user), otherwise pwd, with otp and mfa added when a second factor was passed.
public HttpServer.Response deviceVerification(HttpServer.Request request, Authentication authentication, String csrfParameter, String csrfToken)Answers the device verification page for the signed-in user authentication: the form that takes a user code, the question that follows a right one, and the answer to that question.
public HttpServer.Response deviceVerification(HttpServer.Request request, Authentication authentication, String csrfParameter, String csrfHeader, String csrfToken)deviceVerification(HttpServer.Request, Authentication, String, String), told the header a CSRF token may come back in as well.

Inherited methods

Field details

AUTH_TIME

public static final String AUTH_TIME = "cn1.security.oauth2.authTime"
Session attribute holding the last completed credential check, in epoch seconds.

AUTH_METHODS

public static final String AUTH_METHODS = "cn1.security.oauth2.authMethods"
Session attribute holding how the last completed credential check was made: RFC 8176 method names separated by spaces, such as pwd otp mfa. A grant made in the session carries them as its amr attribute, and the ID token as its amr claim, so a resource server can tell a passkey sign-in from a password one. A token customizer copies the attribute into an access token that needs it.

Constructor details

OAuth2AuthorizationServer

public OAuth2AuthorizationServer(AuthorizationServerSettings settings, String fixedIssuer, String defaultAudience, RegisteredClientRepository clients, OAuth2AuthorizationService authorizations, JwkSource keys, JwtEncoder encoder, PasswordEncoder secrets, OAuth2TokenCustomizer customizer, OidcUserInfoMapper userInfo, RateLimiter verificationLimiter, int verificationAttempts, long verificationWindowSeconds, Clock clock)

Parameters

settings AuthorizationServerSettings
Not documented.
fixedIssuer String
the issuer, or null to read it off each request: a development profile only
defaultAudience String
the aud of an access token whose request named no resource; null for the issuer
clients RegisteredClientRepository
Not documented.
authorizations OAuth2AuthorizationService
Not documented.
keys JwkSource
Not documented.
encoder JwtEncoder
Not documented.
secrets PasswordEncoder
what client secrets were encoded with; null when no client has one
customizer OAuth2TokenCustomizer
Not documented.
userInfo OidcUserInfoMapper
Not documented.
verificationLimiter RateLimiter
what bounds wrong user codes; null to count in this process
verificationAttempts int
how many user codes one user may try in a window, when this process counts
verificationWindowSeconds long
the length of that window
clock Clock
Not documented.

Method details

getSettings

public AuthorizationServerSettings getSettings()

handle

public HttpServer.Response handle(HttpServer.Request request, String path)
Answers a request to one of the endpoints that need no signed-in user: the token, revocation, device authorization, JWK Set, user info and metadata endpoints.

Parameters

request HttpServer.Request
Not documented.
path String
the request’s path

Returns

the answer, or null when path is none of them

isUserEndpoint

public boolean isUserEndpoint(String path)
Whether path is an endpoint a user must be signed in for: the authorization endpoint or the device verification page.

authorize

public HttpServer.Response authorize(HttpServer.Request request, Authentication authentication)

Answers the authorization endpoint for a browser whose user is authentication.

The client and the redirect address are checked before anything else, and a request that fails either is answered here, with a 400: an error is sent to a redirect address only once that address is known to be the client’s own.

  • InsufficientAuthenticationException: when nobody is signed in and the request is a browser’s, so that the chain sends it to sign in and back

Parameters

request HttpServer.Request
Not documented.
authentication Authentication
who is signed in; null for nobody

authMethods

public static String authMethods(Authentication authentication, boolean secondFactor)
The RFC 8176 amr values for a completed sign-in, space separated: hwk for a passkey (and user plus mfa when the authenticator verified the user), otherwise pwd, with otp and mfa added when a second factor was passed. A kind of sign-in it does not know – a remember-me cookie, say – answers null and nothing is recorded.

Parameters

authentication Authentication
Not documented.
secondFactor boolean
whether the sign-in passed a second factor

deviceVerification

public HttpServer.Response deviceVerification(HttpServer.Request request, Authentication authentication, String csrfParameter, String csrfToken)

Answers the device verification page for the signed-in user authentication: the form that takes a user code, the question that follows a right one, and the answer to that question.

The chain’s CSRF protection covers the two posts; the token to put in the forms is passed in.

  • InsufficientAuthenticationException: when nobody is signed in, so that the chain sends the browser to sign in and back

Parameters

request HttpServer.Request
Not documented.
authentication Authentication
who is signed in; null for nobody
csrfParameter String
the name of the CSRF form field, or null
csrfToken String
its value

deviceVerification

public HttpServer.Response deviceVerification(HttpServer.Request request, Authentication authentication, String csrfParameter, String csrfHeader, String csrfToken)

deviceVerification(HttpServer.Request, Authentication, String, String), told the header a CSRF token may come back in as well.

The endpoint has two forms, and the request chooses. A browser gets the pages. A request that asks for JSON – its Accept names application/json and not text/html, or its body is application/json – gets the same three steps as data, so that an application that signed the user in itself can put its own screen in front of them:

  1. GET: {"csrf": {"headerName", "parameterName", "token"}}, the token a post must carry when the chain protects against CSRF.
  2. POST {"user_code": "..."}: the question, as {"user_code", "client_id", "client_name", "scope", "principal", "ticket"}.
  3. POST {"user_code": "...", "ticket": "...", "decision": "approve"} or "deny": {"status": "approved"} or {"status": "denied"}.

Nothing is relaxed for it. The user must be signed in – a request that is not is answered 401 login_required rather than sent to a login page – the chain’s CSRF protection covers both posts, each try at a code is counted, and an answer counts only with the ticket this server put in that session with the question, once. A code that is not valid is 400 invalid_grant, an answer without its ticket 400 invalid_request, and too many tries 429 slow_down.

Parameters

request HttpServer.Request
Not documented.
authentication Authentication
Not documented.
csrfParameter String
Not documented.
csrfHeader String
the name of the header a CSRF token may be sent in, or null
csrfToken String
Not documented.