use of io.trino.server.ui.OAuthWebUiCookie.OAUTH2_COOKIE in project trino by trinodb.
the class TestResourceSecurity method testOAuth2Groups.
@Test(dataProvider = "groups")
public void testOAuth2Groups(Optional<Set<String>> groups) throws Exception {
try (TokenServer tokenServer = new TokenServer(Optional.empty());
TestingTrinoServer server = TestingTrinoServer.builder().setProperties(ImmutableMap.<String, String>builder().putAll(SECURE_PROPERTIES).put("web-ui.enabled", "true").put("http-server.authentication.type", "oauth2").putAll(getOAuth2Properties(tokenServer)).put("http-server.authentication.oauth2.groups-field", GROUPS_CLAIM).buildOrThrow()).setAdditionalModule(oauth2Module(tokenServer)).build()) {
server.getInstance(Key.get(AccessControlManager.class)).addSystemAccessControl(TestSystemAccessControl.NO_IMPERSONATION);
HttpServerInfo httpServerInfo = server.getInstance(Key.get(HttpServerInfo.class));
String accessToken = tokenServer.issueAccessToken(groups);
OkHttpClient clientWithOAuthToken = client.newBuilder().authenticator((route, response) -> response.request().newBuilder().header(AUTHORIZATION, "Bearer " + accessToken).build()).build();
assertAuthenticationAutomatic(httpServerInfo.getHttpsUri(), clientWithOAuthToken);
try (Response response = clientWithOAuthToken.newCall(new Request.Builder().url(getLocation(httpServerInfo.getHttpsUri(), "/protocol/identity")).build()).execute()) {
assertEquals(response.code(), SC_OK);
assertEquals(response.header("user"), TEST_USER);
assertEquals(response.header("principal"), TEST_USER);
assertEquals(response.header("groups"), groups.map(TestResource::toHeader).orElse(""));
}
OkHttpClient clientWithOAuthCookie = client.newBuilder().cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
return ImmutableList.of(new Cookie.Builder().domain(httpServerInfo.getHttpsUri().getHost()).path(UI_LOCATION).name(OAUTH2_COOKIE).value(accessToken).httpOnly().secure().build());
}
}).build();
try (Response response = clientWithOAuthCookie.newCall(new Request.Builder().url(getLocation(httpServerInfo.getHttpsUri(), "/ui/api/identity")).build()).execute()) {
assertEquals(response.code(), SC_OK);
assertEquals(response.header("user"), TEST_USER);
assertEquals(response.header("principal"), TEST_USER);
assertEquals(response.header("groups"), groups.map(TestResource::toHeader).orElse(""));
}
}
}
use of io.trino.server.ui.OAuthWebUiCookie.OAUTH2_COOKIE in project trino by trinodb.
the class BaseOAuth2WebUiAuthenticationFilterTest method testSuccessfulFlow.
@Test
public void testSuccessfulFlow() throws Exception {
// create a new HttpClient which follows redirects and give access to cookies
CookieManager cookieManager = new CookieManager();
CookieStore cookieStore = cookieManager.getCookieStore();
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
setupInsecureSsl(httpClientBuilder);
OkHttpClient httpClient = httpClientBuilder.followRedirects(true).cookieJar(new JavaNetCookieJar(cookieManager)).build();
assertThat(cookieStore.get(uiUri)).isEmpty();
// access UI and follow redirects in order to get OAuth2 cookie
Response response = httpClient.newCall(new Request.Builder().url(uiUri.toURL()).get().build()).execute();
assertEquals(response.code(), SC_OK);
assertEquals(response.request().url().toString(), uiUri.toString());
Optional<HttpCookie> oauth2Cookie = cookieStore.get(uiUri).stream().filter(cookie -> cookie.getName().equals(OAUTH2_COOKIE)).findFirst();
assertThat(oauth2Cookie).isNotEmpty();
assertTrinoCookie(oauth2Cookie.get());
assertUICallWithCookie(oauth2Cookie.get().getValue());
}
Aggregations