-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add authentication to the Preview Web UI
- Loading branch information
Showing
28 changed files
with
4,402 additions
and
2,292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
core/trino-main/src/main/java/io/trino/server/ui/AuthInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.server.ui; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public record AuthInfo(String authType, boolean passwordAllowed, boolean authenticated, Optional<String> username) | ||
{ | ||
public AuthInfo { | ||
requireNonNull(authType, "authType is null"); | ||
requireNonNull(username, "username is null"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/trino-main/src/main/java/io/trino/server/ui/FixedUserPreviewResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.server.ui; | ||
|
||
import com.google.inject.Inject; | ||
import io.trino.server.security.ResourceSecurity; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.trino.server.security.ResourceSecurity.AccessType.WEB_UI; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_AUTH_INFO; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path(UI_PREVIEW_AUTH_INFO) | ||
@ResourceSecurity(WEB_UI) | ||
public class FixedUserPreviewResource | ||
{ | ||
private final String username; | ||
|
||
@Inject | ||
public FixedUserPreviewResource(FixedUserWebUiConfig config) | ||
{ | ||
this.username = requireNonNull(config, "config is null").getUsername(); | ||
} | ||
|
||
@GET | ||
@Produces(APPLICATION_JSON) | ||
public AuthInfo getAuthInfo() | ||
{ | ||
return new AuthInfo("fixed", false, true, Optional.of(username)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
core/trino-main/src/main/java/io/trino/server/ui/LoginPreviewResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.server.ui; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.inject.Inject; | ||
import io.trino.server.security.ResourceSecurity; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.ForbiddenException; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.NewCookie; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Strings.emptyToNull; | ||
import static io.trino.server.security.ResourceSecurity.AccessType.WEB_UI; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_AUTH_INFO; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_LOGIN_FORM; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_LOGOUT; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.getDeleteCookies; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path("") | ||
@ResourceSecurity(WEB_UI) | ||
@Consumes(APPLICATION_JSON) | ||
@Produces(APPLICATION_JSON) | ||
public class LoginPreviewResource | ||
{ | ||
private final FormWebUiAuthenticationFilter formWebUiAuthenticationManager; | ||
|
||
@Inject | ||
public LoginPreviewResource(FormWebUiAuthenticationFilter formWebUiAuthenticationManager) | ||
{ | ||
this.formWebUiAuthenticationManager = requireNonNull(formWebUiAuthenticationManager, "formWebUiAuthenticationManager is null"); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_AUTH_INFO) | ||
public AuthInfo getAuthInfo(ContainerRequestContext request, @Context SecurityContext securityContext) | ||
{ | ||
boolean isPasswordAllowed = formWebUiAuthenticationManager.isPasswordAllowed(securityContext.isSecure()); | ||
Optional<String> username = formWebUiAuthenticationManager.getAuthenticatedUsername(request); | ||
return new AuthInfo("form", isPasswordAllowed, username.isPresent(), username); | ||
} | ||
|
||
@POST | ||
@Path(UI_PREVIEW_LOGIN_FORM) | ||
public Response login(LoginForm loginForm, @Context SecurityContext securityContext) | ||
{ | ||
String username = emptyToNull(loginForm.username()); | ||
String password = emptyToNull(loginForm.password()); | ||
|
||
if (!formWebUiAuthenticationManager.isAuthenticationEnabled(securityContext.isSecure())) { | ||
throw new ForbiddenException(); | ||
} | ||
|
||
Optional<NewCookie[]> authenticationCookie = formWebUiAuthenticationManager.checkLoginCredentials(username, password, securityContext.isSecure()); | ||
if (authenticationCookie.isEmpty()) { | ||
throw new ForbiddenException(); | ||
} | ||
|
||
return Response.noContent() | ||
.cookie(authenticationCookie.get()) | ||
.build(); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_LOGOUT) | ||
public Response logout(@Context HttpHeaders httpHeaders, @Context SecurityContext securityContext) | ||
{ | ||
return Response.noContent() | ||
.cookie(getDeleteCookies(httpHeaders.getCookies(), securityContext.isSecure())) | ||
.build(); | ||
} | ||
|
||
public record LoginForm(@JsonProperty String username, @JsonProperty String password) | ||
{ | ||
@Override | ||
public String username() | ||
{ | ||
return username; | ||
} | ||
|
||
@Override | ||
public String password() | ||
{ | ||
return password; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
core/trino-main/src/main/java/io/trino/server/ui/OAuth2WebUiPreviewResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.server.ui; | ||
|
||
import com.google.inject.Inject; | ||
import io.trino.server.ExternalUriInfo; | ||
import io.trino.server.security.ResourceSecurity; | ||
import io.trino.server.security.oauth2.OAuth2Client; | ||
import io.trino.spi.security.Identity; | ||
import jakarta.ws.rs.BeanParam; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import static io.trino.server.ServletSecurityUtils.authenticatedIdentity; | ||
import static io.trino.server.security.ResourceSecurity.AccessType.WEB_UI; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_LOGOUT; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_AUTH_INFO; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_LOGOUT; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.getDeleteCookies; | ||
import static io.trino.server.ui.OAuthWebUiCookie.delete; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path("") | ||
@ResourceSecurity(WEB_UI) | ||
public class OAuth2WebUiPreviewResource | ||
{ | ||
private final OAuth2Client oAuth2Client; | ||
|
||
@Inject | ||
public OAuth2WebUiPreviewResource(OAuth2Client oAuth2Client) | ||
{ | ||
this.oAuth2Client = requireNonNull(oAuth2Client, "oAuth2Client is null"); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_AUTH_INFO) | ||
@Produces(APPLICATION_JSON) | ||
public AuthInfo getAuthInfo(ContainerRequestContext request) | ||
{ | ||
Optional<String> username = authenticatedIdentity(request).map(Identity::getUser); | ||
return new AuthInfo("oauth2", false, username.isPresent(), username); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_LOGOUT) | ||
@Produces(APPLICATION_JSON) | ||
public Response logout(@Context HttpHeaders httpHeaders, @BeanParam ExternalUriInfo uriInfo, @Context SecurityContext securityContext) | ||
{ | ||
Optional<String> idToken = OAuthIdTokenCookie.read(httpHeaders.getCookies()); | ||
URI callbackUri = uriInfo.absolutePath(UI_LOGOUT + "/logout.html"); | ||
return Response.seeOther(oAuth2Client.getLogoutEndpoint(idToken, callbackUri) | ||
.orElse(callbackUri)) | ||
.cookie(OAuthIdTokenCookie.delete(httpHeaders.getCookies())) | ||
.cookie(getDeleteCookies(httpHeaders.getCookies(), securityContext.isSecure())) | ||
.cookie(delete(httpHeaders.getCookies())) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.