Search in sources :

Example 16 with OAuthAccessor

use of net.oauth.OAuthAccessor in project zm-mailbox by Zimbra.

the class OAuthAuthorizationServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    LOG.debug("Authorization Handler doGet requested!");
    try {
        OAuthMessage oAuthMessage = OAuthServlet.getMessage(request, null);
        OAuthAccessor accessor = OAuthServiceProvider.getAccessor(oAuthMessage);
        if (Boolean.TRUE.equals(accessor.getProperty("authorized"))) {
            // already authorized send the user back
            returnToConsumer(request, response, accessor);
        } else {
            sendToAuthorizePage(request, response, accessor);
        }
    } catch (Exception e) {
        OAuthServiceProvider.handleException(e, request, response, true);
    }
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) OAuthMessage(net.oauth.OAuthMessage) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 17 with OAuthAccessor

use of net.oauth.OAuthAccessor in project zm-mailbox by Zimbra.

the class OAuthTokenCache method get.

public static OAuthAccessor get(String consumer_token, String token_type) throws ServiceException {
    String key_prefix = null;
    if (token_type == OAuthTokenCache.ACCESS_TOKEN_TYPE) {
        key_prefix = OAuthTokenCacheKey.ACCESS_TOKEN_PREFIX;
    } else if (token_type == OAuthTokenCache.REQUEST_TOKEN_TYPE) {
        key_prefix = OAuthTokenCacheKey.REQUEST_TOKEN_PREFIX;
    }
    OAuthTokenCacheKey key = new OAuthTokenCacheKey(consumer_token, key_prefix);
    LOG.debug("get type: " + token_type + " token from memcache with key: " + key.getKeyPrefix() + key.getKeyValue() + ".");
    OAuthAccessor cache = sTheInstance.get(key);
    if (cache != null) {
        if (token_type == OAuthTokenCache.ACCESS_TOKEN_TYPE) {
            cache.accessToken = consumer_token;
            cache.requestToken = null;
        } else {
            cache.requestToken = consumer_token;
            cache.accessToken = null;
        }
    }
    return cache;
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor)

Example 18 with OAuthAccessor

use of net.oauth.OAuthAccessor in project cxf by apache.

the class TokenRequestController method handleRequest.

@RequestMapping("/tokenRequest")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams, HttpServletRequest request) throws Exception {
    String oauthToken = oAuthParams.getOauthToken();
    String tokenRequestEndpoint = oAuthParams.getTokenRequestEndpoint();
    String clientID = oAuthParams.getClientID();
    if (tokenRequestEndpoint == null || "".equals(tokenRequestEndpoint)) {
        oAuthParams.setErrorMessage("Missing token request URI");
    }
    if (clientID == null || "".equals(clientID)) {
        oAuthParams.setErrorMessage("Missing consumer key");
    }
    if (oauthToken == null || "".equals(oauthToken)) {
        oAuthParams.setErrorMessage("Missing oauth token");
    }
    String verifier = oAuthParams.getOauthVerifier();
    if (verifier == null || "".equals(verifier)) {
        oAuthParams.setErrorMessage("Missing oauth verifier");
    }
    if (oAuthParams.getErrorMessage() == null) {
        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthServiceProvider provider = new OAuthServiceProvider(oAuthParams.getTemporaryCredentialsEndpoint(), oAuthParams.getResourceOwnerAuthorizationEndpoint(), tokenRequestEndpoint);
        OAuthConsumer consumer = new OAuthConsumer(null, clientID, oAuthParams.getClientSecret(), provider);
        OAuthAccessor accessor = new OAuthAccessor(consumer);
        accessor.requestToken = oauthToken;
        accessor.tokenSecret = Common.findCookieValue(request, "tokenSec");
        Map<String, String> parameters = new HashMap<>();
        parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, oAuthParams.getSignatureMethod());
        parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
        parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
        parameters.put(OAuth.OAUTH_TOKEN, oauthToken);
        parameters.put(OAuth.OAUTH_VERIFIER, oAuthParams.getOauthVerifier());
        try {
            client.getAccessToken(accessor, OAuthMessage.GET, parameters.entrySet());
            oAuthParams.setOauthToken(accessor.accessToken);
        } catch (Exception e) {
            oAuthParams.setErrorMessage(e.toString());
            oAuthParams.setOauthToken(oauthToken);
            return new ModelAndView("tokenRequest");
        }
        oAuthParams.setOauthTokenSecret(accessor.tokenSecret);
    }
    oAuthParams.setClientID(Common.findCookieValue(request, "clientID"));
    oAuthParams.setClientSecret(Common.findCookieValue(request, "clientSecret"));
    return new ModelAndView("accessToken");
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) OAuthServiceProvider(net.oauth.OAuthServiceProvider) URLConnectionClient(net.oauth.client.URLConnectionClient) OAuthClient(net.oauth.client.OAuthClient) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) OAuthConsumer(net.oauth.OAuthConsumer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with OAuthAccessor

use of net.oauth.OAuthAccessor in project cxf by apache.

the class OAuthClientUtils method getAccessToken.

public static Token getAccessToken(WebClient accessTokenService, Consumer consumer, Token requestToken, String verifier, Map<String, Object> oauthConsumerProps) throws OAuthServiceException {
    Map<String, String> parameters = new HashMap<>();
    parameters.put(OAuth.OAUTH_CONSUMER_KEY, consumer.getKey());
    parameters.put(OAuth.OAUTH_TOKEN, requestToken.getToken());
    parameters.put(OAuth.OAUTH_VERIFIER, verifier);
    if (oauthConsumerProps == null || !oauthConsumerProps.containsKey(OAuth.OAUTH_SIGNATURE_METHOD)) {
        parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
    }
    OAuthAccessor accessor = createAccessor(consumer, oauthConsumerProps);
    accessor.requestToken = requestToken.getToken();
    accessor.tokenSecret = requestToken.getSecret();
    return getToken(accessTokenService, accessor, parameters);
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) HashMap(java.util.HashMap)

Example 20 with OAuthAccessor

use of net.oauth.OAuthAccessor in project cxf by apache.

the class OAuthClientUtils method createAuthorizationHeader.

public static String createAuthorizationHeader(Consumer consumer, Token accessToken, String method, String requestURI, Map<String, Object> oauthConsumerProps) {
    Map<String, String> parameters = new HashMap<>();
    parameters.put(OAuth.OAUTH_CONSUMER_KEY, consumer.getKey());
    if (accessToken != null) {
        parameters.put(OAuth.OAUTH_TOKEN, accessToken.getToken());
    }
    if (oauthConsumerProps == null || !oauthConsumerProps.containsKey(OAuth.OAUTH_SIGNATURE_METHOD)) {
        parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
    }
    parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
    parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
    OAuthAccessor accessor = createAccessor(consumer, oauthConsumerProps);
    if (accessToken != null) {
        accessor.accessToken = accessToken.getToken();
        accessor.tokenSecret = accessToken.getSecret();
    }
    return doGetAuthorizationHeader(accessor, method, requestURI, parameters);
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) HashMap(java.util.HashMap)

Aggregations

OAuthAccessor (net.oauth.OAuthAccessor)20 OAuthMessage (net.oauth.OAuthMessage)10 OAuthConsumer (net.oauth.OAuthConsumer)9 HashMap (java.util.HashMap)7 IOException (java.io.IOException)6 ServletException (javax.servlet.ServletException)4 OAuthProblemException (net.oauth.OAuthProblemException)4 ServiceException (com.zimbra.common.service.ServiceException)3 Account (com.zimbra.cs.account.Account)3 OAuthServiceProvider (net.oauth.OAuthServiceProvider)3 OAuthClient (net.oauth.client.OAuthClient)3 URLConnectionClient (net.oauth.client.URLConnectionClient)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 AuthToken (com.zimbra.cs.account.AuthToken)2 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)2 OAuthAccessorSerializer (com.zimbra.cs.account.oauth.OAuthAccessorSerializer)2 OutputStream (java.io.OutputStream)2 IllegalArgumentException (java.lang.IllegalArgumentException)2 OAuthValidator (net.oauth.OAuthValidator)2