use of io.jans.ca.common.response.GetClientTokenResponse in project jans by JanssenProject.
the class GetAccessTokenByRefreshTokenOperation method execute.
@Override
public IOpResponse execute(GetAccessTokenByRefreshTokenParams params) {
try {
validate(params);
final Rp rp = getRp();
final TokenClient tokenClient = new TokenClient(getDiscoveryService().getConnectDiscoveryResponse(rp).getTokenEndpoint());
tokenClient.setExecutor(getHttpService().getClientEngine());
final TokenResponse tokenResponse = tokenClient.execRefreshToken(scopeAsString(params), params.getRefreshToken(), rp.getClientId(), rp.getClientSecret());
if (tokenResponse != null) {
if (Util.allNotBlank(tokenResponse.getAccessToken())) {
GetClientTokenResponse response = new GetClientTokenResponse();
response.setAccessToken(tokenResponse.getAccessToken());
response.setExpiresIn(tokenResponse.getExpiresIn());
response.setRefreshToken(tokenResponse.getRefreshToken());
response.setScope(Utils.stringToList(tokenResponse.getScope()));
return response;
} else {
LOG.error("access_token is blank in response, params: " + params + ", response: " + tokenResponse);
LOG.error("Please check AS logs for more details (oxauth.log for CE).");
}
} else {
LOG.error("No response from TokenClient");
}
} catch (HttpException e) {
throw e;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
throw HttpException.internalError();
}
use of io.jans.ca.common.response.GetClientTokenResponse in project jans by JanssenProject.
the class GetClientTokenOperation method execute.
@Override
public IOpResponse execute(GetClientTokenParams params) {
try {
final AuthenticationMethod authenticationMethod = AuthenticationMethod.fromString(params.getAuthenticationMethod());
final String tokenEndpoint = getDiscoveryService().getConnectDiscoveryResponse(params.getOpConfigurationEndpoint(), params.getOpHost(), params.getOpDiscoveryPath()).getTokenEndpoint();
final TokenClient tokenClient = getOpClientFactory().createTokenClient(tokenEndpoint);
tokenClient.setExecutor(getHttpService().getClientEngine());
final TokenResponse tokenResponse;
if (authenticationMethod == AuthenticationMethod.PRIVATE_KEY_JWT) {
LOG.trace("Getting client token with private_key_jwt client authentication ...");
SignatureAlgorithm algorithm = SignatureAlgorithm.fromString(params.getAlgorithm());
if (algorithm == null) {
throw new HttpException(ErrorResponseCode.INVALID_SIGNATURE_ALGORITHM);
}
TokenRequest tokenRequest = new TokenRequest(GrantType.CLIENT_CREDENTIALS);
tokenRequest.setScope(scopeAsString(params));
tokenRequest.setAuthUsername(params.getClientId());
tokenRequest.setAuthenticationMethod(AuthenticationMethod.PRIVATE_KEY_JWT);
tokenRequest.setAlgorithm(algorithm);
tokenRequest.setCryptoProvider(getCryptoProvider());
tokenRequest.setKeyId(params.getKeyId());
tokenRequest.setAudience(tokenEndpoint);
tokenClient.setRequest(tokenRequest);
tokenResponse = tokenClient.exec();
} else {
tokenResponse = tokenClient.execClientCredentialsGrant(scopeAsString(params), params.getClientId(), params.getClientSecret());
}
if (tokenResponse != null) {
if (Util.allNotBlank(tokenResponse.getAccessToken())) {
GetClientTokenResponse response = new GetClientTokenResponse();
response.setAccessToken(tokenResponse.getAccessToken());
response.setExpiresIn(tokenResponse.getExpiresIn());
response.setRefreshToken(tokenResponse.getRefreshToken());
response.setScope(Utils.stringToList(tokenResponse.getScope()));
return response;
} else {
LOG.error("access_token is blank in response, params: " + params + ", response: " + tokenResponse);
LOG.error("Please check AS logs for more details (oxauth.log for CE).");
}
} else {
LOG.error("No response from TokenClient");
LOG.error("Please check AS logs for more details (oxauth.log for CE).");
}
} catch (HttpException e) {
throw e;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
throw HttpException.internalError();
}
use of io.jans.ca.common.response.GetClientTokenResponse in project jans by JanssenProject.
the class GetClientTokenTest method getClientToken.
@Parameters({ "host", "opHost" })
@Test
public void getClientToken(String host, String opHost) {
final GetClientTokenParams params = new GetClientTokenParams();
params.setOpHost(opHost);
params.setScope(Lists.newArrayList("openid"));
params.setClientId(Tester.getSetupClient().getClientId());
params.setClientSecret(Tester.getSetupClient().getClientSecret());
GetClientTokenResponse resp = Tester.newClient(host).getClientToken(params);
assertNotNull(resp);
notEmpty(resp.getAccessToken());
}
use of io.jans.ca.common.response.GetClientTokenResponse in project jans by JanssenProject.
the class IntrospectAccessTokenTest method introspectAccessToken.
@Parameters({ "host", "opHost", "redirectUrls" })
@Test
public void introspectAccessToken(String host, String opHost, String redirectUrls) {
ClientInterface client = Tester.newClient(host);
RegisterSiteResponse setupResponse = SetupClientTest.setupClient(client, opHost, redirectUrls);
final GetClientTokenParams params = new GetClientTokenParams();
params.setOpHost(opHost);
params.setScope(Lists.newArrayList("openid", "jans_client_api"));
params.setClientId(setupResponse.getClientId());
params.setClientSecret(setupResponse.getClientSecret());
GetClientTokenResponse tokenResponse = client.getClientToken(params);
assertNotNull(tokenResponse);
notEmpty(tokenResponse.getAccessToken());
IntrospectAccessTokenParams introspectParams = new IntrospectAccessTokenParams();
introspectParams.setRpId(setupResponse.getRpId());
introspectParams.setAccessToken(tokenResponse.getAccessToken());
IntrospectAccessTokenResponse introspectionResponse = client.introspectAccessToken("Bearer " + tokenResponse.getAccessToken(), null, introspectParams);
assertNotNull(introspectionResponse);
assertTrue(introspectionResponse.isActive());
assertNotNull(introspectionResponse.getIssuedAt());
assertNotNull(introspectionResponse.getExpiresAt());
assertTrue(introspectionResponse.getExpiresAt() >= introspectionResponse.getIssuedAt());
}
use of io.jans.ca.common.response.GetClientTokenResponse in project jans by JanssenProject.
the class Tester method getAuthorization.
public static String getAuthorization() {
Preconditions.checkNotNull(SETUP_CLIENT);
if (Strings.isNullOrEmpty(AUTHORIZATION)) {
final GetClientTokenParams params = new GetClientTokenParams();
params.setOpHost(OP_HOST);
params.setScope(Lists.newArrayList("openid"));
params.setClientId(Tester.getSetupClient().getClientId());
params.setClientSecret(Tester.getSetupClient().getClientSecret());
GetClientTokenResponse resp = Tester.newClient(HOST).getClientToken(params);
assertNotNull(resp);
assertTrue(!Strings.isNullOrEmpty(resp.getAccessToken()));
AUTHORIZATION = "Bearer " + resp.getAccessToken();
}
return AUTHORIZATION;
}
Aggregations