Class ApiSecurityConfig

java.lang.Object
com.irurueta.authserver.authorization.api.web.ApiSecurityConfig

@Configuration(proxyBeanMethods=false) public class ApiSecurityConfig extends Object
Security filter chain of the REST API under /api/**: stateless, bearer tokens issued by this server and validated by a decoder of its own (never the framework-wide one, which also serves the UserInfo endpoint). That decoder only accepts a token that this server signed, whose issuer is this server, whose timestamps are valid, whose aud lists the API audience (authserver.api.audience, default auth-server) and that does not carry the purpose claim of the account tokens (an e-mail verification JWT is never a bearer token). Authentication and access failures are answered as described by RFC 6750 (WWW-Authenticate: Bearer ...).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Path pattern of the administration API.
    static final String
    Scope that grants access to the administration API; a role alone never does.
    static final String
    Property that holds the audience a token must carry to call the API.
    static final String
    Path pattern of the REST API.
    private static final String
    Prefix of the authorities derived from roles.
    private static final String
    Name of the claim that holds the roles.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter
    Creates the converter that maps the scope claim to SCOPE_* authorities and the roles claim to ROLE_* authorities.
    (package private) static org.springframework.security.oauth2.jwt.JwtDecoder
    apiJwtDecoder(com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, String issuer, String audience)
    Creates the decoder of the API's bearer tokens: signature against the published keys, issuer, timestamps, audience, and no purpose claim.
    org.springframework.security.web.SecurityFilterChain
    apiSecurityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http, com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, String issuer, String audience)
    Creates the chain of the REST API.
    private static org.springframework.security.oauth2.core.OAuth2TokenValidatorResult
    rejectPurposeBoundToken(org.springframework.security.oauth2.jwt.Jwt jwt)
    Rejects a purpose-bound account token (such as an e-mail verification JWT): it is signed by this server but never meant as a bearer token.
    private static Collection<org.springframework.security.core.GrantedAuthority>
    roleAuthorities(org.springframework.security.oauth2.jwt.Jwt jwt)
    Maps the roles of a token to authorities, adding the ROLE_ prefix when it is missing.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • API_PATTERN

      public static final String API_PATTERN
      Path pattern of the REST API.
      See Also:
    • ADMIN_PATTERN

      public static final String ADMIN_PATTERN
      Path pattern of the administration API.
      See Also:
    • ADMIN_SCOPE_AUTHORITY

      public static final String ADMIN_SCOPE_AUTHORITY
      Scope that grants access to the administration API; a role alone never does.
      See Also:
    • API_AUDIENCE_PROPERTY

      public static final String API_AUDIENCE_PROPERTY
      Property that holds the audience a token must carry to call the API.
      See Also:
    • ROLE_PREFIX

      private static final String ROLE_PREFIX
      Prefix of the authorities derived from roles.
      See Also:
    • ROLES_CLAIM

      private static final String ROLES_CLAIM
      Name of the claim that holds the roles.
      See Also:
  • Constructor Details

    • ApiSecurityConfig

      public ApiSecurityConfig()
  • Method Details

    • apiSecurityFilterChain

      @Bean @Order(0) public org.springframework.security.web.SecurityFilterChain apiSecurityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http, com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, @Value("${spring.security.oauth2.authorizationserver.issuer}") String issuer, @Value("${authserver.api.audience:auth-server}") String audience) throws Exception
      Creates the chain of the REST API.
      Parameters:
      http - security builder.
      jwkSource - the published keys of this server.
      issuer - issuer of this server (spring.security.oauth2.authorizationserver.issuer).
      audience - audience the API requires (authserver.api.audience).
      Returns:
      the API filter chain.
      Throws:
      Exception - if the chain cannot be built.
    • apiJwtDecoder

      static org.springframework.security.oauth2.jwt.JwtDecoder apiJwtDecoder(com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, String issuer, String audience)
      Creates the decoder of the API's bearer tokens: signature against the published keys, issuer, timestamps, audience, and no purpose claim.
      Parameters:
      jwkSource - the published keys of this server.
      issuer - issuer every token must have.
      audience - audience every token must list.
      Returns:
      the decoder.
    • rejectPurposeBoundToken

      private static org.springframework.security.oauth2.core.OAuth2TokenValidatorResult rejectPurposeBoundToken(org.springframework.security.oauth2.jwt.Jwt jwt)
      Rejects a purpose-bound account token (such as an e-mail verification JWT): it is signed by this server but never meant as a bearer token.
      Parameters:
      jwt - the token.
      Returns:
      a failure when the token carries the purpose claim, a success otherwise.
    • apiJwtAuthenticationConverter

      org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter apiJwtAuthenticationConverter()
      Creates the converter that maps the scope claim to SCOPE_* authorities and the roles claim to ROLE_* authorities.
      Returns:
      the converter.
    • roleAuthorities

      private static Collection<org.springframework.security.core.GrantedAuthority> roleAuthorities(org.springframework.security.oauth2.jwt.Jwt jwt)
      Maps the roles of a token to authorities, adding the ROLE_ prefix when it is missing.
      Parameters:
      jwt - token to read.
      Returns:
      the role authorities, empty when the token has no roles.