-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
DataSourceLoginServiceTest.java
175 lines (154 loc) · 6.28 KB
/
DataSourceLoginServiceTest.java
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
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.AuthenticationStore;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.plus.security.DataSourceLoginService;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.Loader;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mariadb.jdbc.MariaDbDataSource;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* DataSourceLoginServiceTest
*/
@Testcontainers(disabledWithoutDocker = true)
public class DataSourceLoginServiceTest
{
private static final String _content = "This is some protected content";
private static String REALM_NAME = "DSRealm";
private static File __docRoot;
private static URI __baseUri;
private static DatabaseLoginServiceTestServer __testServer;
private AuthenticationStore _authStore;
private HttpClient _client;
@BeforeAll
public static void setUp() throws Exception
{
__docRoot = MavenTestingUtils.getTargetTestingDir("dsloginservice-test");
FS.ensureDirExists(__docRoot);
File content = new File(__docRoot, "input.txt");
try (FileOutputStream out = new FileOutputStream(content))
{
out.write(_content.getBytes("utf-8"));
}
//create a datasource and bind to jndi
MariaDbDataSource ds = new MariaDbDataSource();
ds.setDatabaseName(DatabaseLoginServiceTestServer.MARIA_DB_NAME);
ds.setUser(DatabaseLoginServiceTestServer.MARIA_DB_USER);
ds.setPassword(DatabaseLoginServiceTestServer.MARIA_DB_PASSWORD);
ds.setUrl(DatabaseLoginServiceTestServer.MARIA_DB_FULL_URL);
org.eclipse.jetty.plus.jndi.Resource binding =
new org.eclipse.jetty.plus.jndi.Resource(null, "dstest", ds);
__testServer = new DatabaseLoginServiceTestServer();
DataSourceLoginService loginService = new DataSourceLoginService();
loginService.setUserTableName("users");
loginService.setUserTableKey("id");
loginService.setUserTableUserField("username");
loginService.setUserTablePasswordField("pwd");
loginService.setRoleTableName("roles");
loginService.setRoleTableKey("id");
loginService.setRoleTableRoleField("role");
loginService.setUserRoleTableName("user_roles");
loginService.setUserRoleTableRoleKey("role_id");
loginService.setUserRoleTableUserKey("user_id");
loginService.setJndiName("dstest");
loginService.setName(REALM_NAME);
loginService.setServer(__testServer.getServer());
__testServer.setResourceBase(__docRoot.getAbsolutePath());
__testServer.setLoginService(loginService);
__testServer.start();
__baseUri = __testServer.getBaseUri();
}
@AfterAll
public static void tearDown()
throws Exception
{
if (__testServer != null)
{
__testServer.stop();
__testServer = null;
}
}
@BeforeEach
public void setupClient() throws Exception
{
_client = new HttpClient();
_authStore = _client.getAuthenticationStore();
}
@AfterEach
public void stopClient() throws Exception
{
if (_client != null)
{
_client.stop();
_client = null;
}
}
@Test
public void testGetAndPasswordUpdate() throws Exception
{
try
{
_authStore.addAuthentication(new BasicAuthentication(__baseUri, REALM_NAME, "dstest", "dstest"));
_client.start();
ContentResponse response = _client.GET(__baseUri.resolve("input.txt"));
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals(_content, response.getContentAsString());
stopClient();
String newpwd = String.valueOf(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
changePassword("dstest", newpwd);
setupClient();
_authStore.addAuthentication(new BasicAuthentication(__baseUri, REALM_NAME, "dstest", newpwd));
_client.start();
response = _client.GET(__baseUri.resolve("input.txt"));
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals(_content, response.getContentAsString());
}
finally
{
changePassword("dstest", "dstest");
}
}
protected void changePassword(String user, String newpwd) throws Exception
{
Loader.loadClass(DatabaseLoginServiceTestServer.MARIA_DB_DRIVER_CLASS);
try (Connection connection = DriverManager.getConnection(DatabaseLoginServiceTestServer.MARIA_DB_FULL_URL);
Statement stmt = connection.createStatement())
{
connection.setAutoCommit(true);
stmt.executeUpdate("update users set pwd='" + newpwd + "' where username='" + user + "'");
}
}
}