com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl

Here are the examples of the java api class com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl taken from open source projects.

1. OAuthAuthenticator#callback()

Project: che
Source File: OAuthAuthenticator.java
View license
/**
     * 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());
    }
}

2. AbstractAuthorizationCodeCallbackServlet#doGet()

View license
@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    StringBuffer buf = req.getRequestURL();
    if (req.getQueryString() != null) {
        buf.append('?').append(req.getQueryString());
    }
    AuthorizationCodeResponseUrl responseUrl = new AuthorizationCodeResponseUrl(buf.toString());
    String code = responseUrl.getCode();
    if (responseUrl.getError() != null) {
        onError(req, resp, responseUrl);
    } else if (code == null) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        resp.getWriter().print("Missing authorization code");
    } else {
        lock.lock();
        try {
            if (flow == null) {
                flow = initializeFlow();
            }
            String redirectUri = getRedirectUri(req);
            TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
            String userId = getUserId(req);
            Credential credential = flow.createAndStoreCredential(response, userId);
            onSuccess(req, resp, credential);
        } finally {
            lock.unlock();
        }
    }
}