Package io.mangoo.routing.bindings
Class Authentication
- java.lang.Object
-
- io.mangoo.routing.bindings.Authentication
-
public class Authentication extends Object
Convenient class for handling authentication- Author:
- svenkubiak
-
-
Constructor Summary
Constructors Constructor Description Authentication()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Authenticationcreate()LocalDateTimegetExpires()Returns the LocalDateTime when the authentication expiresStringgetSubject()Retrieves the current subjectvoidinvalidate()Invalidates the authentication by sending expiring the client cookiebooleanisInvalid()booleanisLogout()booleanisRememberMe()booleanisTwoFactor()booleanisValid()Checks if the authentication class contains an authenticationAuthenticationlogin(String subject)Performs a login by setting the authentication to the given identifier Please note, that calling validLogin is mandatory before this call!voidlogout()Performs a logout of the currently authenticated userAuthenticationrememberMe()Sets the remember me functionality to true, default is falseAuthenticationrememberMe(boolean remember)Sets the remember me functionality, default is falseAuthenticationtwoFactorAuthentication(boolean twoFactor)Sets the requirement of the two factor authentication, default is falsebooleanuserHasLock(String username)Checks if a username is locked because of to many failed login attemptsbooleanvalidLogin(String identifier, String password, String hash)Creates a hashed value of a given clear text password and checks if the value matches a given, already hashed passwordbooleanvalidSecondFactor(String secret, String number)Checks if a given number for 2FA is valid for the given secretAuthenticationwithExpires(LocalDateTime expires)AuthenticationwithSubject(String subject)
-
-
-
Method Detail
-
create
public static Authentication create()
-
withExpires
public Authentication withExpires(LocalDateTime expires)
-
withSubject
public Authentication withSubject(String subject)
-
getSubject
public String getSubject()
Retrieves the current subject- Returns:
- The subject of the current authentication or null if not set
-
invalidate
public void invalidate()
Invalidates the authentication by sending expiring the client cookie
-
getExpires
public LocalDateTime getExpires()
Returns the LocalDateTime when the authentication expires- Returns:
- A LocalDateTime object or null if not set
-
isLogout
public boolean isLogout()
- Returns:
- True if the user wants to logout, false otherwise
-
isRememberMe
public boolean isRememberMe()
- Returns:
- True if the user wants to stay logged in, false otherwise
-
isTwoFactor
public boolean isTwoFactor()
- Returns:
- True if two factor authentication is enabled for this user
-
validLogin
public boolean validLogin(String identifier, String password, String hash)
Creates a hashed value of a given clear text password and checks if the value matches a given, already hashed password- Parameters:
identifier- The identifier to authenticatepassword- The clear text passwordhash- The previously hashed password to check- Returns:
- True if the new hashed password matches the hash, false otherwise
-
login
public Authentication login(String subject)
Performs a login by setting the authentication to the given identifier Please note, that calling validLogin is mandatory before this call!- Parameters:
subject- The subject to login- Returns:
- Authentication object
-
rememberMe
public Authentication rememberMe(boolean remember)
Sets the remember me functionality, default is false- Parameters:
remember- The state of remember to set- Returns:
- Authentication object
-
rememberMe
public Authentication rememberMe()
Sets the remember me functionality to true, default is false- Returns:
- Authentication object
-
twoFactorAuthentication
public Authentication twoFactorAuthentication(boolean twoFactor)
Sets the requirement of the two factor authentication, default is false- Parameters:
twoFactor- True for enabling two factor authentication, false otherwise- Returns:
- Authentication object
-
userHasLock
public boolean userHasLock(String username)
Checks if a username is locked because of to many failed login attempts- Parameters:
username- The username to check- Returns:
- true if the user has a lock, false otherwise
-
validSecondFactor
public boolean validSecondFactor(String secret, String number)
Checks if a given number for 2FA is valid for the given secret- Parameters:
secret- The plaintext secret to use for checkingnumber- The number entered by the user- Returns:
- True if number is valid, false otherwise
-
logout
public void logout()
Performs a logout of the currently authenticated user
-
isValid
public boolean isValid()
Checks if the authentication class contains an authentication- Returns:
- True if authentication contains an authentication, false otherwise
-
isInvalid
public boolean isInvalid()
-
-