Search in sources :

Example 6 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-security by micronaut-projects.

the class DefaultAuthorizationRedirectHandler method expandedUri.

/**
 * @param baseUrl Base Url
 * @param queryParams Query Parameters
 * @return The Expanded URI
 */
protected String expandedUri(@NonNull String baseUrl, @NonNull Map<String, Object> queryParams) {
    UriBuilder builder = UriBuilder.of(baseUrl);
    queryParams.entrySet().stream().filter(entry -> entry.getValue() != null).forEach(entry -> builder.queryParam(entry.getKey(), entry.getValue()));
    return builder.toString();
}
Also used : Logger(org.slf4j.Logger) UriBuilder(io.micronaut.http.uri.UriBuilder) LoggerFactory(org.slf4j.LoggerFactory) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) Singleton(jakarta.inject.Singleton) HttpHeaders(io.micronaut.http.HttpHeaders) HashMap(java.util.HashMap) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) HttpStatus(io.micronaut.http.HttpStatus) Map(java.util.Map) HttpResponse(io.micronaut.http.HttpResponse) Optional(java.util.Optional) UriBuilder(io.micronaut.http.uri.UriBuilder)

Example 7 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-security by micronaut-projects.

the class DefaultAccessRefreshTokenGenerator method generate.

/**
 * Generate an {@link AccessRefreshToken} response for the given
 * refresh token and claims.
 *
 * @param refreshToken The refresh token
 * @param oldClaims The claims to generate the access token
 * @return The http response
 */
@NonNull
public Optional<AccessRefreshToken> generate(@Nullable String refreshToken, @NonNull Map<String, ?> oldClaims) {
    Map<String, Object> claims = claimsGenerator.generateClaimsSet(oldClaims, accessTokenExpiration(oldClaims));
    Optional<String> optionalAccessToken = tokenGenerator.generateToken(claims);
    if (!optionalAccessToken.isPresent()) {
        debug(LOG, "tokenGenerator failed to generate access token claims: {}", claims.entrySet().stream().map(entry -> entry.getKey() + "=>" + entry.getValue().toString()).collect(Collectors.joining(", ")));
        return Optional.empty();
    }
    String accessToken = optionalAccessToken.get();
    eventPublisher.publishEvent(new AccessTokenGeneratedEvent(accessToken));
    return Optional.of(tokenRenderer.render(accessTokenExpiration(oldClaims), accessToken, refreshToken));
}
Also used : AccessTokenGeneratedEvent(io.micronaut.security.token.event.AccessTokenGeneratedEvent) NonNull(io.micronaut.core.annotation.NonNull)

Example 8 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-security by micronaut-projects.

the class DefaultAccessRefreshTokenGenerator method generate.

/**
 * Generate a new access refresh token.
 *
 * @param refreshToken The refresh token
 * @param authentication The user details to create a new access token
 * @return The optional access refresh token
 */
@NonNull
@Override
public Optional<AccessRefreshToken> generate(@Nullable String refreshToken, @NonNull Authentication authentication) {
    Optional<String> optionalAccessToken = tokenGenerator.generateToken(authentication, accessTokenExpiration(authentication));
    if (!optionalAccessToken.isPresent()) {
        debug(LOG, "Failed to generate access token for user {}", authentication.getName());
        return Optional.empty();
    }
    String accessToken = optionalAccessToken.get();
    eventPublisher.publishEvent(new AccessTokenGeneratedEvent(accessToken));
    return Optional.of(tokenRenderer.render(authentication, accessTokenExpiration(authentication), accessToken, refreshToken));
}
Also used : AccessTokenGeneratedEvent(io.micronaut.security.token.event.AccessTokenGeneratedEvent) NonNull(io.micronaut.core.annotation.NonNull)

Example 9 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-security by micronaut-projects.

the class SignedRefreshTokenGenerator method generate.

@NonNull
@Override
public Optional<String> generate(@NonNull Authentication authentication, @NonNull String token) {
    try {
        JWSObject jwsObject = new JWSObject(new JWSHeader(algorithm), new Payload(token));
        jwsObject.sign(signer);
        return Optional.of(jwsObject.serialize());
    } catch (JOSEException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("JOSEException signing a JWS Object");
        }
    }
    return Optional.empty();
}
Also used : Payload(com.nimbusds.jose.Payload) JWSObject(com.nimbusds.jose.JWSObject) JOSEException(com.nimbusds.jose.JOSEException) JWSHeader(com.nimbusds.jose.JWSHeader) NonNull(io.micronaut.core.annotation.NonNull)

Example 10 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-security by micronaut-projects.

the class X509AuthenticationFetcher method extractName.

/**
 * Extracts the name from the certificate using the subject DN regex.
 *
 * @param certificate the client certificate
 * @return the name if found
 */
@NonNull
protected Optional<String> extractName(@NonNull X509Certificate certificate) {
    String subjectDN = certificate.getSubjectX500Principal().getName();
    Matcher matcher = subjectDnPattern.matcher(subjectDN);
    if (!matcher.find()) {
        return Optional.empty();
    }
    if (matcher.groupCount() != 1) {
        return Optional.empty();
    }
    return Optional.of(matcher.group(1));
}
Also used : Matcher(java.util.regex.Matcher) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

NonNull (io.micronaut.core.annotation.NonNull)19 Singleton (jakarta.inject.Singleton)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 ApplicationContextBuilder (io.micronaut.context.ApplicationContextBuilder)2 BeanContext (io.micronaut.context.BeanContext)2 StringUtils (io.micronaut.core.util.StringUtils)2 MediaType (io.micronaut.http.MediaType)2 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)2 OauthClientConfiguration (io.micronaut.security.oauth2.configuration.OauthClientConfiguration)2 AccessTokenGeneratedEvent (io.micronaut.security.token.event.AccessTokenGeneratedEvent)2 IOException (java.io.IOException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BindableRockerModel (com.fizzed.rocker.BindableRockerModel)1 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)1