All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.scalecube.security.jwt.DefaultJwtAuthenticator Maven / Gradle / Ivy

package io.scalecube.security.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.scalecube.security.api.Profile;
import java.util.Map;
import reactor.core.publisher.Mono;

public final class DefaultJwtAuthenticator implements JwtAuthenticator {

  private final JwtKeyResolver jwtKeyResolver;

  public DefaultJwtAuthenticator(JwtKeyResolver jwtKeyResolver) {
    this.jwtKeyResolver = jwtKeyResolver;
  }

  @Override
  public Mono authenticate(String token) {
    return Mono.defer(
        () -> {
          String tokenWithoutSignature = token.substring(0, token.lastIndexOf(".") + 1);

          JwtParser parser = Jwts.parser();

          Jwt claims = parser.parseClaimsJwt(tokenWithoutSignature);

          return jwtKeyResolver
              .resolve((Map) claims.getHeader())
              .map(key -> parser.setSigningKey(key).parseClaimsJws(token).getBody())
              .map(this::profileFromClaims);
        })
        .onErrorMap(AuthenticationException::new);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy