|
| 1 | +package org.scribe.oauth; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import org.scribe.builder.api.*; |
| 6 | +import org.scribe.model.*; |
| 7 | +import org.scribe.utils.*; |
| 8 | + |
| 9 | +/** |
| 10 | +* OAuth 1.0 implementation of {@link OAuthService} |
| 11 | +* |
| 12 | +* @author Pablo Fernandez (copy of OAuth 1.0a), patched by Martin Vlcek (API 1.0) |
| 13 | +*/ |
| 14 | +public class OAuth10ServiceImpl implements OAuthService |
| 15 | +{ |
| 16 | + private static final String VERSION = "1.0"; |
| 17 | + |
| 18 | + private OAuthConfig config; |
| 19 | + private DefaultApi10 api; |
| 20 | + |
| 21 | + /** |
| 22 | +* Default constructor |
| 23 | +* |
| 24 | +* @param api OAuth1.0 api information |
| 25 | +* @param config OAuth 1.0 configuration param object |
| 26 | +*/ |
| 27 | + public OAuth10ServiceImpl(DefaultApi10 api, OAuthConfig config) |
| 28 | + { |
| 29 | + this.api = api; |
| 30 | + this.config = config; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | +* {@inheritDoc} |
| 35 | +*/ |
| 36 | + public Token getRequestToken() |
| 37 | + { |
| 38 | + config.log("obtaining request token from " + api.getRequestTokenEndpoint()); |
| 39 | + OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint()); |
| 40 | + |
| 41 | + addOAuthParams(request, OAuthConstants.EMPTY_TOKEN); |
| 42 | + appendSignature(request); |
| 43 | + |
| 44 | + config.log("sending request..."); |
| 45 | + Response response = request.send(); |
| 46 | + String body = response.getBody(); |
| 47 | + |
| 48 | + config.log("response status code: " + response.getCode()); |
| 49 | + config.log("response body: " + body); |
| 50 | + return api.getRequestTokenExtractor().extract(body); |
| 51 | + } |
| 52 | + |
| 53 | + private void addOAuthParams(OAuthRequest request, Token token) |
| 54 | + { |
| 55 | + request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds()); |
| 56 | + request.addOAuthParameter(OAuthConstants.NONCE, api.getTimestampService().getNonce()); |
| 57 | + request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey()); |
| 58 | + request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod()); |
| 59 | + request.addOAuthParameter(OAuthConstants.VERSION, getVersion()); |
| 60 | + if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope()); |
| 61 | + request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token)); |
| 62 | + |
| 63 | + config.log("appended additional OAuth parameters: " + MapUtils.toString(request.getOauthParameters())); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | +* {@inheritDoc} |
| 68 | +*/ |
| 69 | + public Token getAccessToken(Token requestToken, Verifier verifier) |
| 70 | + { |
| 71 | + config.log("obtaining access token from " + api.getAccessTokenEndpoint()); |
| 72 | + OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint()); |
| 73 | + request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken()); |
| 74 | + //request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue()); |
| 75 | + |
| 76 | + config.log("setting token to: " + requestToken); |
| 77 | + addOAuthParams(request, requestToken); |
| 78 | + appendSignature(request); |
| 79 | + Response response = request.send(); |
| 80 | + return api.getAccessTokenExtractor().extract(response.getBody()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | +* {@inheritDoc} |
| 85 | +*/ |
| 86 | + public void signRequest(Token token, OAuthRequest request) |
| 87 | + { |
| 88 | + config.log("signing request: " + request.getCompleteUrl()); |
| 89 | + |
| 90 | + // Do not append the token if empty. This is for two legged OAuth calls. |
| 91 | + if (!token.isEmpty()) |
| 92 | + { |
| 93 | + request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken()); |
| 94 | + } |
| 95 | + config.log("setting token to: " + token); |
| 96 | + addOAuthParams(request, token); |
| 97 | + appendSignature(request); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | +* {@inheritDoc} |
| 102 | +*/ |
| 103 | + public String getVersion() |
| 104 | + { |
| 105 | + return VERSION; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | +* {@inheritDoc} |
| 110 | +*/ |
| 111 | + public String getAuthorizationUrl(Token requestToken) |
| 112 | + { |
| 113 | + return api.getAuthorizationUrl(requestToken, config.getCallback()); |
| 114 | + } |
| 115 | + |
| 116 | + private String getSignature(OAuthRequest request, Token token) |
| 117 | + { |
| 118 | + config.log("generating signature..."); |
| 119 | + String baseString = api.getBaseStringExtractor().extract(request); |
| 120 | + String signature = api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret()); |
| 121 | + |
| 122 | + config.log("base string is: " + baseString); |
| 123 | + config.log("signature is: " + signature); |
| 124 | + return signature; |
| 125 | + } |
| 126 | + |
| 127 | + private void appendSignature(OAuthRequest request) |
| 128 | + { |
| 129 | + switch (config.getSignatureType()) |
| 130 | + { |
| 131 | + case Header: |
| 132 | + config.log("using Http Header signature"); |
| 133 | + |
| 134 | + String oauthHeader = api.getHeaderExtractor().extract(request); |
| 135 | + request.addHeader(OAuthConstants.HEADER, oauthHeader); |
| 136 | + break; |
| 137 | + case QueryString: |
| 138 | + config.log("using Querystring signature"); |
| 139 | + |
| 140 | + for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet()) |
| 141 | + { |
| 142 | + request.addQuerystringParameter(entry.getKey(), entry.getValue()); |
| 143 | + } |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments