use of com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest in project hub-alert by blackducksoftware.
the class AzureAuthorizationCodeFlowTest method builderTest.
@Test
public void builderTest() {
AzureAuthorizationCodeFlow.Builder builder = new AzureAuthorizationCodeFlow.Builder(method, httpTransport, jsonFactory, genericUrl, clientAuthentication, clientId, authorizationServerEncodedUrl, null, null);
assertNull(builder.getClientSecret());
assertNull(builder.getRedirectUri());
builder.setClientSecret(clientSecret);
builder.setRedirectUri(redirectUri);
assertEquals(clientSecret, builder.getClientSecret());
assertEquals(redirectUri, builder.getRedirectUri());
AuthorizationCodeFlow azureAuthorizationCodeFlow = builder.build();
AuthorizationCodeTokenRequest tokenRequest = azureAuthorizationCodeFlow.newTokenRequest(authorizationCode);
testAuthorizationCodeTokenRequest(tokenRequest);
}
use of com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest in project hub-alert by blackducksoftware.
the class AzureAuthorizationCodeFlowTest method newTokenRequestTest.
@Test
public void newTokenRequestTest() {
AzureAuthorizationCodeFlow azureAuthorizationCodeFlow = new AzureAuthorizationCodeFlow(method, httpTransport, jsonFactory, genericUrl, clientAuthentication, clientId, authorizationServerEncodedUrl, clientSecret, redirectUri);
AuthorizationCodeTokenRequest tokenRequest = azureAuthorizationCodeFlow.newTokenRequest(authorizationCode);
assertEquals(authorizationCode, tokenRequest.getCode());
assertEquals(AzureOAuthConstants.DEFAULT_GRANT_TYPE, tokenRequest.getGrantType());
assertEquals(redirectUri, tokenRequest.getRedirectUri());
assertEquals("", tokenRequest.getScopes());
assertEquals(authorizationCode, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_ASSERTION));
assertEquals(AzureOAuthConstants.DEFAULT_CLIENT_ASSERTION_TYPE, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_CLIENT_ASSERTION_TYPE));
assertEquals(clientSecret, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_CLIENT_ASSERTION));
assertEquals(redirectUri, tokenRequest.get(AzureOAuthConstants.REQUEST_BODY_FIELD_REDIRECT_URI));
}
use of com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest in project idempiere by idempiere.
the class MAuthorizationCredential method processToken.
/**
* Create or Update an Account based on the token received
* @param ctx
* @param code
* @param paramScope
* @param pilog MPInstanceLog to set the log message and record_ID, it is not saved, the caller must save it
* @return String message indicating success
*/
public String processToken(Properties ctx, String code, String paramScope, MPInstanceLog pilog) {
String msg = null;
try {
String clientId = getAuthorizationClientId();
String clientSecret = getAuthorizationClientSecret();
Timestamp ts = new Timestamp(System.currentTimeMillis());
MAuthorizationProvider ap = new MAuthorizationProvider(ctx, getAD_AuthorizationProvider_ID(), get_TrxName());
AuthorizationCodeTokenRequest request = new AuthorizationCodeTokenRequest(new NetHttpTransport(), GsonFactory.getDefaultInstance(), new GenericUrl(ap.getTokenEndpoint()), code);
request.setRedirectUri(getAuthorizationRedirectURL());
request.setClientAuthentication(new ClientParametersAuthentication(clientId, clientSecret));
TokenResponse tokenResponse = request.execute();
Object id_token = tokenResponse.get("id_token");
String email = null;
if (id_token != null && id_token instanceof String) {
IdToken idtoken = IdToken.parse(tokenResponse.getFactory(), (String) tokenResponse.get("id_token"));
email = (String) idtoken.getPayload().get("email");
}
if (email == null) {
msg = Msg.parseTranslation(ctx, "@Error@ @OAuthProcessToken_CouldNotGetEMail@");
return msg;
}
boolean newAccount = false;
MAuthorizationAccount account = null;
Query query = new Query(ctx, MAuthorizationAccount.Table_Name, "AD_Client_ID=? AND AD_User_ID=? AND EMail=? AND AD_AuthorizationCredential_ID=?", get_TrxName());
query.setParameters(Env.getAD_Client_ID(ctx), Env.getAD_User_ID(ctx), email, getAD_AuthorizationCredential_ID());
account = query.setOnlyActiveRecords(true).first();
if (account == null) {
account = new MAuthorizationAccount(ctx, 0, get_TrxName());
account.setEMail(email);
account.setAD_AuthorizationCredential_ID(getAD_AuthorizationCredential_ID());
account.setAD_User_ID(Env.getAD_User_ID(ctx));
newAccount = true;
}
account.setAD_AuthorizationScopes(paramScope);
account.setAccessToken(tokenResponse.getAccessToken());
account.setAccessTokenTimestamp(ts);
account.setExpireInSeconds(BigDecimal.valueOf(tokenResponse.getExpiresInSeconds()));
account.setIsAuthorized(true);
account.setIsActive(true);
if (tokenResponse.getRefreshToken() == null && account.getRefreshToken() == null) {
String refreshToken = account.findRefreshToken();
if (refreshToken != null) {
account.setRefreshToken(refreshToken);
}
}
if (tokenResponse.getRefreshToken() == null && account.getRefreshToken() == null) {
// revoke access and ask for retry
MAuthorizationProvider provider = new MAuthorizationProvider(ctx, getAD_AuthorizationProvider_ID(), get_TrxName());
String revokeEndPoint = provider.getRevokeEndpoint();
if (revokeEndPoint != null) {
HttpRequestFactory factory = new NetHttpTransport().createRequestFactory();
GenericUrl url = new GenericUrl(revokeEndPoint + "?token=" + account.getAccessToken());
HttpRequest revokeRequest = factory.buildGetRequest(url);
revokeRequest.execute();
}
msg = Msg.parseTranslation(ctx, "@Error@ @OAuthProcessToken_NoRefreshToken@");
return msg;
}
if (tokenResponse.getRefreshToken() != null) {
account.setRefreshToken(tokenResponse.getRefreshToken());
}
account.saveEx();
if (pilog != null) {
String logmsg = Msg.parseTranslation(ctx, (newAccount ? "@Created@" : "@Updated@") + " @AD_AuthorizationAccount_ID@ for ") + account.getEMail();
pilog.setP_Msg(logmsg);
pilog.setRecord_ID(account.getAD_AuthorizationAccount_ID());
}
account.syncOthers();
if (newAccount)
msg = Msg.getMsg(ctx, "Authorization_Access_OK", new Object[] { account.getEMail(), paramScope });
else
msg = Msg.getMsg(ctx, "Authorization_Access_Previous", new Object[] { account.getEMail(), paramScope });
} catch (Exception ex) {
ex.printStackTrace();
msg = Msg.getMsg(ctx, "Error") + ex.getLocalizedMessage();
return msg;
}
return msg;
}
use of com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest in project isaac-api by isaacphysics.
the class FacebookAuthenticator method exchangeCode.
@Override
public String exchangeCode(final String authorizationCode) throws CodeExchangeException {
try {
AuthorizationCodeTokenRequest request = new AuthorizationCodeTokenRequest(httpTransport, jsonFactory, new GenericUrl(TOKEN_EXCHANGE_URL), authorizationCode);
request.setClientAuthentication(new ClientParametersAuthentication(clientId, clientSecret));
request.setRedirectUri(callbackUri);
TokenResponse response = request.execute();
String accessToken;
Long expires;
if (response.get("error") != null) {
throw new CodeExchangeException("Server responded with the following error" + response.get("error") + " given the request" + request.toString());
}
if (response.getAccessToken() != null && response.getExpiresInSeconds() != null) {
accessToken = response.getAccessToken();
expires = response.getExpiresInSeconds();
} else {
throw new IOException("access_token or expires_in values were not found");
}
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setAccessToken(accessToken);
tokenResponse.setExpiresInSeconds(expires);
// I don't really want to use the flow storage but it seems to be
// easier to get credentials this way.
Builder builder = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), httpTransport, jsonFactory, new GenericUrl(TOKEN_EXCHANGE_URL), new ClientParametersAuthentication(clientId, clientSecret), clientId, AUTH_URL);
builder.setScopes(requestedScopes);
AuthorizationCodeFlow flow = builder.setDataStoreFactory(MemoryDataStoreFactory.getDefaultInstance()).build();
Credential credential = flow.createAndStoreCredential(tokenResponse, authorizationCode);
String internalReferenceToken = UUID.randomUUID().toString();
credentialStore.put(internalReferenceToken, credential);
flow.getCredentialDataStore().clear();
return internalReferenceToken;
} catch (IOException e) {
String message = "An error occurred during code exchange";
throw new CodeExchangeException(message, e);
}
}
use of com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest in project blackduck-alert by blackducksoftware.
the class AzureBoardsProperties method requestTokens.
public Optional<Credential> requestTokens(AuthorizationCodeFlow authorizationCodeFlow, String authorizationCode) throws IOException {
AuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(authorizationCode);
TokenResponse tokenResponse = tokenRequest.execute();
Credential credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, oauthUserId);
return Optional.ofNullable(credential);
}
Aggregations