X Tutup
Skip to content

Commit 6cd67d2

Browse files
author
Jose Antonio Espinosa
committed
Added ols OAuth 1.0 implementation
1 parent f64d0a2 commit 6cd67d2

File tree

2 files changed

+300
-0
lines changed

2 files changed

+300
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.AccessTokenExtractor;
4+
import org.scribe.extractors.BaseStringExtractor;
5+
import org.scribe.extractors.BaseStringExtractorImpl;
6+
import org.scribe.extractors.HeaderExtractor;
7+
import org.scribe.extractors.HeaderExtractorImpl;
8+
import org.scribe.extractors.RequestTokenExtractor;
9+
import org.scribe.extractors.TokenExtractorImpl;
10+
import org.scribe.model.OAuthConfig;
11+
import org.scribe.model.Token;
12+
import org.scribe.model.Verb;
13+
import org.scribe.oauth.OAuth10ServiceImpl;
14+
import org.scribe.oauth.OAuthService;
15+
import org.scribe.services.HMACSha1SignatureService;
16+
import org.scribe.services.SignatureService;
17+
import org.scribe.services.TimestampService;
18+
import org.scribe.services.TimestampServiceImpl;
19+
20+
/**
21+
* Default implementation of the OAuth protocol, version 1.0
22+
*
23+
* This class is meant to be extended by concrete implementations of the API,
24+
* providing the endpoints and endpoint-http-verbs.
25+
*
26+
* If your Api adheres to the 1.0 protocol correctly, you just need to extend
27+
* this class and define the getters for your endpoints.
28+
*
29+
* If your Api does something a bit different, you can override the different
30+
* extractors or services, in order to fine-tune the process. Please read the
31+
* javadocs of the interfaces to get an idea of what to do.
32+
*
33+
* @author Pablo Fernandez (copy of OAuth 1.0a), patched by Martin Vlcek (OAuth 1.0)
34+
*
35+
*/
36+
public abstract class DefaultApi10 implements Api
37+
{
38+
/**
39+
* Returns the access token extractor.
40+
*
41+
* @return access token extractor
42+
*/
43+
public AccessTokenExtractor getAccessTokenExtractor()
44+
{
45+
return new TokenExtractorImpl();
46+
}
47+
48+
/**
49+
* Returns the base string extractor.
50+
*
51+
* @return base string extractor
52+
*/
53+
public BaseStringExtractor getBaseStringExtractor()
54+
{
55+
return new BaseStringExtractorImpl();
56+
}
57+
58+
/**
59+
* Returns the header extractor.
60+
*
61+
* @return header extractor
62+
*/
63+
public HeaderExtractor getHeaderExtractor()
64+
{
65+
return new HeaderExtractorImpl();
66+
}
67+
68+
/**
69+
* Returns the request token extractor.
70+
*
71+
* @return request token extractor
72+
*/
73+
public RequestTokenExtractor getRequestTokenExtractor()
74+
{
75+
return new TokenExtractorImpl();
76+
}
77+
78+
/**
79+
* Returns the signature service.
80+
*
81+
* @return signature service
82+
*/
83+
public SignatureService getSignatureService()
84+
{
85+
return new HMACSha1SignatureService();
86+
}
87+
88+
/**
89+
* Returns the timestamp service.
90+
*
91+
* @return timestamp service
92+
*/
93+
public TimestampService getTimestampService()
94+
{
95+
return new TimestampServiceImpl();
96+
}
97+
98+
/**
99+
* Returns the verb for the access token endpoint (defaults to POST)
100+
*
101+
* @return access token endpoint verb
102+
*/
103+
public Verb getAccessTokenVerb()
104+
{
105+
return Verb.POST;
106+
}
107+
108+
/**
109+
* Returns the verb for the request token endpoint (defaults to POST)
110+
*
111+
* @return request token endpoint verb
112+
*/
113+
public Verb getRequestTokenVerb()
114+
{
115+
return Verb.POST;
116+
}
117+
118+
/**
119+
* Returns the URL that receives the request token requests.
120+
*
121+
* @return request token URL
122+
*/
123+
public abstract String getRequestTokenEndpoint();
124+
125+
/**
126+
* Returns the URL that receives the access token requests.
127+
*
128+
* @return access token URL
129+
*/
130+
public abstract String getAccessTokenEndpoint();
131+
132+
/**
133+
* Returns the URL where you should redirect your users to authenticate
134+
* your application.
135+
*
136+
* @param requestToken the request token you need to authorize
137+
* @return the URL where you should redirect your users
138+
*/
139+
public abstract String getAuthorizationUrl(Token requestToken, String callback);
140+
141+
/**
142+
* Returns the {@link OAuthService} for this Api
143+
*
144+
* @param apiKey Key
145+
* @param apiSecret Api Secret
146+
* @param callback OAuth callback (either URL or 'oob')
147+
* @param scope OAuth scope (optional)
148+
*/
149+
public OAuthService createService(OAuthConfig config)
150+
{
151+
return new OAuth10ServiceImpl(this, config);
152+
}
153+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)
X Tutup