| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.io.FileOutputStream; |
| 23 |
import java.net.URI; |
| 24 |
import java.sql.Connection; |
| 25 |
import java.sql.DriverManager; |
| 26 |
import java.sql.Statement; |
| 27 |
import java.util.concurrent.TimeUnit; |
| 28 |
import javax.servlet.http.HttpServletResponse; |
| 29 |
|
| 30 |
import org.eclipse.jetty.client.HttpClient; |
| 31 |
import org.eclipse.jetty.client.api.AuthenticationStore; |
| 32 |
import org.eclipse.jetty.client.api.ContentResponse; |
| 33 |
import org.eclipse.jetty.client.util.BasicAuthentication; |
| 34 |
import org.eclipse.jetty.plus.security.DataSourceLoginService; |
| 35 |
import org.eclipse.jetty.toolchain.test.FS; |
| 36 |
import org.eclipse.jetty.toolchain.test.MavenTestingUtils; |
| 37 |
import org.eclipse.jetty.util.Loader; |
| 38 |
import org.junit.jupiter.api.AfterAll; |
| 39 |
import org.junit.jupiter.api.AfterEach; |
| 40 |
import org.junit.jupiter.api.BeforeAll; |
| 41 |
import org.junit.jupiter.api.BeforeEach; |
| 42 |
import org.junit.jupiter.api.Test; |
| 43 |
import org.mariadb.jdbc.MariaDbDataSource; |
| 44 |
import org.testcontainers.junit.jupiter.Testcontainers; |
| 45 |
|
| 46 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 47 |
|
| 48 |
/** |
| 49 |
* DataSourceLoginServiceTest |
| 50 |
*/ |
| 51 |
@Testcontainers(disabledWithoutDocker = true) |
| 52 |
public class DataSourceLoginServiceTest |
| 53 |
{ |
| 54 |
private static final String _content = "This is some protected content"; |
| 55 |
private static String REALM_NAME = "DSRealm"; |
| 56 |
private static File __docRoot; |
| 57 |
private static URI __baseUri; |
| 58 |
private static DatabaseLoginServiceTestServer __testServer; |
| 59 |
private AuthenticationStore _authStore; |
| 60 |
private HttpClient _client; |
| 61 |
|
| 62 |
@BeforeAll |
| 63 |
public static void setUp() throws Exception |
| 64 |
{ |
| 65 |
__docRoot = MavenTestingUtils.getTargetTestingDir("dsloginservice-test"); |
| 66 |
FS.ensureDirExists(__docRoot); |
| 67 |
|
| 68 |
File content = new File(__docRoot, "input.txt"); |
| 69 |
try (FileOutputStream out = new FileOutputStream(content)) |
| 70 |
{ |
| 71 |
out.write(_content.getBytes("utf-8")); |
| 72 |
} |
| 73 |
|
| 74 |
//create a datasource and bind to jndi |
| 75 |
MariaDbDataSource ds = new MariaDbDataSource(); |
| 76 |
ds.setUser(DatabaseLoginServiceTestServer.MARIA_DB_USER); |
| 77 |
ds.setPassword(DatabaseLoginServiceTestServer.MARIA_DB_PASSWORD); |
| 78 |
ds.setUrl(DatabaseLoginServiceTestServer.MARIA_DB_FULL_URL); |
| 79 |
org.eclipse.jetty.plus.jndi.Resource binding = |
| 80 |
new org.eclipse.jetty.plus.jndi.Resource(null, "dstest", ds); |
| 81 |
|
| 82 |
__testServer = new DatabaseLoginServiceTestServer(); |
| 83 |
|
| 84 |
DataSourceLoginService loginService = new DataSourceLoginService(); |
| 85 |
loginService.setUserTableName("users"); |
| 86 |
loginService.setUserTableKey("id"); |
| 87 |
loginService.setUserTableUserField("username"); |
| 88 |
loginService.setUserTablePasswordField("pwd"); |
| 89 |
loginService.setRoleTableName("roles"); |
| 90 |
loginService.setRoleTableKey("id"); |
| 91 |
loginService.setRoleTableRoleField("role"); |
| 92 |
loginService.setUserRoleTableName("user_roles"); |
| 93 |
loginService.setUserRoleTableRoleKey("role_id"); |
| 94 |
loginService.setUserRoleTableUserKey("user_id"); |
| 95 |
loginService.setJndiName("dstest"); |
| 96 |
loginService.setName(REALM_NAME); |
| 97 |
loginService.setServer(__testServer.getServer()); |
| 98 |
|
| 99 |
__testServer.setResourceBase(__docRoot.getAbsolutePath()); |
| 100 |
__testServer.setLoginService(loginService); |
| 101 |
__testServer.start(); |
| 102 |
__baseUri = __testServer.getBaseUri(); |
| 103 |
} |
| 104 |
|
| 105 |
@AfterAll |
| 106 |
public static void tearDown() |
| 107 |
throws Exception |
| 108 |
{ |
| 109 |
if (__testServer != null) |
| 110 |
{ |
| 111 |
__testServer.stop(); |
| 112 |
__testServer = null; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
@BeforeEach |
| 117 |
public void setupClient() throws Exception |
| 118 |
{ |
| 119 |
_client = new HttpClient(); |
| 120 |
_authStore = _client.getAuthenticationStore(); |
| 121 |
} |
| 122 |
|
| 123 |
@AfterEach |
| 124 |
public void stopClient() throws Exception |
| 125 |
{ |
| 126 |
if (_client != null) |
| 127 |
{ |
| 128 |
_client.stop(); |
| 129 |
_client = null; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void testGetAndPasswordUpdate() throws Exception |
| 135 |
{ |
| 136 |
try |
| 137 |
{ |
| 138 |
_authStore.addAuthentication(new BasicAuthentication(__baseUri, REALM_NAME, "dstest", "dstest")); |
| 139 |
_client.start(); |
| 140 |
ContentResponse response = _client.GET(__baseUri.resolve("input.txt")); |
| 141 |
assertEquals(HttpServletResponse.SC_OK, response.getStatus()); |
| 142 |
assertEquals(_content, response.getContentAsString()); |
| 143 |
|
| 144 |
stopClient(); |
| 145 |
|
| 146 |
String newpwd = String.valueOf(TimeUnit.NANOSECONDS.toMillis(System.nanoTime())); |
| 147 |
|
| 148 |
changePassword("dstest", newpwd); |
| 149 |
|
| 150 |
setupClient(); |
| 151 |
_authStore.addAuthentication(new BasicAuthentication(__baseUri, REALM_NAME, "dstest", newpwd)); |
| 152 |
_client.start(); |
| 153 |
|
| 154 |
response = _client.GET(__baseUri.resolve("input.txt")); |
| 155 |
assertEquals(HttpServletResponse.SC_OK, response.getStatus()); |
| 156 |
assertEquals(_content, response.getContentAsString()); |
| 157 |
} |
| 158 |
finally |
| 159 |
{ |
| 160 |
changePassword("dstest", "dstest"); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
protected void changePassword(String user, String newpwd) throws Exception |
| 165 |
{ |
| 166 |
Loader.loadClass(DatabaseLoginServiceTestServer.MARIA_DB_DRIVER_CLASS); |
| 167 |
try (Connection connection = DriverManager.getConnection(DatabaseLoginServiceTestServer.MARIA_DB_FULL_URL); |
| 168 |
Statement stmt = connection.createStatement()) |
| 169 |
{ |
| 170 |
connection.setAutoCommit(true); |
| 171 |
stmt.executeUpdate("update users set pwd='" + newpwd + "' where username='" + user + "'"); |
| 172 |
} |
| 173 |
} |
| 174 |
} |