Search in sources :

Example 6 with AuthorizationCodeFlow

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow in project data-transfer-project by google.

the class MicrosoftAuth method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, AuthData initialAuthData, String extra) throws IOException {
    Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected for MS oauth flow");
    Preconditions.checkArgument(initialAuthData == null, "Earlier auth data not expected for MS oauth flow");
    AuthorizationCodeFlow flow = createFlow();
    TokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
    callbackBaseUrl + CALLBACK_PATH).execute();
    // Figure out storage
    Credential credential = flow.createAndStoreCredential(response, jobId.toString());
    // GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
    return toAuthData(credential);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) AuthorizationCodeInstalledAppSecureOverride(org.dataportabilityproject.shared.auth.AuthorizationCodeInstalledAppSecureOverride)

Example 7 with AuthorizationCodeFlow

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow in project data-transfer-project by google.

the class MicrosoftAuth method getAuthData.

/**
 * Initiates the auth flow and obtains the access token.
 */
private MicrosoftOauthData getAuthData(String account) throws IOException {
    // set up authorization code flow
    AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(// Access Method
    BearerToken.authorizationHeaderAccessMethod(), // HttpTransport
    MicrosoftStaticObjects.getHttpTransport(), // JsonFactory
    JSON_FACTORY, // GenericUrl
    new GenericUrl(TOKEN_SERVER_URL), new ClientParametersAuthentication(appCredentials.key(), appCredentials.secret()), // clientId
    appCredentials.key(), // encoded authUrl
    AUTHORIZATION_SERVER_URL).setScopes(// scopes
    scopes).setDataStoreFactory(MicrosoftStaticObjects.getDataStoreFactory()).build();
    // authorize
    // NOTE: This requires an https endpoint wired to
    // forward requests to http://domain:port/Callback
    VerificationCodeReceiver receiver = new LocalServerReceiver.Builder().setHost(DOMAIN).setPort(PORT).build();
    try {
        Credential credential = new AuthorizationCodeInstalledAppSecureOverride(flow, receiver).authorize(account);
        return toAuthData(credential);
    } catch (Exception e) {
        throw new IOException("Couldn't authorize", e);
    }
}
Also used : AuthorizationCodeInstalledAppSecureOverride(org.dataportabilityproject.shared.auth.AuthorizationCodeInstalledAppSecureOverride) ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) Credential(com.google.api.client.auth.oauth2.Credential) VerificationCodeReceiver(com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver) GenericUrl(com.google.api.client.http.GenericUrl) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) IOException(java.io.IOException) IOException(java.io.IOException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow)

Example 8 with AuthorizationCodeFlow

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow in project hub-alert by blackducksoftware.

the class AzureBoardsProperties method hasOAuthCredentials.

public boolean hasOAuthCredentials(ProxyInfo proxy) {
    HttpTransport httpTransport = createHttpTransport(proxy);
    try {
        AuthorizationCodeFlow oAuthFlow = createOAuthFlow(httpTransport);
        Optional<Credential> oAuthCredential = getExistingOAuthCredential(oAuthFlow);
        return oAuthCredential.isPresent();
    } catch (IOException e) {
        return false;
    }
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) ApacheHttpTransport(com.google.api.client.http.apache.v2.ApacheHttpTransport) Credential(com.google.api.client.auth.oauth2.Credential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) IOException(java.io.IOException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) AzureAuthorizationCodeFlow(com.synopsys.integration.azure.boards.common.oauth.AzureAuthorizationCodeFlow)

Example 9 with AuthorizationCodeFlow

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow in project hub-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);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeTokenRequest(com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest)

Example 10 with AuthorizationCodeFlow

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow in project hub-alert by blackducksoftware.

the class AzureBoardsProperties method createAzureHttpRequestCreator.

public AzureHttpRequestCreator createAzureHttpRequestCreator(ProxyInfo proxyInfo, Gson gson) throws AlertException {
    HttpTransport httpTransport = createHttpTransport(proxyInfo);
    try {
        AuthorizationCodeFlow oAuthFlow = createOAuthFlow(httpTransport);
        Credential oAuthCredential = getExistingOAuthCredential(oAuthFlow).orElseThrow(() -> new AlertException(String.format("No existing Azure OAuth credential associated with '%s'", oauthUserId)));
        return AzureHttpRequestCreatorFactory.withCredential(httpTransport, oAuthCredential, gson);
    } catch (IOException e) {
        throw new AlertException("Cannot read OAuth credentials", e);
    }
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) ApacheHttpTransport(com.google.api.client.http.apache.v2.ApacheHttpTransport) Credential(com.google.api.client.auth.oauth2.Credential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) IOException(java.io.IOException) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) AzureAuthorizationCodeFlow(com.synopsys.integration.azure.boards.common.oauth.AzureAuthorizationCodeFlow)

Aggregations

AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)12 Credential (com.google.api.client.auth.oauth2.Credential)10 IOException (java.io.IOException)6 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)5 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)4 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)4 VerificationCodeReceiver (com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver)3 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)3 GenericUrl (com.google.api.client.http.GenericUrl)3 HttpTransport (com.google.api.client.http.HttpTransport)3 ApacheHttpTransport (com.google.api.client.http.apache.v2.ApacheHttpTransport)3 AzureAuthorizationCodeFlow (com.synopsys.integration.azure.boards.common.oauth.AzureAuthorizationCodeFlow)3 AuthorizationCodeTokenRequest (com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest)2 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)2 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)2 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)2 AuthorizationCodeInstalledAppSecureOverride (org.dataportabilityproject.shared.auth.AuthorizationCodeInstalledAppSecureOverride)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 AzureHttpRequestCreator (com.synopsys.integration.azure.boards.common.http.AzureHttpRequestCreator)1