Search in sources :

Example 6 with TokenResponse

use of com.google.api.client.auth.oauth2.TokenResponse in project repairnator by Spirals-Team.

the class ManageGoogleAccessToken method initializeCredentialFromAccessToken.

public void initializeCredentialFromAccessToken(String accessToken) {
    TokenResponse tokenResponse = new TokenResponse();
    tokenResponse.setAccessToken(accessToken);
    tokenResponse.setTokenType("offline");
    this.credential = new GoogleCredential().setFromTokenResponse(tokenResponse);
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Example 7 with TokenResponse

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

the class GoogleCredential method executeRefreshToken.

@Override
@Beta
protected TokenResponse executeRefreshToken() throws IOException {
    if (serviceAccountPrivateKey == null) {
        return super.executeRefreshToken();
    }
    // service accounts: no refresh token; instead use private key to request new access token
    JsonWebSignature.Header header = new JsonWebSignature.Header();
    header.setAlgorithm("RS256");
    header.setType("JWT");
    header.setKeyId(serviceAccountPrivateKeyId);
    JsonWebToken.Payload payload = new JsonWebToken.Payload();
    long currentTime = getClock().currentTimeMillis();
    payload.setIssuer(serviceAccountId);
    payload.setAudience(getTokenServerEncodedUrl());
    payload.setIssuedAtTimeSeconds(currentTime / 1000);
    payload.setExpirationTimeSeconds(currentTime / 1000 + 3600);
    payload.setSubject(serviceAccountUser);
    payload.put("scope", Joiner.on(' ').join(serviceAccountScopes));
    try {
        String assertion = JsonWebSignature.signUsingRsaSha256(serviceAccountPrivateKey, getJsonFactory(), header, payload);
        TokenRequest request = new TokenRequest(getTransport(), getJsonFactory(), new GenericUrl(getTokenServerEncodedUrl()), "urn:ietf:params:oauth:grant-type:jwt-bearer");
        request.put("assertion", assertion);
        return request.execute();
    } catch (GeneralSecurityException exception) {
        IOException e = new IOException();
        e.initCause(exception);
        throw e;
    }
}
Also used : JsonWebSignature(com.google.api.client.json.webtoken.JsonWebSignature) GeneralSecurityException(java.security.GeneralSecurityException) TokenRequest(com.google.api.client.auth.oauth2.TokenRequest) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) JsonWebToken(com.google.api.client.json.webtoken.JsonWebToken) Beta(com.google.api.client.util.Beta)

Example 8 with TokenResponse

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

the class CloudShellCredential method executeRefreshToken.

@Override
protected TokenResponse executeRefreshToken() throws IOException {
    Socket socket = new Socket("localhost", this.getAuthPort());
    socket.setSoTimeout(READ_TIMEOUT_MS);
    TokenResponse token = new TokenResponse();
    try {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(GET_AUTH_TOKEN_REQUEST);
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        // Ignore the size line
        input.readLine();
        Collection<Object> messageArray = jsonFactory.createJsonParser(input).parseArray(LinkedList.class, Object.class);
        String accessToken = ((List<Object>) messageArray).get(ACCESS_TOKEN_INDEX).toString();
        token.setAccessToken(accessToken);
    } finally {
        socket.close();
    }
    return token;
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Example 9 with TokenResponse

use of com.google.api.client.auth.oauth2.TokenResponse in project fess by codelibs.

the class OpenIdConnectAuthenticator method processCallback.

protected LoginCredential processCallback(final HttpServletRequest request, final String code) {
    try {
        final TokenResponse tr = getTokenUrl(code);
        final String[] jwt = ((String) tr.get("id_token")).split("\\.");
        final String jwtHeader = new String(Base64.decodeBase64(jwt[0]), Constants.UTF_8_CHARSET);
        final String jwtClaim = new String(Base64.decodeBase64(jwt[1]), Constants.UTF_8_CHARSET);
        final String jwtSigniture = new String(Base64.decodeBase64(jwt[2]), Constants.UTF_8_CHARSET);
        if (logger.isDebugEnabled()) {
            logger.debug("jwtHeader: {}", jwtHeader);
            logger.debug("jwtClaim: {}", jwtClaim);
            logger.debug("jwtSigniture: {}", jwtSigniture);
        }
        // TODO validate signiture
        final Map<String, Object> attributes = new HashMap<>();
        attributes.put("accesstoken", tr.getAccessToken());
        attributes.put("refreshtoken", tr.getRefreshToken() == null ? "null" : tr.getRefreshToken());
        attributes.put("tokentype", tr.getTokenType());
        attributes.put("expire", tr.getExpiresInSeconds());
        attributes.put("jwtheader", jwtHeader);
        attributes.put("jwtclaim", jwtClaim);
        attributes.put("jwtsign", jwtSigniture);
        if (logger.isDebugEnabled()) {
            logger.debug("attribute: {}", attributes);
        }
        parseJwtClaim(jwtClaim, attributes);
        return new OpenIdConnectCredential(attributes);
    } catch (final IOException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process callbacked request.", e);
        }
    }
    return null;
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) OpenIdConnectCredential(org.codelibs.fess.app.web.base.login.OpenIdConnectCredential) HashMap(java.util.HashMap) IOException(java.io.IOException)

Example 10 with TokenResponse

use of com.google.api.client.auth.oauth2.TokenResponse 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)

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