@@ -66,43 +66,42 @@ public OAuthSignatureCalculatorInstance() throws NoSuchAlgorithmException {
66
66
}
67
67
68
68
public void sign (ConsumerKey consumerAuth , RequestToken userAuth , Request request , RequestBuilderBase <?> requestBuilder ) throws InvalidKeyException {
69
- String nonce = generateNonce ();
69
+ String percentEncodedNonce = generatePercentEncodedNonce ();
70
70
long timestamp = generateTimestamp ();
71
- sign (consumerAuth , userAuth , request , requestBuilder , nonce , timestamp );
71
+ sign (consumerAuth , userAuth , request , requestBuilder , percentEncodedNonce , timestamp );
72
72
}
73
73
74
- private String generateNonce () {
74
+ private String generatePercentEncodedNonce () {
75
75
ThreadLocalRandom .current ().nextBytes (nonceBuffer );
76
76
// let's use base64 encoding over hex, slightly more compact than hex or decimals
77
- return Base64 .encode (nonceBuffer );
77
+ return Utf8UrlEncoder . percentEncodeQueryElement ( Base64 .encode (nonceBuffer ) );
78
78
}
79
79
80
80
private static long generateTimestamp () {
81
81
return System .currentTimeMillis () / 1000L ;
82
82
}
83
83
84
- void sign (ConsumerKey consumerAuth , RequestToken userAuth , Request request , RequestBuilderBase <?> requestBuilder , String nonce , long timestamp )
85
- throws InvalidKeyException {
86
- String signature = calculateSignature (consumerAuth , userAuth , request , timestamp , nonce );
87
- String headerValue = constructAuthHeader (consumerAuth , userAuth , signature , nonce , timestamp );
84
+ void sign (ConsumerKey consumerAuth , RequestToken userAuth , Request request , RequestBuilderBase <?> requestBuilder , String percentEncodedNonce , long timestamp ) throws InvalidKeyException {
85
+ String signature = calculateSignature (consumerAuth , userAuth , request , timestamp , percentEncodedNonce );
86
+ String headerValue = constructAuthHeader (consumerAuth , userAuth , signature , percentEncodedNonce , timestamp );
88
87
requestBuilder .setHeader (HttpHeaderNames .AUTHORIZATION , headerValue );
89
88
}
90
89
91
- String calculateSignature (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long oauthTimestamp , String nonce ) throws InvalidKeyException {
90
+ String calculateSignature (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long oauthTimestamp , String percentEncodedNonce ) throws InvalidKeyException {
92
91
93
- StringBuilder sb = signatureBaseString (consumerAuth , userAuth , request , oauthTimestamp , nonce );
92
+ StringBuilder sb = signatureBaseString (consumerAuth , userAuth , request , oauthTimestamp , percentEncodedNonce );
94
93
95
94
ByteBuffer rawBase = StringUtils .charSequence2ByteBuffer (sb , UTF_8 );
96
95
byte [] rawSignature = digest (consumerAuth , userAuth , rawBase );
97
96
// and finally, base64 encoded... phew!
98
97
return Base64 .encode (rawSignature );
99
98
}
100
99
101
- StringBuilder signatureBaseString (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long oauthTimestamp , String nonce ) {
100
+ StringBuilder signatureBaseString (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long oauthTimestamp , String percentEncodedNonce ) {
102
101
103
102
// beware: must generate first as we're using pooled StringBuilder
104
103
String baseUrl = request .getUri ().toBaseUrl ();
105
- String encodedParams = encodedParams (consumerAuth , userAuth , oauthTimestamp , nonce , request .getFormParams (), request .getQueryParams ());
104
+ String encodedParams = encodedParams (consumerAuth , userAuth , oauthTimestamp , percentEncodedNonce , request .getFormParams (), request .getQueryParams ());
106
105
107
106
StringBuilder sb = StringBuilderPool .DEFAULT .stringBuilder ();
108
107
sb .append (request .getMethod ()); // POST / GET etc (nothing to URL encode)
@@ -115,16 +114,17 @@ StringBuilder signatureBaseString(ConsumerKey consumerAuth, RequestToken userAut
115
114
return sb ;
116
115
}
117
116
118
- private String encodedParams (ConsumerKey consumerAuth , RequestToken userAuth , long oauthTimestamp , String nonce , List <Param > formParams , List <Param > queryParams ) {
117
+ private String encodedParams (ConsumerKey consumerAuth , RequestToken userAuth , long oauthTimestamp , String percentEncodedNonce , List <Param > formParams , List <Param > queryParams ) {
119
118
120
119
parameters .reset ();
121
120
122
121
/**
123
122
* List of all query and form parameters added to this request; needed for calculating request signature
124
123
*/
125
124
// start with standard OAuth parameters we need
126
- parameters .add (KEY_OAUTH_CONSUMER_KEY , consumerAuth .getPercentEncodedKey ())
127
- .add (KEY_OAUTH_NONCE , Utf8UrlEncoder .percentEncodeQueryElement (nonce )).add (KEY_OAUTH_SIGNATURE_METHOD , OAUTH_SIGNATURE_METHOD )
125
+ parameters .add (KEY_OAUTH_CONSUMER_KEY , consumerAuth .getPercentEncodedKey ())//
126
+ .add (KEY_OAUTH_NONCE , percentEncodedNonce )
127
+ .add (KEY_OAUTH_SIGNATURE_METHOD , OAUTH_SIGNATURE_METHOD )//
128
128
.add (KEY_OAUTH_TIMESTAMP , String .valueOf (oauthTimestamp ));
129
129
if (userAuth .getKey () != null ) {
130
130
parameters .add (KEY_OAUTH_TOKEN , userAuth .getPercentEncodedKey ());
@@ -170,7 +170,7 @@ private byte[] digest(ConsumerKey consumerAuth, RequestToken userAuth, ByteBuffe
170
170
return mac .doFinal ();
171
171
}
172
172
173
- String constructAuthHeader (ConsumerKey consumerAuth , RequestToken userAuth , String signature , String nonce , long oauthTimestamp ) {
173
+ String constructAuthHeader (ConsumerKey consumerAuth , RequestToken userAuth , String signature , String percentEncodedNonce , long oauthTimestamp ) {
174
174
StringBuilder sb = StringBuilderPool .DEFAULT .stringBuilder ();
175
175
sb .append ("OAuth " );
176
176
sb .append (KEY_OAUTH_CONSUMER_KEY ).append ("=\" " ).append (consumerAuth .getPercentEncodedKey ()).append ("\" , " );
@@ -184,10 +184,7 @@ String constructAuthHeader(ConsumerKey consumerAuth, RequestToken userAuth, Stri
184
184
Utf8UrlEncoder .encodeAndAppendPercentEncoded (sb , signature ).append ("\" , " );
185
185
sb .append (KEY_OAUTH_TIMESTAMP ).append ("=\" " ).append (oauthTimestamp ).append ("\" , " );
186
186
187
- // also: nonce may contain things that need URL encoding (esp. when using base64):
188
- sb .append (KEY_OAUTH_NONCE ).append ("=\" " );
189
- Utf8UrlEncoder .encodeAndAppendPercentEncoded (sb , nonce );
190
- sb .append ("\" , " );
187
+ sb .append (KEY_OAUTH_NONCE ).append ("=\" " ).append (percentEncodedNonce ).append ("\" , " );
191
188
192
189
sb .append (KEY_OAUTH_VERSION ).append ("=\" " ).append (OAUTH_VERSION_1_0 ).append ("\" " );
193
190
return sb .toString ();
0 commit comments