/
BasicCustomAuthProviderTest.java
157 lines (138 loc) · 5.78 KB
/
BasicCustomAuthProviderTest.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
package io.dropwizard.auth.basic;
import com.codahale.metrics.MetricRegistry;
import io.dropwizard.auth.*;
import io.dropwizard.auth.util.AuthUtil;
import io.dropwizard.jersey.DropwizardResourceConfig;
import io.dropwizard.logging.BootstrapLogging;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.glassfish.jersey.servlet.ServletProperties;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.Test;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import java.security.Principal;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
public class BasicCustomAuthProviderTest extends JerseyTest {
final private static String VALID_ROLE = "ADMIN";
static {
BootstrapLogging.bootstrap();
}
@Override
protected TestContainerFactory getTestContainerFactory()
throws TestContainerException {
return new GrizzlyWebTestContainerFactory();
}
@Override
protected DeploymentContext configureDeployment() {
forceSet(TestProperties.CONTAINER_PORT, "0");
return ServletDeploymentContext.builder(new BasicAuthTestResourceConfig())
.initParam(ServletProperties.JAXRS_APPLICATION_CLASS, BasicAuthTestResourceConfig.class.getName())
.build();
}
@Test
public void respondsToMissingCredentialsWith401() throws Exception {
try {
target("/test").request().get(String.class);
failBecauseExceptionWasNotThrown(WebApplicationException.class);
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatus()).isEqualTo(401);
assertThat(e.getResponse().getHeaders().get(HttpHeaders.WWW_AUTHENTICATE))
.containsOnly("Custom realm=\"realm\"");
}
}
@Test
public void transformsCredentialsToPrincipals() throws Exception {
assertThat(target("/test").request()
.header(HttpHeaders.AUTHORIZATION, "Custom Z29vZC1ndXk6c2VjcmV0")
.get(String.class))
.isEqualTo("good-guy");
}
@Test
public void resourceWithAuthNotRequired200() {
assertThat(target("/test/authnotrequired").request()
.get(String.class))
.isEqualTo("No Principal");
}
@Test
public void resourceWithDenyAllAndNoAuth401() {
try {
target("/test/denied").request().get(String.class);
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatus()).isEqualTo(401);
}
}
@Test
public void resourceWithDenyAllAndAuth403() {
try {
target("/test/denied").request()
.header(HttpHeaders.AUTHORIZATION, "Custom Z29vZC1ndXk6c2VjcmV0")
.get(String.class);
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatus()).isEqualTo(403);
}
}
@Test
public void respondsToNonBasicCredentialsWith401() throws Exception {
try {
target("/test").request()
.header(HttpHeaders.AUTHORIZATION, "Derp Z29vZC1ndXk6c2VjcmV0")
.get(String.class);
failBecauseExceptionWasNotThrown(WebApplicationException.class);
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatus()).isEqualTo(401);
assertThat(e.getResponse().getHeaders().get(HttpHeaders.WWW_AUTHENTICATE))
.containsOnly("Custom realm=\"realm\"");
}
}
@Test
public void respondsToExceptionsWith500() throws Exception {
try {
target("/test").request()
.header(HttpHeaders.AUTHORIZATION, "Custom YmFkLWd1eTpzZWNyZXQ=")
.get(String.class);
failBecauseExceptionWasNotThrown(WebApplicationException.class);
} catch (WebApplicationException e) {
assertThat(e.getResponse().getStatus()).isEqualTo(500);
}
}
@Path("/test/")
@Produces(MediaType.TEXT_PLAIN)
public static class ExampleResource {
@RolesAllowed({VALID_ROLE})
@GET
public String show(@Context SecurityContext context) {
return context.getUserPrincipal().getName();
}
}
public static class BasicAuthTestResourceConfig extends DropwizardResourceConfig {
public BasicAuthTestResourceConfig() {
super(true, new MetricRegistry());
register(new AuthDynamicFeature(getAuthFilter()));
register(RolesAllowedDynamicFeature.class);
register(AuthResource.class);
}
private ContainerRequestFilter getAuthFilter() {
final String validUser = "good-guy";
BasicCredentialAuthFilter.Builder<Principal> builder = new BasicCredentialAuthFilter.Builder<>();
builder.setAuthorizer(AuthUtil.getTestAuthorizer(validUser, VALID_ROLE));
builder.setAuthenticator(AuthUtil.getTestAuthenticatorBasicCredential(validUser));
builder.setPrefix("Custom");
return builder.buildAuthFilter();
}
}
}