Class OAuth2AuthorizationServer

java.lang.Object
com.codename1.backend.security.oauth2.server.authorization.OAuth2AuthorizationServer

public final class OAuth2AuthorizationServer extends Object
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.
  • Field Details

    • AUTH_TIME

      public static final String AUTH_TIME
      Session attribute holding the last completed credential check, in epoch seconds.
      See Also:
    • AUTH_METHODS

      public static final String AUTH_METHODS
      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.
      See Also:
  • 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:
      fixedIssuer - the issuer, or null to read it off each request: a development profile only
      defaultAudience - the aud of an access token whose request named no resource; null for the issuer
      secrets - what client secrets were encoded with; null when no client has one
      verificationLimiter - what bounds wrong user codes; null to count in this process
      verificationAttempts - how many user codes one user may try in a window, when this process counts
      verificationWindowSeconds - the length of that window
  • Method Details

    • clientRegistration

      public void clientRegistration(ClientRegistrationPolicy policy)

      Turns on client registration (RFC 7591) at AuthorizationServerSettings.getClientRegistrationEndpoint(), governed by policy; see ClientRegistrationPolicy for what a registered client may be. Registered clients are saved in the repository, so it must be one that keeps them somewhere other than this process: a repository that answers RegisteredClientRepository.findAll() with a list -- the in-memory one -- is refused, since every stranger's client would then be kept in memory for as long as the process runs.

      Call before the server answers its first request.

    • authorizationConsent

      public void authorizationConsent(AuthorizationConsentPolicy policy, String page)

      Asks the user to approve an authorization when policy says so, as well as for every client whose ClientSettings.isRequireAuthorizationConsent() is set, and shows page to do it instead of the built-in one.

      page, when given, is a path or address the browser is sent to with the query parameters client_id, scope (the scopes asked for, separated by spaces), ticket and action. The page posts a form to action -- with the session's CSRF token, as any post in the chain -- carrying ticket, decision (approve or deny) and one scope field per approved scope (a field may also hold several, separated by spaces). The grant carries the approved scopes that were asked for and nothing else; the scopes of a request that asked for some, with none approved, are a denial.

      Parameters:
      policy - which authorizations to ask about; null for only the clients that require it
      page - the consent page; null for the built-in one
    • 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:
      path - 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:
      authentication - who is signed in; null for nobody
    • authorize

      public HttpServer.Response authorize(HttpServer.Request request, Authentication authentication, String csrfParameter, String csrfToken)
      authorize(HttpServer.Request, Authentication), told the CSRF token the built-in consent page's forms must carry.
      Parameters:
      csrfParameter - the name of the CSRF form field, or null
      csrfToken - its value
    • 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:
      secondFactor - 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:
      authentication - who is signed in; null for nobody
      csrfParameter - the name of the CSRF form field, or null
      csrfToken - 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:
      csrfHeader - the name of the header a CSRF token may be sent in, or null