use of org.apache.syncope.common.rest.api.service.AccessTokenService in project syncope by apache.
the class JWTITCase method getJWTToken.
@Test
public void getJWTToken() throws ParseException {
// Get the token
SyncopeClient localClient = clientFactory.create(ADMIN_UNAME, ADMIN_PWD);
AccessTokenService accessTokenService = localClient.getService(AccessTokenService.class);
Response response = accessTokenService.login();
String token = response.getHeaderString(RESTHeaders.TOKEN);
assertNotNull(token);
String expiry = response.getHeaderString(RESTHeaders.TOKEN_EXPIRE);
assertNotNull(expiry);
// Validate the signature
JwsJwtCompactConsumer consumer = new JwsJwtCompactConsumer(token);
JwsSignatureVerifier jwsSignatureVerifier = new HmacJwsSignatureVerifier(JWS_KEY.getBytes(), SignatureAlgorithm.HS512);
assertTrue(consumer.verifySignatureWith(jwsSignatureVerifier));
Date now = new Date();
// Verify the expiry header matches that of the token
Long expiryTime = consumer.getJwtClaims().getExpiryTime();
assertNotNull(expiryTime);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date tokenDate = dateFormat.parse(dateFormat.format(new Date(expiryTime * 1000L)));
Date parsedDate = dateFormat.parse(expiry);
assertEquals(tokenDate, parsedDate);
assertTrue(parsedDate.after(now));
// Verify issuedAt
Long issuedAt = consumer.getJwtClaims().getIssuedAt();
assertNotNull(issuedAt);
assertTrue(new Date(issuedAt).before(now));
// Validate subject + issuer
assertEquals(ADMIN_UNAME, consumer.getJwtClaims().getSubject());
assertEquals(JWT_ISSUER, consumer.getJwtClaims().getIssuer());
// Verify NotBefore
Long notBefore = consumer.getJwtClaims().getNotBefore();
assertNotNull(notBefore);
assertTrue(new Date(notBefore).before(now));
}
use of org.apache.syncope.common.rest.api.service.AccessTokenService in project syncope by apache.
the class JWTITCase method noneSignature.
@Test
public void noneSignature() throws ParseException {
// Get an initial token
SyncopeClient localClient = clientFactory.create(ADMIN_UNAME, ADMIN_PWD);
AccessTokenService accessTokenService = localClient.getService(AccessTokenService.class);
Response response = accessTokenService.login();
String token = response.getHeaderString(RESTHeaders.TOKEN);
assertNotNull(token);
JwsJwtCompactConsumer consumer = new JwsJwtCompactConsumer(token);
String tokenId = consumer.getJwtClaims().getTokenId();
// Create a new token using the Id of the first token
JwtClaims jwtClaims = new JwtClaims();
jwtClaims.setTokenId(tokenId);
jwtClaims.setSubject(consumer.getJwtClaims().getSubject());
jwtClaims.setIssuedAt(consumer.getJwtClaims().getIssuedAt());
jwtClaims.setIssuer(consumer.getJwtClaims().getIssuer());
jwtClaims.setExpiryTime(consumer.getJwtClaims().getExpiryTime());
jwtClaims.setNotBefore(consumer.getJwtClaims().getNotBefore());
JwsHeaders jwsHeaders = new JwsHeaders(JoseType.JWT, SignatureAlgorithm.NONE);
JwtToken jwtToken = new JwtToken(jwsHeaders, jwtClaims);
JwsJwtCompactProducer producer = new JwsJwtCompactProducer(jwtToken);
JwsSignatureProvider jwsSignatureProvider = new NoneJwsSignatureProvider();
String signed = producer.signWith(jwsSignatureProvider);
SyncopeClient jwtClient = clientFactory.create(signed);
UserSelfService jwtUserSelfService = jwtClient.getService(UserSelfService.class);
try {
jwtUserSelfService.read();
fail("Failure expected on no signature");
} catch (AccessControlException ex) {
// expected
}
}
use of org.apache.syncope.common.rest.api.service.AccessTokenService in project syncope by apache.
the class JWTITCase method tokenValidation.
@Test
public void tokenValidation() throws ParseException {
// Get an initial token
SyncopeClient localClient = clientFactory.create(ADMIN_UNAME, ADMIN_PWD);
AccessTokenService accessTokenService = localClient.getService(AccessTokenService.class);
Response response = accessTokenService.login();
String token = response.getHeaderString(RESTHeaders.TOKEN);
assertNotNull(token);
JwsJwtCompactConsumer consumer = new JwsJwtCompactConsumer(token);
String tokenId = consumer.getJwtClaims().getTokenId();
// Create a new token using the Id of the first token
Date now = new Date();
long currentTime = now.getTime() / 1000L;
Calendar expiry = Calendar.getInstance();
expiry.setTime(now);
expiry.add(Calendar.MINUTE, 5);
JwtClaims jwtClaims = new JwtClaims();
jwtClaims.setTokenId(tokenId);
jwtClaims.setSubject(ADMIN_UNAME);
jwtClaims.setIssuedAt(currentTime);
jwtClaims.setIssuer(JWT_ISSUER);
jwtClaims.setExpiryTime(expiry.getTime().getTime() / 1000L);
jwtClaims.setNotBefore(currentTime);
JwsHeaders jwsHeaders = new JwsHeaders(JoseType.JWT, SignatureAlgorithm.HS512);
JwtToken jwtToken = new JwtToken(jwsHeaders, jwtClaims);
JwsJwtCompactProducer producer = new JwsJwtCompactProducer(jwtToken);
JwsSignatureProvider jwsSignatureProvider = new HmacJwsSignatureProvider(JWS_KEY.getBytes(), SignatureAlgorithm.HS512);
String signed = producer.signWith(jwsSignatureProvider);
SyncopeClient jwtClient = clientFactory.create(signed);
UserSelfService jwtUserSelfService = jwtClient.getService(UserSelfService.class);
jwtUserSelfService.read();
}
Aggregations