use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project teiid by teiid.
the class SAMLBearerTokenLoginModule method login.
@Override
public boolean login() throws LoginException {
this.callerSubject = getSubject();
this.callerPrincipal = getPrincipal();
final String samlToken = getSAMLResponseToken();
if (samlToken == null) {
return false;
}
OAuth20CredentialImpl cred = new OAuth20CredentialImpl() {
protected ClientAccessToken getAccessToken() {
Consumer consumer = new Consumer(getClientId(), getClientSecret());
WebClient client = WebClient.create(getAccessTokenURI());
Saml2BearerGrant grant = null;
if (scope != null) {
grant = new Saml2BearerGrant(samlToken, scope);
} else {
grant = new Saml2BearerGrant(samlToken);
}
return OAuthClientUtils.getAccessToken(client, consumer, grant, null, false);
}
};
cred.setClientId(getClientId());
cred.setClientSecret(getClientSecret());
cred.setAccessTokenURI(getAccessTokenURI());
setCredential(cred);
return super.login();
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project ddf by codice.
the class OAuthSecurityImpl method refreshToken.
/**
* Attempts to refresh an expired access token
*
* @param id The ID to use when storing tokens
* @param sourceId The ID of the source using OAuth to use when storing tokens
* @param clientId The client ID registered with the OAuth provider
* @param clientSecret The client secret registered with the OAuth provider
* @param discoveryUrl The URL where the OAuth provider's metadata is hosted
* @param refreshToken The unexpired refresh token to use
* @param metadata The OAuh provider's metadata
* @return refreshed access token
*/
private String refreshToken(String id, String sourceId, String clientId, String clientSecret, String discoveryUrl, String refreshToken, OIDCProviderMetadata metadata) {
if (refreshToken == null || isExpired(refreshToken)) {
LOGGER.debug("Error refreshing access token: unable to find an unexpired refresh token.");
return null;
}
ClientAccessToken clientAccessToken;
try {
LOGGER.debug("Attempting to refresh the user's access token.");
WebClient webClient = createWebClient(metadata.getTokenEndpointURI());
Consumer consumer = new Consumer(clientId, clientSecret);
AccessTokenGrant accessTokenGrant = new RefreshTokenGrant(refreshToken);
clientAccessToken = OAuthClientUtils.getAccessToken(webClient, consumer, accessTokenGrant);
} catch (OAuthServiceException e) {
LOGGER.debug("Error refreshing access token.", e);
return null;
}
// Validate new access token
try {
AccessToken accessToken = convertCxfAccessTokenToNimbusdsToken(clientAccessToken);
OidcTokenValidator.validateAccessToken(accessToken, null, resourceRetriever, metadata, null);
} catch (OidcValidationException e) {
LOGGER.debug("Error validating access token.");
return null;
}
// Store new tokens
String newAccessToken = clientAccessToken.getTokenKey();
String newRefreshToken = clientAccessToken.getRefreshToken();
int status = tokenStorage.create(id, sourceId, newAccessToken, newRefreshToken, discoveryUrl);
if (status != SC_OK) {
LOGGER.warn("Error updating the token information.");
}
return newAccessToken;
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.
the class OAuthUtils method toClientAccessToken.
public static ClientAccessToken toClientAccessToken(ServerAccessToken serverToken, boolean supportOptionalParams) {
String tokenKey = serverToken.getEncodedToken() != null ? serverToken.getEncodedToken() : serverToken.getTokenKey();
ClientAccessToken clientToken = new ClientAccessToken(serverToken.getTokenType(), tokenKey);
clientToken.setRefreshToken(serverToken.getRefreshToken());
if (supportOptionalParams) {
clientToken.setExpiresIn(serverToken.getExpiresIn());
List<OAuthPermission> perms = serverToken.getScopes();
String scopeString = OAuthUtils.convertPermissionsToScope(perms);
if (!StringUtils.isEmpty(scopeString)) {
clientToken.setApprovedScope(scopeString);
}
clientToken.setParameters(new HashMap<String, String>(serverToken.getParameters()));
}
return clientToken;
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.
the class OAuthClientUtilsTest method getAccessToken.
@Test
public void getAccessToken() {
WebClient accessTokenService = mock(WebClient.class);
String tokenKey = "tokenKey";
String response = "{\"" + OAuthConstants.ACCESS_TOKEN + "\":\"" + tokenKey + "\"}";
expect(accessTokenService.form(anyObject(Form.class))).andReturn(Response.ok(new ByteArrayInputStream(response.getBytes()), MediaType.APPLICATION_JSON).build());
replay(accessTokenService);
ClientAccessToken cat = OAuthClientUtils.getAccessToken(accessTokenService, null, new RefreshTokenGrant(""), null, "defaultTokenType", false);
assertEquals(tokenKey, cat.getTokenKey());
verify(accessTokenService);
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.
the class OAuthJSONProviderTest method doReadClientAccessToken.
@SuppressWarnings({ "unchecked", "rawtypes" })
public ClientAccessToken doReadClientAccessToken(String response, String expectedTokenType, Map<String, String> expectedParams) throws Exception {
OAuthJSONProvider provider = new OAuthJSONProvider();
ClientAccessToken token = (ClientAccessToken) provider.readFrom((Class) ClientAccessToken.class, ClientAccessToken.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(response.getBytes()));
assertEquals("1234", token.getTokenKey());
assertTrue(expectedTokenType.equalsIgnoreCase(token.getTokenType()));
assertEquals("5678", token.getRefreshToken());
assertEquals(12345, token.getExpiresIn());
assertEquals("read", token.getApprovedScope());
Map<String, String> extraParams = token.getParameters();
if (expectedParams != null) {
assertEquals(expectedParams, extraParams);
}
assertEquals("http://abc", extraParams.get("my_parameter"));
return token;
}
Aggregations