Enable header-based auth for WebSocket connections

This commit is contained in:
Sergey Skrobotov
2023-09-25 11:28:23 -07:00
parent a263611746
commit d0fdae3df7
8 changed files with 147 additions and 85 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 Signal Messenger, LLC
* Copyright 2013 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.websocket;
@@ -57,7 +57,7 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends WebSo
if (authenticator.isPresent()) {
AuthenticationResult<T> authenticationResult = authenticator.get().authenticate(request);
if (authenticationResult.getUser().isEmpty() && authenticationResult.isRequired()) {
if (authenticationResult.getUser().isEmpty() && authenticationResult.credentialsPresented()) {
response.sendForbidden("Unauthorized");
return null;
} else {

View File

@@ -1,33 +1,32 @@
/*
* Copyright 2013-2020 Signal Messenger, LLC
* Copyright 2013 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.websocket.auth;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import java.security.Principal;
import java.util.Optional;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
public interface WebSocketAuthenticator<T extends Principal> {
AuthenticationResult<T> authenticate(UpgradeRequest request) throws AuthenticationException;
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public class AuthenticationResult<T> {
class AuthenticationResult<T> {
private final Optional<T> user;
private final boolean required;
private final boolean credentialsPresented;
public AuthenticationResult(Optional<T> user, boolean required) {
this.user = user;
this.required = required;
public AuthenticationResult(final Optional<T> user, final boolean credentialsPresented) {
this.user = user;
this.credentialsPresented = credentialsPresented;
}
public Optional<T> getUser() {
return user;
}
public boolean isRequired() {
return required;
public boolean credentialsPresented() {
return credentialsPresented;
}
}
}