Package org.apache.ofbiz.webapp.control
Class JWTManager
java.lang.Object
org.apache.ofbiz.webapp.control.JWTManager
This class manages the single sign-on authentication through JWT tokens between OFBiz applications.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
checkJWTLogin
(HttpServletRequest request, HttpServletResponse response) OFBiz controller preprocessor event.static String
Create and return a JWT token using the claims of the provided map and the configured expiration time.static String
Create and return a JWT token using the claims of the provided map and the provided expiration time.static String
Create and return a JWT token using the claims of the provided map and the provided expiration time.static String
getAuthenticationToken
(HttpServletRequest request, HttpServletResponse response) Get the authentication token based for user This takes OOTB username/password and if user is authenticated it will generate the JWT token using a secret key.static String
Gets the authentication token from the "Authorization" header if it is in the formBearer <token>
.static String
Get the JWT secret key from database or security.properties.static String
Get the JWT secret key from database or security.properties.validateToken
(String jwtToken, String key) Validates the provided token using the secret key.validateToken
(Delegator delegator, String jwtToken, String keySalt) Validates the provided token using a salt to recreate the key from the secret If the token is valid it will get the contained claims and return them.
-
Constructor Details
-
JWTManager
public JWTManager()
-
-
Method Details
-
checkJWTLogin
OFBiz controller preprocessor event. The method is designed to be used in a chain of controller preprocessor event: it always returns "success" even when the Authorization token is missing or the Authorization fails. This in order to move the processing to the next event in the chain. This works in a similar same way than externalLoginKey but between 2 servers on 2 different domains, not 2 webapps on the same server. The OFBiz internal Single Sign On (SSO) is ensured by a JWT token, then all is handled as normal by a session on the reached server. The servers may or may not share a database but the 2 loginUserIds must be the same. In case of a multitenancy usage, the tenant is verified.- Parameters:
request
- The HTTPRequest object for the current requestresponse
- The HTTPResponse object for the current request- Returns:
- String always "success"
-
getJWTKey
Get the JWT secret key from database or security.properties.- Parameters:
delegator
- the delegator- Returns:
- the JWT secret key
-
getJWTKey
Get the JWT secret key from database or security.properties.- Parameters:
delegator
- the delegator- Returns:
- the JWT secret key
-
getAuthenticationToken
public static String getAuthenticationToken(HttpServletRequest request, HttpServletResponse response) Get the authentication token based for user This takes OOTB username/password and if user is authenticated it will generate the JWT token using a secret key.- Parameters:
request
- the http request in which the authentication token is searched and stored- Returns:
- the authentication token
-
getHeaderAuthBearerToken
Gets the authentication token from the "Authorization" header if it is in the formBearer <token>
. Public for API access from third party code.- Parameters:
request
- the request to get the token from- Returns:
- the bare JWT token
-
validateToken
Validates the provided token using the secret key. If the token is valid it will get the conteined claims and return them. If token validation failed it will return an error. Public for API access from third party code.- Parameters:
jwtToken
- the JWT tokenkey
- the server side key to verify the signature- Returns:
- Map of the claims contained in the token or an error
-
validateToken
public static Map<String,Object> validateToken(Delegator delegator, String jwtToken, String keySalt) Validates the provided token using a salt to recreate the key from the secret If the token is valid it will get the contained claims and return them. If token validation failed it will return an error.- Parameters:
delegator
-jwtToken
-keySalt
-- Returns:
- Map of the claims contained in the token or an error
-
createJwt
Create and return a JWT token using the claims of the provided map and the configured expiration time.- Parameters:
delegator
- the delegatorclaims
- the map containing the JWT claims- Returns:
- a JWT token
-
createJwt
Create and return a JWT token using the claims of the provided map and the provided expiration time.- Parameters:
delegator
-claims
- the map containing the JWT claimsexpireTime
- the expiration time in seconds- Returns:
- a JWT token
-
createJwt
public static String createJwt(Delegator delegator, Map<String, String> claims, String keySalt, int expireTime) Create and return a JWT token using the claims of the provided map and the provided expiration time.- Parameters:
delegator
-claims
- the map containing the JWT claimskeySalt
- salt to use as prefix on the encrypt keyexpireTime
- the expiration time in seconds- Returns:
- a JWT token
-