Class StatelessUtilImpl
- java.lang.Object
-
- com.broadleafcommerce.auth.user.session.StatelessUtilImpl
-
- All Implemented Interfaces:
StatelessUtil
public class StatelessUtilImpl extends Object implements StatelessUtil
- Author:
- Jeff Fischer
- See Also:
StatelessUtil
-
-
Constructor Summary
Constructors Constructor Description StatelessUtilImpl(AuthorizedClientService<AuthorizedClient> clientService, AuthorizationServerService<AuthorizationServer> serverService, String encodedPrivateKey, String encodedPublicKey, String issuer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description org.springframework.http.ResponseCookiecreateSavedRequestCookie(String requestUrl, String redirectUrl)Create a cookie containing a JWT token identifying an originating request url and a redirect url.org.springframework.http.ResponseCookiecreateSavedRequestRemovalCookie()Create a cookie that can be passed to the response to clear any existing saved request cookie in the browser.javax.servlet.http.CookiecreateSessionCookie(OAuth2SessionToken sessionToken)Creates a cookie for theOAuth2SessionToken.protected Supplier<? extends com.broadleafcommerce.data.tracking.core.exception.EntityMissingException>entityMissing(String msg)protected AuthorizedClientfindClient(String clientId)protected AuthorizationServerfindServer(AuthorizedClient client)com.nimbusds.jwt.SignedJWTgenerateSignedJwt(String subject, Map<String,Object> claims, Long expiresInSeconds)Generate a signed JWT with issuer and audience values as well as optional additional claims.StringgetClientId(String savedRequestToken)Map<String,Object>getCookieClaims(String cookieValue)Get a Map of the claims from a SignedJWT cookie.protected StringgetDecodedClaim(String token, String claim, String errorMessage)protected StringgetDecodedClaim(javax.servlet.http.Cookie savedRequestCookie, String claim, String errorMessage)StringgetIssuer()protected StatelessUtilPropertiesgetProperties()StringgetRedirectUrl(javax.servlet.http.Cookie savedRequestCookie)Verify the JWT token contained in the cookie and then return the redirect url contained therein.org.springframework.http.ResponseCookiegetRemovalCookie(javax.servlet.http.Cookie cookieToRemove)Returns a removal cookie for any arbitrary cookie.javax.servlet.http.CookiegetRemoveSavedRequestCookie()Create a cookie that can be passed to the response to clear any existing saved request cookie in the browser.javax.servlet.http.CookiegetRemoveSessionCookie(String clientId)Create a cookie that can be passed to the response to clear any existing session cookie in the browser.StringgetRequestUrl(javax.servlet.http.Cookie savedRequestCookie)Verify the JWT token contained in the cookie and then return the original request url contained therein.protected StringgetSameSiteAttributeForSavedRequestCookie()Gets the SameSite attribute value to use for the saved request cookies (createSavedRequestCookie(String, String)andcreateSavedRequestRemovalCookie()).javax.servlet.http.CookiegetSavedRequestCookie(String requestUrl, String redirectUrl)Create a cookie containing a JWT token identifying an originating request url and a redirect url.StringgetSavedRequestCookieName()Get the standard name for a saved request cookieStringgetSavedRequestJwt(String requestUrl, String redirectUrl)org.springframework.http.ResponseCookiegetSessionCookie(OAuth2SessionToken sessionToken)Gets aResponseCookiefor theOAuth2SessionToken.StringgetSessionCookieName(AuthorizedClient client, AuthorizationServer server)StringgetSessionCookieName(String clientId)Get the standard name for a session cookieorg.springframework.http.ResponseCookiegetSessionRemovalCookie(String clientId)Gets aResponseCookiefor the removal of the session cookie.protected StringgetSessionSameSiteAttribute(AuthorizationServer authorizationServer)Gets the SameSite attribute value for the session cookie.OAuth2SessionTokengetSessionToken(String clientId, String userId, String subject, Map<String,Object> additionalClaims)Creates aOAuth2SessionTokenfor the provided client ID and subject with the set of additional claims.protected com.nimbusds.jwt.JWTClaimsSetgetSessionTokenClaims(String clientId, String userId, String subject, Map<String,Object> additionalClaims)OAuth2SessionTokenrefreshSessionToken(OAuth2SessionToken sessionToken)Returns aOAuth2SessionTokenwith a refreshed expiration time.voidsetProperties(StatelessUtilProperties properties)com.nimbusds.jwt.SignedJWTverify(String token)Verify the signature of a signed JWT inside a cookie
-
-
-
Constructor Detail
-
StatelessUtilImpl
public StatelessUtilImpl(AuthorizedClientService<AuthorizedClient> clientService, AuthorizationServerService<AuthorizationServer> serverService, @NonNull String encodedPrivateKey, @NonNull String encodedPublicKey, String issuer)
-
-
Method Detail
-
getSessionToken
public OAuth2SessionToken getSessionToken(String clientId, String userId, String subject, Map<String,Object> additionalClaims)
Description copied from interface:StatelessUtilCreates aOAuth2SessionTokenfor the provided client ID and subject with the set of additional claims.- Specified by:
getSessionTokenin interfaceStatelessUtil- Parameters:
clientId- the client IDsubject- the user subjectadditionalClaims- the additional session claims- Returns:
-
getSessionTokenClaims
protected com.nimbusds.jwt.JWTClaimsSet getSessionTokenClaims(String clientId, String userId, String subject, @NonNull Map<String,Object> additionalClaims)
-
refreshSessionToken
public OAuth2SessionToken refreshSessionToken(OAuth2SessionToken sessionToken)
Description copied from interface:StatelessUtilReturns aOAuth2SessionTokenwith a refreshed expiration time.- Specified by:
refreshSessionTokenin interfaceStatelessUtil- Parameters:
sessionToken- the session token- Returns:
- the refreshed session token
-
generateSignedJwt
public com.nimbusds.jwt.SignedJWT generateSignedJwt(@Nullable String subject, @Nullable Map<String,Object> claims, @Nullable Long expiresInSeconds)Description copied from interface:StatelessUtilGenerate a signed JWT with issuer and audience values as well as optional additional claims.- Specified by:
generateSignedJwtin interfaceStatelessUtil- Parameters:
subject- The subject of the JWTclaims- Additional claims to add to the JWT- Returns:
- A signed JWT
-
getRedirectUrl
public String getRedirectUrl(javax.servlet.http.Cookie savedRequestCookie)
Description copied from interface:StatelessUtilVerify the JWT token contained in the cookie and then return the redirect url contained therein.- Specified by:
getRedirectUrlin interfaceStatelessUtil- Parameters:
savedRequestCookie- seeStatelessUtil.createSavedRequestCookie(String, String)- Returns:
- a decoded URL set with
StatelessUtil.createSavedRequestCookie(String, String)
-
getRequestUrl
public String getRequestUrl(javax.servlet.http.Cookie savedRequestCookie)
Description copied from interface:StatelessUtilVerify the JWT token contained in the cookie and then return the original request url contained therein.- Specified by:
getRequestUrlin interfaceStatelessUtil- Parameters:
savedRequestCookie- seeStatelessUtil.createSavedRequestCookie(String, String)- Returns:
- a decoded URL set with
StatelessUtil.createSavedRequestCookie(String, String)
-
getClientId
public String getClientId(String savedRequestToken)
- Specified by:
getClientIdin interfaceStatelessUtil
-
getDecodedClaim
protected String getDecodedClaim(javax.servlet.http.Cookie savedRequestCookie, String claim, String errorMessage)
-
getCookieClaims
public Map<String,Object> getCookieClaims(String cookieValue)
Description copied from interface:StatelessUtilGet a Map of the claims from a SignedJWT cookie. The values of the map are Base64 encoded.- Specified by:
getCookieClaimsin interfaceStatelessUtil- Returns:
-
createSessionCookie
public javax.servlet.http.Cookie createSessionCookie(OAuth2SessionToken sessionToken)
Description copied from interface:StatelessUtilCreates a cookie for theOAuth2SessionToken.- Specified by:
createSessionCookiein interfaceStatelessUtil- Parameters:
sessionToken- the session token- Returns:
- the session cookie
-
getRemoveSessionCookie
public javax.servlet.http.Cookie getRemoveSessionCookie(String clientId)
Description copied from interface:StatelessUtilCreate a cookie that can be passed to the response to clear any existing session cookie in the browser.- Specified by:
getRemoveSessionCookiein interfaceStatelessUtil- Returns:
- the newly created cookie
-
getSessionCookie
public org.springframework.http.ResponseCookie getSessionCookie(OAuth2SessionToken sessionToken)
Description copied from interface:StatelessUtilGets aResponseCookiefor theOAuth2SessionToken.- Specified by:
getSessionCookiein interfaceStatelessUtil- Parameters:
sessionToken- the session token- Returns:
- the session cookie
-
getSessionRemovalCookie
public org.springframework.http.ResponseCookie getSessionRemovalCookie(String clientId)
Description copied from interface:StatelessUtilGets aResponseCookiefor the removal of the session cookie.- Specified by:
getSessionRemovalCookiein interfaceStatelessUtil- Parameters:
clientId- the client ID- Returns:
- the session removal cookie
-
getSessionSameSiteAttribute
protected String getSessionSameSiteAttribute(AuthorizationServer authorizationServer)
Gets the SameSite attribute value for the session cookie. This method should return one of the following values: "None", "Lax", or "Strict".Learn more about the SameSite attribute at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
- Parameters:
authorizationServer- the authorization server- Returns:
- the SameSite attribute value
-
getSavedRequestCookie
public javax.servlet.http.Cookie getSavedRequestCookie(String requestUrl, String redirectUrl)
Description copied from interface:StatelessUtilCreate a cookie containing a JWT token identifying an originating request url and a redirect url. This information is used to forward the user to an authentication url. Once authenticated, the original request is completed.- Specified by:
getSavedRequestCookiein interfaceStatelessUtil- Parameters:
requestUrl- the original request (e.g. /oauth/authorize)redirectUrl- the uri to redirect to for authentication (e.g. /login)- Returns:
- a newly created session cookie
-
createSavedRequestCookie
public org.springframework.http.ResponseCookie createSavedRequestCookie(String requestUrl, String redirectUrl)
Description copied from interface:StatelessUtilCreate a cookie containing a JWT token identifying an originating request url and a redirect url. This information is used to forward the user to an authentication url. Once authenticated, the original request is completed.- Specified by:
createSavedRequestCookiein interfaceStatelessUtil- Parameters:
requestUrl- the original request (e.g. /oauth/authorize)redirectUrl- the uri to redirect to for authentication (e.g. /login)- Returns:
- a newly created session cookie
-
getSameSiteAttributeForSavedRequestCookie
protected String getSameSiteAttributeForSavedRequestCookie()
Gets the SameSite attribute value to use for the saved request cookies (createSavedRequestCookie(String, String)andcreateSavedRequestRemovalCookie()). This method should return one of the following values: "None", "Lax", or "Strict".Learn more about the SameSite attribute at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
- Returns:
- the SameSite attribute value to use for the saved request cookies
-
getSavedRequestJwt
public String getSavedRequestJwt(String requestUrl, String redirectUrl)
- Specified by:
getSavedRequestJwtin interfaceStatelessUtil
-
getRemoveSavedRequestCookie
public javax.servlet.http.Cookie getRemoveSavedRequestCookie()
Description copied from interface:StatelessUtilCreate a cookie that can be passed to the response to clear any existing saved request cookie in the browser.- Specified by:
getRemoveSavedRequestCookiein interfaceStatelessUtil- Returns:
- the newly created cookie
-
createSavedRequestRemovalCookie
public org.springframework.http.ResponseCookie createSavedRequestRemovalCookie()
Description copied from interface:StatelessUtilCreate a cookie that can be passed to the response to clear any existing saved request cookie in the browser.- Specified by:
createSavedRequestRemovalCookiein interfaceStatelessUtil- Returns:
- the newly created cookie
- See Also:
StatelessUtil.createSavedRequestCookie(String, String)
-
verify
public com.nimbusds.jwt.SignedJWT verify(String token)
Description copied from interface:StatelessUtilVerify the signature of a signed JWT inside a cookie- Specified by:
verifyin interfaceStatelessUtil- Parameters:
token- the cookie to verify- Returns:
- a verified and signed JWT token, or null if the given cookie has no value
-
getSessionCookieName
public String getSessionCookieName(String clientId)
Description copied from interface:StatelessUtilGet the standard name for a session cookie- Specified by:
getSessionCookieNamein interfaceStatelessUtil- Returns:
- the cookie name used to store the session
- See Also:
#getSessionCookie(String, String)
-
getSessionCookieName
public String getSessionCookieName(AuthorizedClient client, AuthorizationServer server)
- Specified by:
getSessionCookieNamein interfaceStatelessUtil
-
getIssuer
public String getIssuer()
- Specified by:
getIssuerin interfaceStatelessUtil
-
getSavedRequestCookieName
public String getSavedRequestCookieName()
Description copied from interface:StatelessUtilGet the standard name for a saved request cookie- Specified by:
getSavedRequestCookieNamein interfaceStatelessUtil- Returns:
- the saved request cookie name
- See Also:
StatelessUtil.createSavedRequestCookie(String, String)
-
getRemovalCookie
public org.springframework.http.ResponseCookie getRemovalCookie(javax.servlet.http.Cookie cookieToRemove)
Description copied from interface:StatelessUtilReturns a removal cookie for any arbitrary cookie.- Specified by:
getRemovalCookiein interfaceStatelessUtil- Parameters:
cookieToRemove- The cookie to target for removal- Returns:
- A cookie that may be set on an
HttpServletResponseto remove the supplied cookie.
-
findClient
protected AuthorizedClient findClient(String clientId)
-
findServer
protected AuthorizationServer findServer(AuthorizedClient client)
-
entityMissing
protected Supplier<? extends com.broadleafcommerce.data.tracking.core.exception.EntityMissingException> entityMissing(String msg)
-
getProperties
protected StatelessUtilProperties getProperties()
-
setProperties
@Autowired public void setProperties(StatelessUtilProperties properties)
-
-