Search in sources :

Example 6 with Parameter

use of net.oauth.OAuth.Parameter in project liferay-ide by liferay.

the class OAuthRequest method exchangeRequestToken.

/**
 * Implements section 6.3 of the OAuth spec.
 */
private void exchangeRequestToken() throws OAuthRequestException, OAuthProtocolException {
    if (accessorInfo.getAccessor().accessToken != null) {
        // session extension per
        // http://oauth.googlecode.com/svn/spec/ext/session/1.0/drafts/1/spec.html
        accessorInfo.getAccessor().requestToken = accessorInfo.getAccessor().accessToken;
        accessorInfo.getAccessor().accessToken = null;
    }
    OAuthAccessor accessor = accessorInfo.getAccessor();
    if (accessor.consumer.serviceProvider.accessTokenURL == null) {
        throw new OAuthRequestException(OAuthError.BAD_OAUTH_TOKEN_URL, "access token");
    }
    Uri accessTokenUri = Uri.parse(accessor.consumer.serviceProvider.accessTokenURL);
    HttpRequest request = new HttpRequest(accessTokenUri);
    request.setMethod(accessorInfo.getHttpMethod().toString());
    if (accessorInfo.getHttpMethod() == HttpMethod.POST) {
        request.setHeader("Content-Type", OAuth.FORM_ENCODED);
    }
    List<Parameter> msgParams = Lists.newArrayList();
    msgParams.add(new Parameter(OAuth.OAUTH_TOKEN, accessor.requestToken));
    if (accessorInfo.getSessionHandle() != null) {
        msgParams.add(new Parameter(OAuthConstants.OAUTH_SESSION_HANDLE, accessorInfo.getSessionHandle()));
    }
    String receivedCallback = realRequest.getOAuthArguments().getReceivedCallbackUrl();
    if (!StringUtils.isBlank(receivedCallback)) {
        try {
            Uri parsed = Uri.parse(receivedCallback);
            String verifier = parsed.getQueryParameter(OAuth.OAUTH_VERIFIER);
            if (verifier != null) {
                msgParams.add(new Parameter(OAuth.OAUTH_VERIFIER, verifier));
            }
        } catch (IllegalArgumentException e) {
            throw new OAuthRequestException(OAuthError.INVALID_REQUEST, "Invalid received callback URL: " + receivedCallback, e);
        }
    }
    HttpRequest signed = sanitizeAndSign(request, msgParams, true);
    OAuthMessage reply = sendOAuthMessage(signed);
    accessor.accessToken = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN);
    accessor.tokenSecret = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN_SECRET);
    accessorInfo.setSessionHandle(OAuthUtil.getParameter(reply, OAuthConstants.OAUTH_SESSION_HANDLE));
    accessorInfo.setTokenExpireMillis(ACCESS_TOKEN_EXPIRE_UNKNOWN);
    if (OAuthUtil.getParameter(reply, OAuthConstants.OAUTH_EXPIRES_IN) != null) {
        try {
            int expireSecs = Integer.parseInt(OAuthUtil.getParameter(reply, OAuthConstants.OAUTH_EXPIRES_IN));
            long expireMillis = fetcherConfig.getClock().currentTimeMillis() + expireSecs * 1000L;
            accessorInfo.setTokenExpireMillis(expireMillis);
        } catch (NumberFormatException e) {
            // Hrm.  Bogus server.  We can safely ignore this, we'll just wait for the server to
            // tell us when the access token has expired.
            responseParams.logDetailedWarning("server returned bogus expiration");
        }
    }
    // future.
    if (accessTokenUri.equals(realRequest.getUri())) {
        accessTokenData = Maps.newHashMap();
        for (Entry<String, String> param : OAuthUtil.getParameters(reply)) {
            if (!param.getKey().startsWith("oauth")) {
                accessTokenData.put(param.getKey(), param.getValue());
            }
        }
    }
}
Also used : HttpRequest(org.apache.shindig.gadgets.http.HttpRequest) OAuthMessage(net.oauth.OAuthMessage) Uri(org.apache.shindig.common.uri.Uri) OAuthAccessor(net.oauth.OAuthAccessor) Parameter(net.oauth.OAuth.Parameter)

Aggregations

Parameter (net.oauth.OAuth.Parameter)6 OAuthMessage (net.oauth.OAuthMessage)3 HttpRequest (org.apache.shindig.gadgets.http.HttpRequest)3 OAuthAccessor (net.oauth.OAuthAccessor)2 IOException (java.io.IOException)1 OAuthException (net.oauth.OAuthException)1 Uri (org.apache.shindig.common.uri.Uri)1 UriBuilder (org.apache.shindig.common.uri.UriBuilder)1