forked from Twitter4J/Twitter4J
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TFJ-762 add Application-only authentication feature.
- Loading branch information
1 parent
b25e57a
commit cad1650
Showing
14 changed files
with
749 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
twitter4j-core/src/main/java/twitter4j/auth/OAuth2Authorization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package twitter4j.auth; | ||
|
||
import twitter4j.TwitterException; | ||
import twitter4j.conf.Configuration; | ||
import twitter4j.internal.http.*; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
|
||
/** | ||
* @author KOMIYA Atsushi - komiya.atsushi at gmail.com | ||
* @see <a href="https://dev.twitter.com/docs/auth/application-only-auth">Application-only authentication</a> | ||
*/ | ||
public class OAuth2Authorization implements Authorization, java.io.Serializable, OAuth2Support { | ||
private static final long serialVersionUID = 4274784415515174129L; | ||
|
||
private final Configuration conf; | ||
|
||
private HttpClientWrapper http; | ||
|
||
private String consumerKey; | ||
|
||
private String consumerSecret; | ||
|
||
private OAuth2Token token; | ||
|
||
public OAuth2Authorization(Configuration conf) { | ||
this.conf = conf; | ||
setOAuthConsumer(conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret()); | ||
http = new HttpClientWrapper(conf); | ||
} | ||
|
||
@Override | ||
public void setOAuthConsumer(String consumerKey, String consumerSecret) { | ||
this.consumerKey = consumerKey != null ? consumerKey : ""; | ||
this.consumerSecret = consumerSecret != null ? consumerSecret : ""; | ||
} | ||
|
||
@Override | ||
public OAuth2Token getOAuth2Token() throws TwitterException { | ||
if (token != null) { | ||
throw new IllegalStateException("OAuth 2 Bearer Token is already available."); | ||
} | ||
|
||
HttpParameter[] params = new HttpParameter[1]; | ||
params[0] = new HttpParameter("grant_type", "client_credentials"); | ||
|
||
HttpResponse res = http.post(conf.getOAuth2TokenURL(), params, this); | ||
if (res.getStatusCode() != 200) { | ||
throw new TwitterException("Obtaining OAuth 2 Bearer Token failed.", res); | ||
} | ||
token = new OAuth2Token(res); | ||
return token; | ||
} | ||
|
||
@Override | ||
public void setOAuth2Token(OAuth2Token oauth2Token) { | ||
this.token = oauth2Token; | ||
} | ||
|
||
@Override | ||
public void invalidateOAuth2Token() throws TwitterException { | ||
if (token == null) { | ||
throw new IllegalStateException("OAuth 2 Bearer Token is not available."); | ||
} | ||
|
||
HttpParameter[] params = new HttpParameter[1]; | ||
params[0] = new HttpParameter("access_token", token.getAccessToken()); | ||
|
||
OAuth2Token _token = token; | ||
boolean succeed = false; | ||
|
||
try { | ||
token = null; | ||
|
||
HttpResponse res = http.post(conf.getOAuth2InvalidateTokenURL(), params, this); | ||
if (res.getStatusCode() != 200) { | ||
throw new TwitterException("Invalidating OAuth 2 Bearer Token failed.", res); | ||
} | ||
|
||
succeed = true; | ||
|
||
} finally { | ||
if (!succeed) { | ||
token = _token; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String getAuthorizationHeader(HttpRequest req) { | ||
if (token == null) { | ||
String credentials = ""; | ||
try { | ||
credentials = | ||
URLEncoder.encode(consumerKey, "UTF-8") | ||
+ ":" | ||
+ URLEncoder.encode(consumerSecret, "UTF-8"); | ||
|
||
} catch (UnsupportedEncodingException ignore) { | ||
} | ||
|
||
return "Basic " + BASE64Encoder.encode(credentials.getBytes()); | ||
|
||
} else { | ||
return token.generateAuthorizationHeader(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return token != null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null || !(obj instanceof OAuth2Authorization)) { | ||
return false; | ||
} | ||
|
||
OAuth2Authorization that = (OAuth2Authorization) obj; | ||
if (consumerKey != null ? !consumerKey.equals(that.consumerKey) : that.consumerKey != null) { | ||
return false; | ||
} | ||
if (consumerSecret != null ? !consumerSecret.equals(that.consumerSecret) : that.consumerSecret != null) { | ||
return false; | ||
} | ||
if (token != null ? !token.equals(that.token) : that.token != null) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = consumerKey != null ? consumerKey.hashCode() : 0; | ||
result = 31 * result + (consumerSecret != null ? consumerSecret.hashCode() : 0); | ||
result = 31 * result + (token != null ? token.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OAuth2Authorization{" + | ||
"consumerKey='" + consumerKey + '\'' + | ||
", consumerSecret='******************************************\'" + | ||
", token=" + token.toString() + | ||
'}'; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
twitter4j-core/src/main/java/twitter4j/auth/OAuth2Support.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package twitter4j.auth; | ||
|
||
import twitter4j.TwitterException; | ||
|
||
/** | ||
* @author KOMIYA Atsushi - komiya.atsushi at gmail.com | ||
* @see <a href="https://dev.twitter.com/docs/auth/application-only-auth">Application-only authentication | Twitter Developers</a> | ||
*/ | ||
public interface OAuth2Support { | ||
/** | ||
* Sets the OAuth consumer key and consumer secret. | ||
* | ||
* @param consumerKey OAuth consumer key | ||
* @param consumerSecret OAuth consumer secret | ||
* @throws IllegalStateException when OAuth consumer has already been set, or the instance is using basic authorization. | ||
*/ | ||
void setOAuthConsumer(String consumerKey, String consumerSecret); | ||
|
||
/** | ||
* Obtains an OAuth 2 Bearer token. | ||
* | ||
* @return | ||
* @throws TwitterException when Twitter service or network is unavailable, or connecting non-SSL endpoints. | ||
* @throws IllegalStateException when Bearer token is already available, or OAuth consumer is not available. | ||
* @see <a href="https://dev.twitter.com/docs/api/1.1/post/oauth2/token">POST oauth2/token | Twitter Developers</a> | ||
*/ | ||
OAuth2Token getOAuth2Token() throws TwitterException; | ||
|
||
/** | ||
* Sets the OAuth 2 Bearer token. | ||
* | ||
* @param oauth2Token OAuth 2 Bearer token | ||
*/ | ||
void setOAuth2Token(OAuth2Token oauth2Token); | ||
|
||
/** | ||
* Revokes an issued OAuth 2 Bearer Token. | ||
* | ||
* @throws TwitterException when Twitter service or network is unavailable, or connecting non-SSL endpoints. | ||
* @throws IllegalStateException when Bearer token is not available. | ||
*/ | ||
void invalidateOAuth2Token() throws TwitterException; | ||
} |
Oops, something went wrong.