-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHereAccessTokenProviderIT.java
More file actions
251 lines (216 loc) · 10.9 KB
/
Copy pathHereAccessTokenProviderIT.java
File metadata and controls
251 lines (216 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (c) 2018 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.here.account.oauth2;
import com.here.account.auth.OAuth1ClientCredentialsProvider;
import com.here.account.auth.OAuth1Signer;
import com.here.account.auth.OAuth1SignerExposer;
import com.here.account.auth.provider.ClientAuthorizationProviderChain;
import com.here.account.auth.provider.FromDefaultHereCredentialsPropertiesFileExposer;
import com.here.account.auth.provider.FromHereCredentialsIniFile;
import com.here.account.auth.provider.FromHereCredentialsIniStream;
import com.here.account.http.HttpProvider;
import com.here.account.http.java.JavaHttpProvider;
import com.here.account.util.Clock;
import com.here.account.util.SettableSystemClock;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map.Entry;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author kmccrack
*/
public class HereAccessTokenProviderIT {
@Test
public void test_builder_basic() throws IOException {
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder().build()
) {
do_basic(accessTokens);
}
}
protected void do_basic(HereAccessTokenProvider accessTokens) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
AccessTokenResponse accessTokenResponse = accessTokens.getAccessTokenResponse();
assertTrue("accessTokenResponse was null", null != accessTokenResponse);
assertEquals("tokenType invalid", "bearer", accessTokenResponse.getTokenType());
}
@Test
public void test_JavaHttpProvider() throws IOException {
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder().setHttpProvider(JavaHttpProvider.builder().build()).build();
) {
do_basic(accessTokens);
}
}
private static final int ONE_HOUR_SKEW_MILLIS = 60 * 60 * 1000;
@Test
public void test_builder_clockskew_spy() throws IOException, NoSuchFieldException, IllegalAccessException {
SettableSystemClock clock = new SettableSystemClock();
ClientAuthorizationProviderChain provider = ClientAuthorizationProviderChain
.getNewDefaultClientCredentialsProviderChain(clock);
ClientAuthorizationRequestProvider mockProvider = Mockito.spy(provider);
OAuth1Signer oAuth1Signer = (OAuth1Signer) provider.getClientAuthorizer();
String accessKeyId = OAuth1SignerExposer.getAccessKeyId(oAuth1Signer);
String accessKeySecret = OAuth1SignerExposer.getAccessKeySecret(oAuth1Signer);
clock.setCurrentTimeMillis(0L);
HttpProvider.HttpRequestAuthorizer myAuthorizer = new OAuth1Signer(clock, accessKeyId, accessKeySecret);
Mockito.doReturn(myAuthorizer).when(mockProvider).getClientAuthorizer();
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder()
.setClientAuthorizationRequestProvider(mockProvider)
.setAlwaysRequestNewToken(true)
.build()
) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
AccessTokenResponse accessTokenResponse = accessTokens.getAccessTokenResponse();
assertTrue("accessTokenResponse was null", null != accessTokenResponse);
assertEquals("tokenType invalid", "bearer", accessTokenResponse.getTokenType());
}
}
@Test
public void test_builder_clockskew() throws IOException, NoSuchFieldException, IllegalAccessException {
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder()
.setAlwaysRequestNewToken(true)
.build()
) {
for (int i = 0; i < 2; i++) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
AccessTokenResponse accessTokenResponse = accessTokens.getAccessTokenResponse();
assertTrue("accessTokenResponse was null", null != accessTokenResponse);
assertEquals("tokenType invalid", "bearer", accessTokenResponse.getTokenType());
}
}
}
@Test
public void test_builder_basic_multipleTokens() throws IOException {
do_builder_basic(10);
}
protected void do_builder_basic(int numTokens) throws IOException {
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder().build()
) {
for (int i = 0; i < numTokens; i++) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
}
}
}
@Ignore // we don't yet return credentials.ini files from our APIs
@Test
public void test_builder_ini() throws IOException {
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder()
.setClientAuthorizationRequestProvider(new FromHereCredentialsIniFile())
.build()
) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
}
}
@Test
public void test_builder_iniStream() throws IOException {
Clock clock = new SettableSystemClock();
try (
InputStream inputStream = getTestIniFromOther();
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder()
.setClientAuthorizationRequestProvider(new FromHereCredentialsIniStream(clock, inputStream))
.build()
) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
}
}
protected boolean notEmpty(String s) {
return null != s && s.length() > 0;
}
/**
* Using the file ~/.here/credentials.properties, create some fake credentials.ini
* content by prepending the default section in memory, returning an InputStream to it.
*
* @return
* @throws IOException
*/
protected InputStream getTestIniFromOther() throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
outputStream.write("[default]\n".getBytes(StandardCharsets.UTF_8));
File file = FromDefaultHereCredentialsPropertiesFileExposer.getDefaultHereCredentialsFile();
// System properties first
String tokenEndpointUrl = System.getProperty(OAuth1ClientCredentialsProvider.FromProperties.TOKEN_ENDPOINT_URL_PROPERTY);
String accessKeyId = System.getProperty(OAuth1ClientCredentialsProvider.FromProperties.ACCESS_KEY_ID_PROPERTY);
String accessKeySecret = System.getProperty(OAuth1ClientCredentialsProvider.FromProperties.ACCESS_KEY_SECRET_PROPERTY);
String scope = System.getProperty(OAuth1ClientCredentialsProvider.FromProperties.TOKEN_SCOPE_PROPERTY);
if (notEmpty(tokenEndpointUrl) && notEmpty(accessKeyId) && notEmpty(accessKeySecret)) {
outputStream.write((OAuth1ClientCredentialsProvider.FromProperties.TOKEN_ENDPOINT_URL_PROPERTY
+ "=" + tokenEndpointUrl + "\n").getBytes(StandardCharsets.UTF_8));
outputStream.write((OAuth1ClientCredentialsProvider.FromProperties.ACCESS_KEY_ID_PROPERTY
+ "=" + accessKeyId + "\n").getBytes(StandardCharsets.UTF_8));
outputStream.write((OAuth1ClientCredentialsProvider.FromProperties.ACCESS_KEY_SECRET_PROPERTY
+ "=" + accessKeySecret + "\n").getBytes(StandardCharsets.UTF_8));
// scope is optional
if (notEmpty(scope)) {
outputStream.write((OAuth1ClientCredentialsProvider.FromProperties.TOKEN_SCOPE_PROPERTY
+ "=" + scope + "\n").getBytes(StandardCharsets.UTF_8));
}
} else {
Properties properties = OAuth1ClientCredentialsProvider.getPropertiesFromFile(file);
for (Entry<Object, Object> property : properties.entrySet()) {
Object name = property.getKey();
Object value = property.getValue();
String line = name + "=" + value + "\n";
outputStream.write(line.getBytes(StandardCharsets.UTF_8));
}
}
outputStream.flush();
byte[] bytes = outputStream.toByteArray();
System.out.println("configs:\n"+(new String(bytes, StandardCharsets.UTF_8))+"\n");
return new ByteArrayInputStream(bytes);
}
}
@Test
public void test_alwaysRequestNewToken() throws IOException {
try (
HereAccessTokenProvider accessTokens = HereAccessTokenProvider.builder()
.setAlwaysRequestNewToken(true)
.build()
) {
String accessToken = accessTokens.getAccessToken();
assertTrue("accessToken was null", null != accessToken);
assertTrue("accessToken was blank", accessToken.trim().length() > 0);
AccessTokenResponse accessTokenResponse = accessTokens.getAccessTokenResponse();
assertTrue("accessTokenResponse was null", null != accessTokenResponse);
assertEquals("tokenType invalid", "bearer", accessTokenResponse.getTokenType());
}
}
@Test (expected = NullPointerException.class)
public void test_nullPropertyFile() throws IOException {
OAuth1ClientCredentialsProvider.getPropertiesFromFile(null);
}
}