Search in sources :

Example 1 with OAuthClient

use of net.oauth.client.OAuthClient in project cxf by apache.

the class GetProtectedResourceController method handleRequest.

@RequestMapping("/getProtectedResource")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams, HttpServletRequest request) throws Exception {
    OAuthServiceProvider provider = new OAuthServiceProvider(oAuthParams.getTemporaryCredentialsEndpoint(), oAuthParams.getResourceOwnerAuthorizationEndpoint(), null);
    OAuthConsumer consumer = new OAuthConsumer(null, oAuthParams.getClientID(), oAuthParams.getClientSecret(), provider);
    OAuthAccessor accessor = new OAuthAccessor(consumer);
    accessor.requestToken = oAuthParams.getOauthToken();
    accessor.tokenSecret = oAuthParams.getOauthTokenSecret();
    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, oAuthParams.getOauthToken());
    parameters.put(OAuth.OAUTH_CONSUMER_KEY, oAuthParams.getClientID());
    OAuthMessage msg = null;
    String method = request.getParameter("op");
    if ("GET".equals(method)) {
        msg = accessor.newRequestMessage(OAuthMessage.GET, oAuthParams.getGetResourceURL(), parameters.entrySet());
    } else {
        msg = accessor.newRequestMessage(OAuthMessage.POST, oAuthParams.getPostResourceURL(), parameters.entrySet());
    }
    OAuthClient client = new OAuthClient(new URLConnectionClient());
    msg = client.access(msg, ParameterStyle.QUERY_STRING);
    StringBuilder bodyBuffer = readBody(msg);
    oAuthParams.setResourceResponse(bodyBuffer.toString());
    String authHeader = msg.getHeader("WWW-Authenticate");
    String oauthHeader = msg.getHeader("OAuth");
    String header = "";
    if (authHeader != null) {
        header += "WWW-Authenticate:" + authHeader;
    }
    if (oauthHeader != null) {
        header += "OAuth:" + oauthHeader;
    }
    oAuthParams.setHeader(header);
    oAuthParams.setResponseCode(((OAuthResponseMessage) msg).getHttpResponse().getStatusCode());
    return new ModelAndView("accessToken");
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) OAuthServiceProvider(net.oauth.OAuthServiceProvider) OAuthMessage(net.oauth.OAuthMessage) URLConnectionClient(net.oauth.client.URLConnectionClient) HashMap(java.util.HashMap) OAuthClient(net.oauth.client.OAuthClient) OAuthResponseMessage(net.oauth.client.OAuthResponseMessage) ModelAndView(org.springframework.web.servlet.ModelAndView) OAuthConsumer(net.oauth.OAuthConsumer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with OAuthClient

use of net.oauth.client.OAuthClient in project cxf by apache.

the class TemporaryCredentialsController method handleRequest.

@RequestMapping("/handleTemporaryCredentials")
public ModelAndView handleRequest(@ModelAttribute(value = "oAuthParams") OAuthParams oAuthParams, HttpServletResponse response) {
    OAuthServiceProvider provider;
    OAuthConsumer consumer;
    OAuthAccessor accessor;
    OAuthClient client = new OAuthClient(new URLConnectionClient());
    oAuthParams.setErrorMessage(null);
    String temporaryCredentialsEndpointUrl = oAuthParams.getTemporaryCredentialsEndpoint();
    if (temporaryCredentialsEndpointUrl == null || "".equals(temporaryCredentialsEndpointUrl)) {
        oAuthParams.setErrorMessage("Missing temporary credentials endpoint url");
    }
    String clientId = oAuthParams.getClientID();
    if (clientId == null || "".equals(clientId)) {
        oAuthParams.setErrorMessage("Missing client identifier");
    }
    String secret = oAuthParams.getClientSecret();
    if (secret == null || "".equals(secret)) {
        oAuthParams.setErrorMessage("Missing client shared-secret");
    }
    if (oAuthParams.getErrorMessage() == null) {
        provider = new OAuthServiceProvider(temporaryCredentialsEndpointUrl, oAuthParams.getResourceOwnerAuthorizationEndpoint(), oAuthParams.getTokenRequestEndpoint());
        consumer = new OAuthConsumer(null, clientId, secret, provider);
        accessor = new OAuthAccessor(consumer);
        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_CALLBACK, oAuthParams.getCallbackURL());
        parameters.put("realm", "private");
        parameters.put("scope", "read_info modify_info");
        try {
            accessor.consumer.setProperty(OAuthClient.PARAMETER_STYLE, ParameterStyle.AUTHORIZATION_HEADER);
            client.getRequestToken(accessor, OAuthMessage.POST, parameters.entrySet());
        } catch (Exception e) {
            oAuthParams.setErrorMessage(e.toString());
        }
        oAuthParams.setOauthToken(accessor.requestToken);
        oAuthParams.setOauthTokenSecret(accessor.tokenSecret);
        Cookie cId = new Cookie("clientID", oAuthParams.getClientID());
        Cookie cSec = new Cookie("clientSecret", oAuthParams.getClientSecret());
        Cookie tokenSec = new Cookie("tokenSec", accessor.tokenSecret);
        response.addCookie(cId);
        response.addCookie(cSec);
        response.addCookie(tokenSec);
    }
    ModelAndView modelAndView = new ModelAndView();
    if (oAuthParams.getErrorMessage() != null) {
        modelAndView.setViewName("temporaryCredentials");
    } else {
        modelAndView.setViewName("authorizeResourceOwner");
    }
    return modelAndView;
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) Cookie(javax.servlet.http.Cookie) 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 3 with OAuthClient

use of net.oauth.client.OAuthClient in project ngtesting-platform by aaronchen2k.

the class AtlassianOAuthClient method getRequestToken.

public TokenSecretVerifierHolder getRequestToken() {
    try {
        OAuthAccessor accessor = getAccessor();
        OAuthClient oAuthClient = new OAuthClient(new HttpClient4());
        List<OAuth.Parameter> callBack;
        if (callback == null || "".equals(callback)) {
            callBack = Collections.<OAuth.Parameter>emptyList();
        } else {
            callBack = ImmutableList.of(new OAuth.Parameter(OAuth.OAUTH_CALLBACK, callback));
        }
        OAuthMessage message = oAuthClient.getRequestTokenResponse(accessor, "POST", callBack);
        TokenSecretVerifierHolder tokenSecretVerifier = new TokenSecretVerifierHolder();
        tokenSecretVerifier.token = accessor.requestToken;
        tokenSecretVerifier.secret = accessor.tokenSecret;
        tokenSecretVerifier.verifier = message.getParameter(OAUTH_VERIFIER);
        return tokenSecretVerifier;
    } catch (Exception e) {
        throw new RuntimeException("Failed to obtain request token", e);
    }
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) HttpClient4(net.oauth.client.httpclient4.HttpClient4) OAuthMessage(net.oauth.OAuthMessage) OAuthClient(net.oauth.client.OAuthClient) OAuth(net.oauth.OAuth)

Example 4 with OAuthClient

use of net.oauth.client.OAuthClient in project ngtesting-platform by aaronchen2k.

the class AtlassianOAuthClient method getAuthenticatedRequest.

public String getAuthenticatedRequest(String url, String accessToken, List<OAuth.Parameter> params) {
    try {
        OAuthAccessor accessor = getAccessor();
        OAuthClient client = new OAuthClient(new HttpClient4());
        accessor.accessToken = accessToken;
        OAuthMessage response = client.invoke(accessor, url, params);
        return response.readBodyAsString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to make an authenticated request.", e);
    }
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) HttpClient4(net.oauth.client.httpclient4.HttpClient4) OAuthMessage(net.oauth.OAuthMessage) OAuthClient(net.oauth.client.OAuthClient)

Example 5 with OAuthClient

use of net.oauth.client.OAuthClient 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)

Aggregations

OAuthAccessor (net.oauth.OAuthAccessor)6 OAuthClient (net.oauth.client.OAuthClient)6 OAuthMessage (net.oauth.OAuthMessage)4 HashMap (java.util.HashMap)3 OAuthConsumer (net.oauth.OAuthConsumer)3 OAuthServiceProvider (net.oauth.OAuthServiceProvider)3 URLConnectionClient (net.oauth.client.URLConnectionClient)3 HttpClient4 (net.oauth.client.httpclient4.HttpClient4)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 Cookie (javax.servlet.http.Cookie)1 OAuth (net.oauth.OAuth)1 OAuthResponseMessage (net.oauth.client.OAuthResponseMessage)1