Skip to content

Commit 7d3554f

Browse files
Create TestJWTAuthToken.java
1 parent 44cf2f8 commit 7d3554f

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package org.privacyidea;
2+
3+
import com.auth0.jwt.JWT;
4+
import com.auth0.jwt.algorithms.Algorithm;
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.mockserver.integration.ClientAndServer;
9+
import org.mockserver.model.HttpRequest;
10+
import org.mockserver.model.HttpResponse;
11+
12+
import java.util.Date;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
public class TestJWTAuthToken
17+
{
18+
private ClientAndServer mockServer;
19+
private String authToken;
20+
21+
@Before
22+
public void setup()
23+
{
24+
mockServer = ClientAndServer.startClientAndServer(1080);
25+
}
26+
27+
/**
28+
* Test if the JWT auth token is updated after the expiration time.
29+
*/
30+
@Test
31+
public void testSuccess()
32+
{
33+
String serviceAccount = "admin";
34+
String servicePassword = "admin";
35+
36+
// Pre-set the auth token
37+
authToken = getAuthToken();
38+
39+
mockServer.when(HttpRequest.request()
40+
.withPath(PIConstants.ENDPOINT_AUTH)
41+
.withMethod("POST")
42+
.withBody("username=" + serviceAccount + "&password=" + servicePassword))
43+
.respond(HttpResponse.response()
44+
.withBody(postAuthSuccessResponse()));
45+
46+
PrivacyIDEA privacyIDEA = PrivacyIDEA.newBuilder("https://127.0.0.1:1080", "test")
47+
.serviceAccount(serviceAccount, servicePassword)
48+
.httpTimeoutMs(15000)
49+
.verifySSL(false)
50+
.logger(new PILogImplementation())
51+
.simpleLogger(System.out::println)
52+
.build();
53+
54+
// Check if the auth token is updated after expiration time
55+
for (int i = 0; i < 2; i++)
56+
{
57+
// Compare the tokens
58+
assertEquals(authToken, privacyIDEA.authToken);
59+
60+
System.out.println("Expected: " + authToken);
61+
System.out.println("Actual : " + privacyIDEA.authToken);
62+
System.out.println(i + 1 + "/3 auth token test passed!");
63+
64+
// Actualize the auth token
65+
authToken = getAuthToken();
66+
67+
// Reset the mock server response
68+
mockServer.clear(HttpRequest.request()
69+
.withPath(PIConstants.ENDPOINT_AUTH)
70+
.withMethod("POST")
71+
.withBody("username=" + serviceAccount + "&password=" + servicePassword));
72+
73+
mockServer.when(HttpRequest.request()
74+
.withPath(PIConstants.ENDPOINT_AUTH)
75+
.withMethod("POST")
76+
.withBody("username=" + serviceAccount + "&password=" + servicePassword))
77+
.respond(HttpResponse.response()
78+
.withBody(postAuthSuccessResponse()));
79+
80+
// Wait 5 seconds for a new token
81+
try
82+
{
83+
Thread.sleep(3000);
84+
}
85+
catch (InterruptedException e)
86+
{
87+
Thread.currentThread().interrupt();
88+
}
89+
}
90+
91+
assertEquals(authToken, privacyIDEA.authToken);
92+
System.out.println("Expected: " + authToken);
93+
System.out.println("Actual : " + privacyIDEA.authToken);
94+
System.out.println("3/3 auth token test passed!");
95+
}
96+
97+
@After
98+
public void tearDown()
99+
{
100+
mockServer.stop();
101+
}
102+
103+
/**
104+
* Create the auth tokens substitute.
105+
* This method is not used in the test, but it is used in the main code.
106+
*
107+
* @return String - the auth token
108+
*/
109+
private String getAuthToken()
110+
{
111+
System.out.println("JWT test token's expiration date: " + new Date(System.currentTimeMillis() + 65000));
112+
return JWT.create()
113+
.withSubject("testUser")
114+
.withIssuer("testIssuer")
115+
.withExpiresAt(new Date(System.currentTimeMillis() + 65000))
116+
.sign(Algorithm.HMAC256("testSecret"));
117+
}
118+
119+
private String postAuthSuccessResponse()
120+
{
121+
return "{\n" + " \"id\": 1,\n" +
122+
" \"jsonrpc\": \"2.0\",\n" +
123+
" \"result\": {\n" +
124+
" \"status\": true,\n" +
125+
" \"value\": {\n" +
126+
" \"log_level\": 20,\n" +
127+
" \"menus\": [\n" +
128+
" \"components\",\n" +
129+
" \"machines\"\n" +
130+
" ],\n" +
131+
" \"realm\": \"\",\n" +
132+
" \"rights\": [\n" +
133+
" \"policydelete\",\n" +
134+
" \"resync\"\n" +
135+
" ],\n" +
136+
" \"role\": \"admin\",\n" +
137+
" \"token\": \"" +
138+
authToken + "\",\n" +
139+
" \"username\": \"admin\",\n" +
140+
" \"logout_time\": 120,\n" +
141+
" \"default_tokentype\": \"hotp\",\n" +
142+
" \"user_details\": false,\n" +
143+
" \"subscription_status\": 0\n" +
144+
" }\n" + " },\n" +
145+
" \"time\": " + (System.currentTimeMillis() / 1000L) + ",\n" +
146+
" \"version\": \"privacyIDEA 3.2.1\",\n" +
147+
" \"versionnumber\": \"3.2.1\",\n" +
148+
" \"signature\": \"rsa_sha256_pss:\"\n" +
149+
"}";
150+
}
151+
}

0 commit comments

Comments
 (0)