io.jsonwebtoken.io.Decoders Java Examples

The following examples show how to use io.jsonwebtoken.io.Decoders. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TokenProvider.java    From jhipster-registry with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #2
Source File: TokenProvider.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #3
Source File: TokenProvider.java    From tutorials with MIT License 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #4
Source File: TokenProvider.java    From ehcache3-samples with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #5
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeKeyPair() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256);

    String privateKey = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKey = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    String token = AuthTokenUtils.createToken(AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKey), SignatureAlgorithm.RS256),
            SUBJECT,
            Optional.empty());

    @SuppressWarnings("unchecked")
    Jwt<?, Claims> jwt = Jwts.parser()
            .setSigningKey(AuthTokenUtils.decodePublicKey(Decoders.BASE64.decode(publicKey), SignatureAlgorithm.RS256))
            .parse(token);

    assertNotNull(jwt);
    assertNotNull(jwt.getBody());
    assertEquals(jwt.getBody().getSubject(), SUBJECT);
}
 
Example #6
Source File: TokenProvider.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #7
Source File: TokenProvider.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #8
Source File: TokenProvider.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #9
Source File: TokenProvider.java    From 21-points with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #10
Source File: TokensCliUtils.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public void run() throws Exception {
    String token;
    if (args != null) {
        token = args.get(0);
    } else if (stdin) {
        @Cleanup
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        token = r.readLine();
    } else if (tokenFile != null) {
        token = new String(Files.readAllBytes(Paths.get(tokenFile)), Charsets.UTF_8);
    } else if (System.getenv("TOKEN") != null) {
        token = System.getenv("TOKEN");
    } else {
        System.err.println(
                "Token needs to be either passed as an argument or through `--stdin`, `--token-file` or by the `TOKEN` environment variable");
        System.exit(1);
        return;
    }

    String[] parts = token.split("\\.");
    System.out.println(new String(Decoders.BASE64URL.decode(parts[0])));
    System.out.println("---");
    System.out.println(new String(Decoders.BASE64URL.decode(parts[1])));
}
 
Example #11
Source File: TokenProvider.java    From jhipster-online with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    byte[] keyBytes;
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    if (!StringUtils.isEmpty(secret)) {
        log.warn("Warning: the JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret());
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #12
Source File: TokenProviderTest.java    From 21-points with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    jHipsterProperties = Mockito.mock(JHipsterProperties.class);
    tokenProvider = new TokenProvider(jHipsterProperties);
    key = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    ReflectionTestUtils.setField(tokenProvider, "key", key);
    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", ONE_MINUTE);
}
 
Example #13
Source File: TokenProviderTest.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    tokenProvider = new TokenProvider( new JHipsterProperties());
    key = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    ReflectionTestUtils.setField(tokenProvider, "key", key);
    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", ONE_MINUTE);
}
 
Example #14
Source File: TokenProviderTest.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
private String createTokenWithDifferentSignature() {
    Key otherKey = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("Xfd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    return Jwts.builder()
        .setSubject("anonymous")
        .signWith(otherKey, SignatureAlgorithm.HS512)
        .setExpiration(new Date(new Date().getTime() + ONE_MINUTE))
        .compact();
}
 
Example #15
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyPair() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.RS256);
    String token = AuthTokenUtils.createToken(privateKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);

    provider.close();
}
 
Example #16
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyPairWithECDSA() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.ES256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);
    // Set that we are using EC keys
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_ALG, SignatureAlgorithm.ES256.getValue());

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.ES256);
    String token = AuthTokenUtils.createToken(privateKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);

    provider.close();
}
 
Example #17
Source File: JWTFilterTest.java    From 21-points with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JHipsterProperties jHipsterProperties = new JHipsterProperties();
    tokenProvider = new TokenProvider(jHipsterProperties);
    ReflectionTestUtils.setField(tokenProvider, "key",
        Keys.hmacShaKeyFor(Decoders.BASE64
            .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8")));

    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", 60000);
    jwtFilter = new JWTFilter(tokenProvider);
    SecurityContextHolder.getContext().setAuthentication(null);
}
 
Example #18
Source File: JWTFilterTest.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    JHipsterProperties jHipsterProperties = new JHipsterProperties();
    tokenProvider = new TokenProvider(jHipsterProperties);
    ReflectionTestUtils.setField(tokenProvider, "key",
        Keys.hmacShaKeyFor(Decoders.BASE64
            .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8")));

    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", 60000);
    jwtFilter = new JWTFilter(tokenProvider);
    SecurityContextHolder.getContext().setAuthentication(null);
}
 
Example #19
Source File: TokenProviderTest.java    From 21-points with Apache License 2.0 5 votes vote down vote up
private String createTokenWithDifferentSignature() {
    Key otherKey = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("Xfd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    return Jwts.builder()
        .setSubject("anonymous")
        .signWith(otherKey, SignatureAlgorithm.HS512)
        .setExpiration(new Date(new Date().getTime() + ONE_MINUTE))
        .compact();
}
 
Example #20
Source File: DefaultJwtBuilder.java    From jjwt with Apache License 2.0 5 votes vote down vote up
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) throws InvalidKeyException {
    Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty.");
    Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
    byte[] bytes = Decoders.BASE64.decode(base64EncodedSecretKey);
    return signWith(alg, bytes);
}
 
Example #21
Source File: TokenProviderUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void setup() {
    jHipsterProperties = Mockito.mock(JHipsterProperties.class);
    tokenProvider = new TokenProvider(jHipsterProperties);
    key = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    ReflectionTestUtils.setField(tokenProvider, "key", key);
    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", ONE_MINUTE);
}
 
Example #22
Source File: TokenProviderUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
private String createTokenWithDifferentSignature() {
    Key otherKey = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("Xfd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    return Jwts.builder()
        .setSubject("anonymous")
        .signWith(otherKey, SignatureAlgorithm.HS512)
        .setExpiration(new Date(new Date().getTime() + ONE_MINUTE))
        .compact();
}
 
Example #23
Source File: JWTFilterUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void setup() {
    JHipsterProperties jHipsterProperties = new JHipsterProperties();
    tokenProvider = new TokenProvider(jHipsterProperties);
    ReflectionTestUtils.setField(tokenProvider, "key",
        Keys.hmacShaKeyFor(Decoders.BASE64
            .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8")));

    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", 60000);
    jwtFilter = new JWTFilter(tokenProvider);
    SecurityContextHolder.getContext().setAuthentication(null);
}
 
Example #24
Source File: TokenProvider.java    From eladmin with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
    byte[] keyBytes = Decoders.BASE64.decode(properties.getBase64Secret());
    Key key = Keys.hmacShaKeyFor(keyBytes);
    jwtParser = Jwts.parserBuilder()
            .setSigningKey(key)
            .build();
    jwtBuilder = Jwts.builder()
            .signWith(key, SignatureAlgorithm.HS512);
}
 
Example #25
Source File: TokenProvider.java    From flair-registry with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
    String secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getSecret();
    String base64secret = jHipsterProperties.getSecurity().getAuthentication().getJwt().getBase64Secret();
    byte[] keyBytes;
    if (StringUtils.isEmpty(base64secret)) {
        log.info("The JWT key used is not Base64-encoded. " +
            "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");

        if (StringUtils.isEmpty(secret)) {
            log.error("\n----------------------------------------------------------\n" +
                "Your JWT secret key is not set up, you will not be able to log into the JHipster.\n"+
                "Please read the documentation at https://www.jhipster.tech/jhipster-registry/\n" +
                "----------------------------------------------------------");
            throw new RuntimeException("No JWT secret key is configured, the application cannot start.");
        }
        keyBytes = secret.getBytes(StandardCharsets.UTF_8);
    } else {
        log.debug("Using a Base64-encoded JWT secret key");
        keyBytes = Decoders.BASE64.decode(base64secret);
    }
    this.key = Keys.hmacShaKeyFor(keyBytes);
    this.tokenValidityInMilliseconds =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt().getTokenValidityInSeconds();
    this.tokenValidityInMillisecondsForRememberMe =
        1000 * jHipsterProperties.getSecurity().getAuthentication().getJwt()
            .getTokenValidityInSecondsForRememberMe();
}
 
Example #26
Source File: JWTFilterTest.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    JHipsterProperties jHipsterProperties = new JHipsterProperties();
    tokenProvider = new TokenProvider(jHipsterProperties);
    ReflectionTestUtils.setField(tokenProvider, "key",
        Keys.hmacShaKeyFor(Decoders.BASE64
            .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8")));

    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", 60000);
    jwtFilter = new JWTFilter(tokenProvider);
    SecurityContextHolder.getContext().setAuthentication(null);
}
 
Example #27
Source File: TokenProviderTest.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    tokenProvider = new TokenProvider( new JHipsterProperties());
    key = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    ReflectionTestUtils.setField(tokenProvider, "key", key);
    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", ONE_MINUTE);
}
 
Example #28
Source File: TokenProviderTest.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private String createTokenWithDifferentSignature() {
    Key otherKey = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("Xfd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    return Jwts.builder()
        .setSubject("anonymous")
        .signWith(otherKey, SignatureAlgorithm.HS512)
        .setExpiration(new Date(new Date().getTime() + ONE_MINUTE))
        .compact();
}
 
Example #29
Source File: TokenProviderTest.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
private String createTokenWithDifferentSignature() {
    Key otherKey = Keys.hmacShaKeyFor(Decoders.BASE64
        .decode("Xfd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8"));

    return Jwts.builder()
        .setSubject("anonymous")
        .signWith(otherKey, SignatureAlgorithm.HS512)
        .setExpiration(new Date(new Date().getTime() + ONE_MINUTE))
        .compact();
}
 
Example #30
Source File: JWTFilterTest.java    From e-commerce-microservice with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JHipsterProperties jHipsterProperties = new JHipsterProperties();
    tokenProvider = new TokenProvider(jHipsterProperties);
    ReflectionTestUtils.setField(tokenProvider, "key",
        Keys.hmacShaKeyFor(Decoders.BASE64
            .decode("fd54a45s65fds737b9aafcb3412e07ed99b267f33413274720ddbb7f6c5e64e9f14075f2d7ed041592f0b7657baf8")));

    ReflectionTestUtils.setField(tokenProvider, "tokenValidityInMilliseconds", 60000);
    jwtFilter = new JWTFilter(tokenProvider);
    SecurityContextHolder.getContext().setAuthentication(null);
}