Package com.gmt2001.httpwsserver.auth
Class WsSharedRWTokenAuthenticationHandler
java.lang.Object
com.gmt2001.httpwsserver.auth.WsSharedRWTokenAuthenticationHandler
- All Implemented Interfaces:
WsAuthenticationHandler
Provides a
WsAuthenticationHandler
that implements A Write/ReadOnly token-based authentication where all clients share the same tokens- Author:
- gmt2001
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AttributeKey<PanelUser>
Represents theATTR_AUTH_USER
attributestatic final AttributeKey<Boolean>
Represents theATTR_IS_READ_ONLY
attributeFields inherited from interface com.gmt2001.httpwsserver.auth.WsAuthenticationHandler
ATTR_AUTHENTICATED, ATTR_SENT_AUTH_REPLY
-
Constructor Summary
ConstructorsConstructorDescriptionWsSharedRWTokenAuthenticationHandler
(String readOnlyToken, String readWriteToken, int maxAttempts) ConstructorWsSharedRWTokenAuthenticationHandler
(String readOnlyToken, String readWriteToken, int maxAttempts, boolean allowPaneluser) ConstructorWsSharedRWTokenAuthenticationHandler
(String readOnlyToken, String readWriteToken, int maxAttempts, Runnable authenticatedCallback) ConstructorWsSharedRWTokenAuthenticationHandler
(String readOnlyToken, String readWriteToken, int maxAttempts, Runnable authenticatedCallback, boolean allowPaneluser) Constructor -
Method Summary
Modifier and TypeMethodDescriptionboolean
checkAuthorization
(ChannelHandlerContext ctx, WebSocketFrame frame) Checks if the givenWebSocketFrame
has the correct token and closes the connection aftermaxAttempts
failed attempts Saves the authorization results in the channel attributes and recalls them to keep the session authorized in subsequent frames If this is an authentication frame, or another frame when authentication already failed, sends a JSON-encoded frame defined as{"authresult": string:"true" or "false", "authtype": string:"none" or "read" or "read/write"}
boolean
checkAuthorizationHeaders
(ChannelHandlerContext ctx, HttpHeaders headers) Checks if the givenHttpHeaders
contains a valid login for a panel user, if enabledboolean
int
hashCode()
void
Invalidates the authentication of the specifiedChannelHandlerContext
, if supported by the authentication handler
-
Field Details
-
ATTR_IS_READ_ONLY
Represents theATTR_IS_READ_ONLY
attribute -
ATTR_AUTH_USER
Represents theATTR_AUTH_USER
attribute
-
-
Constructor Details
-
WsSharedRWTokenAuthenticationHandler
public WsSharedRWTokenAuthenticationHandler(String readOnlyToken, String readWriteToken, int maxAttempts) Constructor- Parameters:
readOnlyToken
- The authorization token that grants read-only accessreadWriteToken
- The authorization token that grants read-write accessmaxAttempts
- The maximum allowed auth failure responses before the connection is shut down
-
WsSharedRWTokenAuthenticationHandler
public WsSharedRWTokenAuthenticationHandler(String readOnlyToken, String readWriteToken, int maxAttempts, boolean allowPaneluser) Constructor- Parameters:
readOnlyToken
- The authorization token that grants read-only accessreadWriteToken
- The authorization token that grants read-write accessmaxAttempts
- The maximum allowed auth failure responses before the connection is shut downallowPaneluser
- Whether to allow authenticating via HTTP Headers to aPanelUser
-
WsSharedRWTokenAuthenticationHandler
public WsSharedRWTokenAuthenticationHandler(String readOnlyToken, String readWriteToken, int maxAttempts, Runnable authenticatedCallback) Constructor- Parameters:
readOnlyToken
- The authorization token that grants read-only accessreadWriteToken
- The authorization token that grants read-write accessmaxAttempts
- The maximum allowed auth failure responses before the connection is shut downauthenticatedCallback
- A callback to run when a connection authenticates successfully
-
WsSharedRWTokenAuthenticationHandler
public WsSharedRWTokenAuthenticationHandler(String readOnlyToken, String readWriteToken, int maxAttempts, Runnable authenticatedCallback, boolean allowPaneluser) Constructor- Parameters:
readOnlyToken
- The authorization token that grants read-only accessreadWriteToken
- The authorization token that grants read-write accessmaxAttempts
- The maximum allowed auth failure responses before the connection is shut downauthenticatedCallback
- A callback to run when a connection authenticates successfullyallowPaneluser
- Whether to allow authenticating via HTTP Headers to aPanelUser
-
-
Method Details
-
checkAuthorization
Checks if the givenWebSocketFrame
has the correct token and closes the connection aftermaxAttempts
failed attempts Saves the authorization results in the channel attributes and recalls them to keep the session authorized in subsequent frames If this is an authentication frame, or another frame when authentication already failed, sends a JSON-encoded frame defined as{"authresult": string:"true" or "false", "authtype": string:"none" or "read" or "read/write"}
- Specified by:
checkAuthorization
in interfaceWsAuthenticationHandler
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionframe
- TheWebSocketFrame
to check- Returns:
- , this method will also reply with the appropriate frames to continue the authentication sequence, or an
Unauthorized
frame if authentication has been fully attempted and failed
-
invalidateAuthorization
Description copied from interface:WsAuthenticationHandler
Invalidates the authentication of the specifiedChannelHandlerContext
, if supported by the authentication handler- Specified by:
invalidateAuthorization
in interfaceWsAuthenticationHandler
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionframe
- TheWebSocketFrame
-
checkAuthorizationHeaders
Checks if the givenHttpHeaders
contains a valid login for a panel user, if enabled- Specified by:
checkAuthorizationHeaders
in interfaceWsAuthenticationHandler
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionheaders
- TheHttpHeaders
to check- Returns:
true
if authorized
-
hashCode
public int hashCode() -
equals
-