Search in sources :

Example 1 with TokenResponse

use of com.google.api.client.auth.oauth2.TokenResponse in project che by eclipse.

the class OAuthAuthenticator method callback.

/**
     * Process callback request.
     *
     * @param requestUrl
     *         request URI. URI should contain authorization code generated by authorization server
     * @param scopes
     *         specify exactly what type of access needed. This list must be exactly the same as list passed to the method
     *         {@link #getAuthenticateUrl(URL, java.util.List)}
     * @return id of authenticated user
     * @throws OAuthAuthenticationException
     *         if authentication failed or <code>requestUrl</code> does not contain required parameters, e.g. 'code'
     */
public String callback(URL requestUrl, List<String> scopes) throws OAuthAuthenticationException {
    if (!isConfigured()) {
        throw new OAuthAuthenticationException("Authenticator is not configured");
    }
    AuthorizationCodeResponseUrl authorizationCodeResponseUrl = new AuthorizationCodeResponseUrl(requestUrl.toString());
    final String error = authorizationCodeResponseUrl.getError();
    if (error != null) {
        throw new OAuthAuthenticationException("Authentication failed: " + error);
    }
    final String code = authorizationCodeResponseUrl.getCode();
    if (code == null) {
        throw new OAuthAuthenticationException("Missing authorization code. ");
    }
    try {
        TokenResponse tokenResponse = flow.newTokenRequest(code).setRequestInitializer(request -> {
            if (request.getParser() == null) {
                request.setParser(flow.getJsonFactory().createJsonObjectParser());
            }
            request.getHeaders().setAccept(MediaType.APPLICATION_JSON);
        }).setRedirectUri(findRedirectUrl(requestUrl)).setScopes(scopes).execute();
        String userId = getUserFromUrl(authorizationCodeResponseUrl);
        if (userId == null) {
            userId = getUser(newDto(OAuthToken.class).withToken(tokenResponse.getAccessToken())).getId();
        }
        flow.createAndStoreCredential(tokenResponse, userId);
        return userId;
    } catch (IOException ioe) {
        throw new OAuthAuthenticationException(ioe.getMessage());
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Arrays(java.util.Arrays) URLDecoder(java.net.URLDecoder) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) MediaType(javax.ws.rs.core.MediaType) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) JsonParseException(org.eclipse.che.commons.json.JsonParseException) Map(java.util.Map) GenericUrl(com.google.api.client.http.GenericUrl) JsonHelper(org.eclipse.che.commons.json.JsonHelper) Credential(com.google.api.client.auth.oauth2.Credential) URI(java.net.URI) AuthorizationCodeRequestUrl(com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) Logger(org.slf4j.Logger) User(org.eclipse.che.security.oauth.shared.User) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) BearerToken(com.google.api.client.auth.oauth2.BearerToken) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) IOException(java.io.IOException) List(java.util.List) AuthorizationCodeResponseUrl(com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl) ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) MemoryDataStoreFactory(com.google.api.client.util.store.MemoryDataStoreFactory) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) InputStream(java.io.InputStream) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeResponseUrl(com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl) IOException(java.io.IOException)

Example 2 with TokenResponse

use of com.google.api.client.auth.oauth2.TokenResponse in project OsmAnd-tools by osmandapp.

the class UpdateSubscriptionImpl method getPublisherApi.

private static AndroidPublisher getPublisherApi(String file) throws JSONException, IOException {
    Properties properties = new Properties();
    properties.load(new FileInputStream(file));
    GOOGLE_CLIENT_CODE = properties.getProperty("GOOGLE_CLIENT_CODE");
    GOOGLE_CLIENT_ID = properties.getProperty("GOOGLE_CLIENT_ID");
    GOOGLE_CLIENT_SECRET = properties.getProperty("GOOGLE_CLIENT_SECRET");
    GOOGLE_REDIRECT_URI = properties.getProperty("GOOGLE_REDIRECT_URI");
    TOKEN = properties.getProperty("TOKEN");
    // getRefreshToken();
    String token = TOKEN;
    String accessToken = getAccessToken(token);
    TokenResponse tokenResponse = new TokenResponse();
    // System.out.println("refresh token=" + token);
    // System.out.println("access token=" + accessToken);
    tokenResponse.setAccessToken(accessToken);
    tokenResponse.setRefreshToken(token);
    tokenResponse.setExpiresInSeconds(3600L);
    tokenResponse.setScope("https://www.googleapis.com/auth/androidpublisher");
    tokenResponse.setTokenType("Bearer");
    HttpRequestInitializer credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setClientSecrets(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET).build().setFromTokenResponse(tokenResponse);
    AndroidPublisher publisher = new AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(GOOGLE_PRODUCT_NAME).build();
    return publisher;
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 3 with TokenResponse

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

the class GoogleAuthDataGenerator method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
    AuthorizationCodeFlow flow;
    TokenResponse response;
    try {
        flow = createFlow();
        response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
        callbackBaseUrl + redirectPath).execute();
    } catch (IOException e) {
        throw new RuntimeException("Error calling AuthorizationCodeFlow.execute ", e);
    }
    // Figure out storage
    Credential credential = null;
    try {
        credential = flow.createAndStoreCredential(response, id);
    } catch (IOException e) {
        throw new RuntimeException("Error calling AuthorizationCodeFlow.createAndStoreCredential ", e);
    }
    // GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
    return new TokensAndUrlAuthData(credential.getAccessToken(), credential.getRefreshToken(), credential.getTokenServerEncodedUrl());
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) IOException(java.io.IOException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)

Example 4 with TokenResponse

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

the class GoogleAuth method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, @Nullable AuthData initialAuthData, @Nullable String extra) throws IOException {
    Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google 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) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)

Example 5 with TokenResponse

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

the class InstagramAuth 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 Instagram oauth flow");
    Preconditions.checkArgument(initialAuthData == null, "Earlier auth data not expected for Instagram 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());
    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)

Aggregations

TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)12 Credential (com.google.api.client.auth.oauth2.Credential)8 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)5 GenericUrl (com.google.api.client.http.GenericUrl)4 IOException (java.io.IOException)4 AuthorizationCodeRequestUrl (com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl)2 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)2 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 HashMap (java.util.HashMap)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AuthorizationCodeResponseUrl (com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl)1 AuthorizationCodeTokenRequest (com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest)1 BearerToken (com.google.api.client.auth.oauth2.BearerToken)1 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)1 RefreshTokenRequest (com.google.api.client.auth.oauth2.RefreshTokenRequest)1 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)1 TokenRequest (com.google.api.client.auth.oauth2.TokenRequest)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1