13
13
*/
14
14
package org .asynchttpclient .oauth ;
15
15
16
- import io .netty .handler .codec .http .HttpHeaderNames ;
17
16
import org .asynchttpclient .Param ;
18
- import org .asynchttpclient .Request ;
19
- import org .asynchttpclient .RequestBuilderBase ;
20
17
import org .asynchttpclient .SignatureCalculator ;
18
+ import org .asynchttpclient .uri .Uri ;
21
19
import org .asynchttpclient .util .StringBuilderPool ;
22
20
import org .asynchttpclient .util .StringUtils ;
23
21
import org .asynchttpclient .util .Utf8UrlEncoder ;
40
38
* Supports most common signature inclusion and calculation methods: HMAC-SHA1 for calculation, and Header inclusion as inclusion method. Nonce generation uses simple random
41
39
* numbers with base64 encoding.
42
40
*/
43
- class OAuthSignatureCalculatorInstance {
41
+ public class OAuthSignatureCalculatorInstance {
44
42
45
43
private static final Pattern STAR_CHAR_PATTERN = Pattern .compile ("*" , Pattern .LITERAL );
46
44
private static final Pattern PLUS_CHAR_PATTERN = Pattern .compile ("+" , Pattern .LITERAL );
@@ -60,19 +58,19 @@ class OAuthSignatureCalculatorInstance {
60
58
private final byte [] nonceBuffer = new byte [16 ];
61
59
private final Parameters parameters = new Parameters ();
62
60
63
- OAuthSignatureCalculatorInstance () throws NoSuchAlgorithmException {
61
+ public OAuthSignatureCalculatorInstance () throws NoSuchAlgorithmException {
64
62
mac = Mac .getInstance (HMAC_SHA1_ALGORITHM );
65
63
}
66
64
67
- private static long generateTimestamp () {
68
- return System .currentTimeMillis () / 1000L ;
69
- }
70
-
71
- public void sign (ConsumerKey consumerAuth , RequestToken userAuth , Request request , RequestBuilderBase <?> requestBuilder ) throws InvalidKeyException {
65
+ public String computeAuthorizationHeader (ConsumerKey consumerAuth ,
66
+ RequestToken userAuth ,
67
+ Uri uri ,
68
+ String method ,
69
+ List <Param > formParams ,
70
+ List <Param > queryParams ) throws InvalidKeyException {
72
71
String nonce = generateNonce ();
73
72
long timestamp = generateTimestamp ();
74
- String authorization = computeAuthorizationHeader (consumerAuth , userAuth , request , timestamp , nonce );
75
- requestBuilder .setHeader (HttpHeaderNames .AUTHORIZATION , authorization );
73
+ return computeAuthorizationHeader (consumerAuth , userAuth , uri , method , formParams , queryParams , timestamp , nonce );
76
74
}
77
75
78
76
private String generateNonce () {
@@ -81,30 +79,63 @@ private String generateNonce() {
81
79
return Base64 .getEncoder ().encodeToString (nonceBuffer );
82
80
}
83
81
84
- String computeAuthorizationHeader (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long timestamp , String nonce ) throws InvalidKeyException {
85
- String percentEncodedNonce = Utf8UrlEncoder .percentEncodeQueryElement (nonce );
86
- String signature = calculateSignature (consumerAuth , userAuth , request , timestamp , percentEncodedNonce );
87
- return constructAuthHeader (consumerAuth , userAuth , signature , timestamp , percentEncodedNonce );
82
+ private static long generateTimestamp () {
83
+ return System .currentTimeMillis () / 1000L ;
88
84
}
89
85
90
- String calculateSignature (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long oauthTimestamp , String percentEncodedNonce ) throws InvalidKeyException {
86
+ String computeAuthorizationHeader (ConsumerKey consumerAuth ,
87
+ RequestToken userAuth ,
88
+ Uri uri ,
89
+ String method ,
90
+ List <Param > formParams ,
91
+ List <Param > queryParams ,
92
+ long timestamp ,
93
+ String nonce ) throws InvalidKeyException {
94
+ String percentEncodedNonce = Utf8UrlEncoder .percentEncodeQueryElement (nonce );
95
+ String signature = computeSignature (consumerAuth , userAuth , uri , method , formParams , queryParams , timestamp , percentEncodedNonce );
96
+ return computeAuthorizationHeader (consumerAuth , userAuth , signature , timestamp , percentEncodedNonce );
97
+ }
91
98
92
- StringBuilder sb = signatureBaseString (consumerAuth , userAuth , request , oauthTimestamp , percentEncodedNonce );
99
+ String computeSignature (ConsumerKey consumerAuth ,
100
+ RequestToken userAuth ,
101
+ Uri uri ,
102
+ String method ,
103
+ List <Param > formParams ,
104
+ List <Param > queryParams ,
105
+ long oauthTimestamp ,
106
+ String percentEncodedNonce ) throws InvalidKeyException {
107
+
108
+ StringBuilder sb = signatureBaseString (
109
+ consumerAuth ,
110
+ userAuth ,
111
+ uri ,
112
+ method ,
113
+ queryParams ,
114
+ formParams ,
115
+ oauthTimestamp ,
116
+ percentEncodedNonce );
93
117
94
118
ByteBuffer rawBase = StringUtils .charSequence2ByteBuffer (sb , UTF_8 );
95
119
byte [] rawSignature = digest (consumerAuth , userAuth , rawBase );
96
120
// and finally, base64 encoded... phew!
97
121
return Base64 .getEncoder ().encodeToString (rawSignature );
98
122
}
99
123
100
- StringBuilder signatureBaseString (ConsumerKey consumerAuth , RequestToken userAuth , Request request , long oauthTimestamp , String percentEncodedNonce ) {
124
+ StringBuilder signatureBaseString (ConsumerKey consumerAuth ,
125
+ RequestToken userAuth ,
126
+ Uri uri ,
127
+ String method ,
128
+ List <Param > formParams ,
129
+ List <Param > queryParams ,
130
+ long oauthTimestamp ,
131
+ String percentEncodedNonce ) {
101
132
102
133
// beware: must generate first as we're using pooled StringBuilder
103
- String baseUrl = request . getUri () .toBaseUrl ();
104
- String encodedParams = encodedParams (consumerAuth , userAuth , oauthTimestamp , percentEncodedNonce , request . getFormParams (), request . getQueryParams () );
134
+ String baseUrl = uri .toBaseUrl ();
135
+ String encodedParams = encodedParams (consumerAuth , userAuth , oauthTimestamp , percentEncodedNonce , formParams , queryParams );
105
136
106
137
StringBuilder sb = StringBuilderPool .DEFAULT .stringBuilder ();
107
- sb .append (request . getMethod () ); // POST / GET etc (nothing to URL encode)
138
+ sb .append (method ); // POST / GET etc (nothing to URL encode)
108
139
sb .append ('&' );
109
140
Utf8UrlEncoder .encodeAndAppendPercentEncoded (sb , baseUrl );
110
141
@@ -114,7 +145,12 @@ StringBuilder signatureBaseString(ConsumerKey consumerAuth, RequestToken userAut
114
145
return sb ;
115
146
}
116
147
117
- private String encodedParams (ConsumerKey consumerAuth , RequestToken userAuth , long oauthTimestamp , String percentEncodedNonce , List <Param > formParams , List <Param > queryParams ) {
148
+ private String encodedParams (ConsumerKey consumerAuth ,
149
+ RequestToken userAuth ,
150
+ long oauthTimestamp ,
151
+ String percentEncodedNonce ,
152
+ List <Param > formParams ,
153
+ List <Param > queryParams ) {
118
154
119
155
parameters .reset ();
120
156
@@ -168,7 +204,7 @@ private byte[] digest(ConsumerKey consumerAuth, RequestToken userAuth, ByteBuffe
168
204
return mac .doFinal ();
169
205
}
170
206
171
- String constructAuthHeader (ConsumerKey consumerAuth , RequestToken userAuth , String signature , long oauthTimestamp , String percentEncodedNonce ) {
207
+ String computeAuthorizationHeader (ConsumerKey consumerAuth , RequestToken userAuth , String signature , long oauthTimestamp , String percentEncodedNonce ) {
172
208
StringBuilder sb = StringBuilderPool .DEFAULT .stringBuilder ();
173
209
sb .append ("OAuth " );
174
210
sb .append (KEY_OAUTH_CONSUMER_KEY ).append ("=\" " ).append (consumerAuth .getPercentEncodedKey ()).append ("\" , " );
0 commit comments