Search in sources :

Example 1 with OAuthToken

use of com.google.gerrit.extensions.auth.oauth.OAuthToken in project gerrit by GerritCodeReview.

the class OAuthSession method login.

boolean login(HttpServletRequest request, HttpServletResponse response, OAuthServiceProvider oauth) throws IOException {
    log.debug("Login " + this);
    if (isOAuthFinal(request)) {
        if (!checkState(request)) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return false;
        }
        log.debug("Login-Retrieve-User " + this);
        OAuthToken token = oauth.getAccessToken(new OAuthVerifier(request.getParameter("code")));
        user = oauth.getUserInfo(token);
        if (isLoggedIn()) {
            log.debug("Login-SUCCESS " + this);
            authenticateAndRedirect(request, response, token);
            return true;
        }
        response.sendError(SC_UNAUTHORIZED);
        return false;
    }
    log.debug("Login-PHASE1 " + this);
    redirectToken = request.getRequestURI();
    // We are here in content of filter.
    // Due to this Jetty limitation:
    // https://bz.apache.org/bugzilla/show_bug.cgi?id=28323
    // we cannot use LoginUrlToken.getToken() method,
    // because it relies on getPathInfo() and it is always null here.
    redirectToken = redirectToken.substring(request.getContextPath().length());
    response.sendRedirect(oauth.getAuthorizationUrl() + "&state=" + state);
    return false;
}
Also used : OAuthToken(com.google.gerrit.extensions.auth.oauth.OAuthToken) OAuthVerifier(com.google.gerrit.extensions.auth.oauth.OAuthVerifier)

Example 2 with OAuthToken

use of com.google.gerrit.extensions.auth.oauth.OAuthToken in project gerrit by GerritCodeReview.

the class GetOAuthToken method apply.

@Override
public OAuthTokenInfo apply(AccountResource rsrc) throws AuthException, ResourceNotFoundException {
    if (self.get() != rsrc.getUser()) {
        throw new AuthException("not allowed to get access token");
    }
    Account a = rsrc.getUser().getAccount();
    OAuthToken accessToken = tokenCache.get(a.getId());
    if (accessToken == null) {
        throw new ResourceNotFoundException();
    }
    OAuthTokenInfo accessTokenInfo = new OAuthTokenInfo();
    accessTokenInfo.username = a.getUserName();
    accessTokenInfo.resourceHost = hostName;
    accessTokenInfo.accessToken = accessToken.getToken();
    accessTokenInfo.providerId = accessToken.getProviderId();
    accessTokenInfo.expiresAt = Long.toString(accessToken.getExpiresAt());
    accessTokenInfo.type = BEARER_TYPE;
    return accessTokenInfo;
}
Also used : OAuthToken(com.google.gerrit.extensions.auth.oauth.OAuthToken) Account(com.google.gerrit.reviewdb.client.Account) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 3 with OAuthToken

use of com.google.gerrit.extensions.auth.oauth.OAuthToken in project gerrit by GerritCodeReview.

the class OAuthTokenCache method get.

public OAuthToken get(Account.Id id) {
    OAuthToken accessToken = cache.getIfPresent(id);
    if (accessToken == null) {
        return null;
    }
    accessToken = decrypt(accessToken);
    if (accessToken.isExpired()) {
        cache.invalidate(id);
        return null;
    }
    return accessToken;
}
Also used : OAuthToken(com.google.gerrit.extensions.auth.oauth.OAuthToken)

Aggregations

OAuthToken (com.google.gerrit.extensions.auth.oauth.OAuthToken)3 OAuthVerifier (com.google.gerrit.extensions.auth.oauth.OAuthVerifier)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 Account (com.google.gerrit.reviewdb.client.Account)1