2
2
3
3
import com .github .scribejava .core .builder .ServiceBuilder ;
4
4
import com .github .scribejava .core .model .OAuth2AccessToken ;
5
+ import com .github .scribejava .core .model .OAuth2Authorization ;
5
6
import com .github .scribejava .core .model .OAuthConstants ;
6
7
import com .github .scribejava .core .services .Base64Encoder ;
7
8
import com .google .gson .Gson ;
8
9
import com .google .gson .reflect .TypeToken ;
9
- import org .junit .Assert ;
10
+ import static org .junit .Assert .assertNotNull ;
11
+ import static org .junit .Assert .assertEquals ;
10
12
import org .junit .Test ;
11
13
12
14
import java .nio .charset .Charset ;
@@ -25,23 +27,23 @@ public void shouldProduceCorrectRequestSync() {
25
27
final OAuth2AccessToken token =service .getAccessTokenPasswordGrant ("user1" ,"password1" );
26
28
final Gson json =new Gson ();
27
29
28
- Assert . assertNotNull (token );
30
+ assertNotNull (token );
29
31
30
32
final Map <String ,String >map =json .fromJson (token .getRawResponse (),new TypeTokenImpl ().getType ());
31
33
32
- Assert . assertEquals (OAuth20ServiceUnit .TOKEN ,map .get (OAuthConstants .ACCESS_TOKEN ));
33
- Assert . assertEquals (OAuth20ServiceUnit .STATE ,map .get (OAuthConstants .STATE ));
34
- Assert . assertEquals (OAuth20ServiceUnit .EXPIRES ,map .get ("expires_in" ));
34
+ assertEquals (OAuth20ServiceUnit .TOKEN ,map .get (OAuthConstants .ACCESS_TOKEN ));
35
+ assertEquals (OAuth20ServiceUnit .STATE ,map .get (OAuthConstants .STATE ));
36
+ assertEquals (OAuth20ServiceUnit .EXPIRES ,map .get ("expires_in" ));
35
37
36
38
final String authorize =Base64Encoder .getInstance ()
37
39
.encode (String .format ("%s:%s" ,service .getConfig ().getApiKey (),service .getConfig ().getApiSecret ())
38
40
.getBytes (Charset .forName ("UTF-8" )));
39
41
40
- Assert . assertEquals (OAuthConstants .BASIC +" " +authorize ,map .get (OAuthConstants .HEADER ));
42
+ assertEquals (OAuthConstants .BASIC +" " +authorize ,map .get (OAuthConstants .HEADER ));
41
43
42
- Assert . assertEquals ("user1" ,map .get ("query-username" ));
43
- Assert . assertEquals ("password1" ,map .get ("query-password" ));
44
- Assert . assertEquals ("password" ,map .get ("query-grant_type" ));
44
+ assertEquals ("user1" ,map .get ("query-username" ));
45
+ assertEquals ("password1" ,map .get ("query-password" ));
46
+ assertEquals ("password" ,map .get ("query-grant_type" ));
45
47
}
46
48
47
49
@ Test
@@ -54,23 +56,72 @@ public void shouldProduceCorrectRequestAsync() throws ExecutionException, Interr
54
56
final OAuth2AccessToken token =service .getAccessTokenPasswordGrantAsync ("user1" ,"password1" ,null ).get ();
55
57
final Gson json =new Gson ();
56
58
57
- Assert . assertNotNull (token );
59
+ assertNotNull (token );
58
60
59
61
final Map <String ,String >map =json .fromJson (token .getRawResponse (),new TypeTokenImpl ().getType ());
60
62
61
- Assert . assertEquals (OAuth20ServiceUnit .TOKEN ,map .get (OAuthConstants .ACCESS_TOKEN ));
62
- Assert . assertEquals (OAuth20ServiceUnit .STATE ,map .get (OAuthConstants .STATE ));
63
- Assert . assertEquals (OAuth20ServiceUnit .EXPIRES ,map .get ("expires_in" ));
63
+ assertEquals (OAuth20ServiceUnit .TOKEN ,map .get (OAuthConstants .ACCESS_TOKEN ));
64
+ assertEquals (OAuth20ServiceUnit .STATE ,map .get (OAuthConstants .STATE ));
65
+ assertEquals (OAuth20ServiceUnit .EXPIRES ,map .get ("expires_in" ));
64
66
65
67
final String authorize =Base64Encoder .getInstance ()
66
68
.encode (String .format ("%s:%s" ,service .getConfig ().getApiKey (),service .getConfig ().getApiSecret ())
67
69
.getBytes (Charset .forName ("UTF-8" )));
68
70
69
- Assert .assertEquals (OAuthConstants .BASIC +" " +authorize ,map .get (OAuthConstants .HEADER ));
71
+ assertEquals (OAuthConstants .BASIC +" " +authorize ,map .get (OAuthConstants .HEADER ));
72
+
73
+ assertEquals ("user1" ,map .get ("query-username" ));
74
+ assertEquals ("password1" ,map .get ("query-password" ));
75
+ assertEquals ("password" ,map .get ("query-grant_type" ));
76
+ }
77
+
78
+ @ Test
79
+ public void testOAuthExtractAuthorization () {
80
+ final OAuth20Service service =new ServiceBuilder ()
81
+ .apiKey ("your_api_key" )
82
+ .apiSecret ("your_api_secret" )
83
+ .build (new OAuth20ApiUnit ());
84
+
85
+ OAuth2Authorization authorization =service .extractAuthorization ("https://cl.ex.com/cb?code=SplxlOB&state=xyz" );
86
+ assertEquals ("SplxlOB" ,authorization .getCode ());
87
+ assertEquals ("xyz" ,authorization .getState ());
88
+
89
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?state=xyz&code=SplxlOB" );
90
+ assertEquals ("SplxlOB" ,authorization .getCode ());
91
+ assertEquals ("xyz" ,authorization .getState ());
92
+
93
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?key=value&state=xyz&code=SplxlOB" );
94
+ assertEquals ("SplxlOB" ,authorization .getCode ());
95
+ assertEquals ("xyz" ,authorization .getState ());
96
+
97
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?state=xyz&code=SplxlOB&key=value&" );
98
+ assertEquals ("SplxlOB" ,authorization .getCode ());
99
+ assertEquals ("xyz" ,authorization .getState ());
100
+
101
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?code=SplxlOB&state=" );
102
+ assertEquals ("SplxlOB" ,authorization .getCode ());
103
+ assertEquals (null ,authorization .getState ());
104
+
105
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?code=SplxlOB" );
106
+ assertEquals ("SplxlOB" ,authorization .getCode ());
107
+ assertEquals (null ,authorization .getState ());
108
+
109
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?code=" );
110
+ assertEquals (null ,authorization .getCode ());
111
+ assertEquals (null ,authorization .getState ());
112
+
113
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?code" );
114
+ assertEquals (null ,authorization .getCode ());
115
+ assertEquals (null ,authorization .getState ());
116
+
117
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb?" );
118
+ assertEquals (null ,authorization .getCode ());
119
+ assertEquals (null ,authorization .getState ());
120
+
121
+ authorization =service .extractAuthorization ("https://cl.ex.com/cb" );
122
+ assertEquals (null ,authorization .getCode ());
123
+ assertEquals (null ,authorization .getState ());
70
124
71
- Assert .assertEquals ("user1" ,map .get ("query-username" ));
72
- Assert .assertEquals ("password1" ,map .get ("query-password" ));
73
- Assert .assertEquals ("password" ,map .get ("query-grant_type" ));
74
125
}
75
126
76
127
private static class TypeTokenImpl extends TypeToken <Map <String ,String >> {