|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | +package org.opensearch.security.http; |
| 11 | + |
| 12 | +import java.security.KeyPair; |
| 13 | +import java.util.Base64; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 18 | +import org.apache.http.Header; |
| 19 | +import org.junit.ClassRule; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | + |
| 24 | +import org.opensearch.test.framework.JwtConfigBuilder; |
| 25 | +import org.opensearch.test.framework.TestSecurityConfig; |
| 26 | +import org.opensearch.test.framework.cluster.ClusterManager; |
| 27 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 28 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 29 | +import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; |
| 30 | +import org.opensearch.test.framework.log.LogsRule; |
| 31 | + |
| 32 | +import io.jsonwebtoken.SignatureAlgorithm; |
| 33 | +import io.jsonwebtoken.security.Keys; |
| 34 | + |
| 35 | +import static java.nio.charset.StandardCharsets.US_ASCII; |
| 36 | +import static org.apache.http.HttpHeaders.AUTHORIZATION; |
| 37 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | +import static org.hamcrest.Matchers.equalTo; |
| 39 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; |
| 40 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.BASIC_AUTH_DOMAIN_ORDER; |
| 41 | +import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; |
| 42 | + |
| 43 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 44 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 45 | +public class JwtAuthenticationWithUrlParamTests { |
| 46 | + |
| 47 | + public static final String CLAIM_USERNAME = "preferred-username"; |
| 48 | + public static final String CLAIM_ROLES = "backend-user-roles"; |
| 49 | + public static final String POINTER_USERNAME = "/user_name"; |
| 50 | + |
| 51 | + private static final KeyPair KEY_PAIR = Keys.keyPairFor(SignatureAlgorithm.RS256); |
| 52 | + private static final String PUBLIC_KEY = new String(Base64.getEncoder().encode(KEY_PAIR.getPublic().getEncoded()), US_ASCII); |
| 53 | + |
| 54 | + static final TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS); |
| 55 | + |
| 56 | + private static final String TOKEN_URL_PARAM = "token"; |
| 57 | + |
| 58 | + private static final JwtAuthorizationHeaderFactory tokenFactory = new JwtAuthorizationHeaderFactory( |
| 59 | + KEY_PAIR.getPrivate(), |
| 60 | + CLAIM_USERNAME, |
| 61 | + CLAIM_ROLES, |
| 62 | + AUTHORIZATION |
| 63 | + ); |
| 64 | + |
| 65 | + public static final TestSecurityConfig.AuthcDomain JWT_AUTH_DOMAIN = new TestSecurityConfig.AuthcDomain( |
| 66 | + "jwt", |
| 67 | + BASIC_AUTH_DOMAIN_ORDER - 1 |
| 68 | + ).jwtHttpAuthenticator( |
| 69 | + new JwtConfigBuilder().jwtUrlParameter(TOKEN_URL_PARAM).signingKey(PUBLIC_KEY).subjectKey(CLAIM_USERNAME).rolesKey(CLAIM_ROLES) |
| 70 | + ).backend("noop"); |
| 71 | + |
| 72 | + @ClassRule |
| 73 | + public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE) |
| 74 | + .anonymousAuth(false) |
| 75 | + .nodeSettings( |
| 76 | + Map.of("plugins.security.restapi.roles_enabled", List.of("user_" + ADMIN_USER.getName() + "__" + ALL_ACCESS.getName())) |
| 77 | + ) |
| 78 | + .authc(AUTHC_HTTPBASIC_INTERNAL) |
| 79 | + .authc(JWT_AUTH_DOMAIN) |
| 80 | + .users(ADMIN_USER) |
| 81 | + .build(); |
| 82 | + |
| 83 | + @Rule |
| 84 | + public LogsRule logsRule = new LogsRule("com.amazon.dlic.auth.http.jwt.HTTPJwtAuthenticator"); |
| 85 | + |
| 86 | + @Test |
| 87 | + public void shouldAuthenticateWithJwtTokenInUrl_positive() { |
| 88 | + Header jwtToken = tokenFactory.generateValidToken(ADMIN_USER.getName()); |
| 89 | + String jwtTokenValue = jwtToken.getValue(); |
| 90 | + try (TestRestClient client = cluster.getRestClient()) { |
| 91 | + HttpResponse response = client.getAuthInfo(Map.of(TOKEN_URL_PARAM, jwtTokenValue)); |
| 92 | + |
| 93 | + response.assertStatusCode(200); |
| 94 | + String username = response.getTextFromJsonBody(POINTER_USERNAME); |
| 95 | + assertThat(username, equalTo(ADMIN_USER.getName())); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testUnauthenticatedRequest() { |
| 101 | + try (TestRestClient client = cluster.getRestClient()) { |
| 102 | + HttpResponse response = client.getAuthInfo(); |
| 103 | + |
| 104 | + response.assertStatusCode(401); |
| 105 | + logsRule.assertThatContainExactly(String.format("No JWT token found in '%s' url parameter header", TOKEN_URL_PARAM)); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments