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_USERattributestatic final AttributeKey<Boolean>Represents theATTR_IS_READ_ONLYattributeFields 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 TypeMethodDescriptionbooleancheckAuthorization(ChannelHandlerContext ctx, WebSocketFrame frame) Checks if the givenWebSocketFramehas the correct token and closes the connection aftermaxAttemptsfailed 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"}booleancheckAuthorizationHeaders(ChannelHandlerContext ctx, HttpHeaders headers) Checks if the givenHttpHeaderscontains a valid login for a panel user, if enabledbooleaninthashCode()voidInvalidates the authentication of the specifiedChannelHandlerContext, if supported by the authentication handler
-
Field Details
-
ATTR_IS_READ_ONLY
Represents theATTR_IS_READ_ONLYattribute -
ATTR_AUTH_USER
Represents theATTR_AUTH_USERattribute
-
-
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 givenWebSocketFramehas the correct token and closes the connection aftermaxAttemptsfailed 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:
checkAuthorizationin interfaceWsAuthenticationHandler- Parameters:
ctx- TheChannelHandlerContextof the sessionframe- TheWebSocketFrameto check- Returns:
- , this method will also reply with the appropriate frames to continue the authentication sequence, or an
Unauthorizedframe if authentication has been fully attempted and failed
-
invalidateAuthorization
Description copied from interface:WsAuthenticationHandlerInvalidates the authentication of the specifiedChannelHandlerContext, if supported by the authentication handler- Specified by:
invalidateAuthorizationin interfaceWsAuthenticationHandler- Parameters:
ctx- TheChannelHandlerContextof the sessionframe- TheWebSocketFrame
-
checkAuthorizationHeaders
Checks if the givenHttpHeaderscontains a valid login for a panel user, if enabled- Specified by:
checkAuthorizationHeadersin interfaceWsAuthenticationHandler- Parameters:
ctx- TheChannelHandlerContextof the sessionheaders- TheHttpHeadersto check- Returns:
trueif authorized
-
hashCode
public int hashCode() -
equals
-