Class OAuthRealm
java.lang.Object
org.apache.shiro.realm.CachingRealm
org.apache.shiro.realm.AuthenticatingRealm
de.businesscode.bcdui.subjectsettings.oauth2.OAuthRealm
- All Implemented Interfaces:
org.apache.shiro.authc.LogoutAware
,org.apache.shiro.cache.CacheManagerAware
,org.apache.shiro.lang.util.Initializable
,org.apache.shiro.lang.util.Nameable
,org.apache.shiro.realm.Realm
public class OAuthRealm
extends org.apache.shiro.realm.AuthenticatingRealm
This Realm will accept logins from trusted oAuth authentication servers
Per default we also require the user to be found in bcd_sec_user to not lose control, as bcdAuthc resources only need a session and no permissions!
For permissions, which are added in BCD-UI JdbcRealm, entries in bcd_sec_user_settings are anyway necessary
This one is an authenticating realm only as such we obtain identity from resource server and let other realms obtain authorization data from elsewhere
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
static final record
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
assurePrincipleIsAllowed
(OAuthRealm.OAuthUserInfo providedPrincipal, OAuthRealm.PrincipalInfo localPi) Unless explicitly disabled, we do enforce existence of principal in bcd_sec_user here to avoid everybody getting a session who is known to the remote system but not ours Can also be used as an extension point for example test for an additional flag ncd bcd_sec_user etc by overwriting this class for an oAuth provider in shiro.iniprotected String
callTokenEndpoint
(OAuthToken oauthToken, String tokenProperty) call against oauth2.0 "/token" endpoint to obtain an access tokenprotected org.apache.shiro.authc.AuthenticationInfo
doGetAuthenticationInfo
(org.apache.shiro.authc.AuthenticationToken authToken) creates access token viacallTokenEndpoint(OAuthToken, String)
and passes toretrieveUserPrincipal(OAuthToken)
then constructsSimpleAuthenticationInfo
with returned principal if oauth AND bcd_sec_user exist: - local userId is being used - login_name is the same for local and oauth - fullName we use from oauth if configured (fullNamePropertyName is set), otherwise local - same for email.protected OAuthRealm.PrincipalInfo
Unless explicitly disabled, we do enforce existence of ui in bcd_sec_user here to avoid everybody getting a session who is known to the remote system but not ours Can also be used as an extension point for example test for an additional flag ncd bcd_sec_user etc by overwriting this class for an oAuth provider in shiro.iniboolean
protected OAuthRealm.OAuthUserInfo
getUserPrincipalFromIdToken
(String accessToken, String origNonce) Default.protected OAuthRealm.OAuthUserInfo
getUserPrincipalFromUserInfoEndpoint
(String accessToken) Useful if overwriting this class to retrieve more properties of the principal than its unique name Derive name of the principal from an extra call to the user info endpoint like https://graph.microsoft.com/v1.0/me/ Make sure the token we received earlier is allowed to access it, i.e. requested scope for example includes https://graph.microsoft.com/user.readprotected com.google.gson.JsonElement
readJsonProperty
(String jsonString, String propertyName, boolean isPropertyRequired) helper to access plain property of a json string, i.e. readJsonProperty('{ foo:"bar" }', "foo").getAsString() returns "bar"protected OAuthRealm.OAuthUserInfo
retrieveUserPrincipal
(OAuthToken oauthToken) implements retrieving user principal from resource server First gets the id-token from the token endpoint and then either retrieves it from the token or does an extra call to the info endpoint, if that is configured, which allows extracting more info in case we are overwrittenvoid
setAuthenticator
(OAuthAuthenticatingFilter authenticator) Set in shiro.ini We need to set the instance of filter class to determine in realm if that should support processing, in case we have many realms bound with multiple authenticators for given tokenvoid
setClientSecret
(String clientSecret) Set in shiro.ini secret we need to obtain access token from /token endpoint, used incallTokenEndpoint(OAuthToken, String)
void
setDisableSslValidation
(boolean disableSslValidation) void
setEmailPropertyName
(String emailPropertyName) Overwrite default in shiro.ini this is the property we extract from JSON response and use as emailvoid
setFullNamePropertyName
(String principalNameName) Overwrite default in shiro.ini this is the property we extract from JSON response and use as a full name "Jon Doe"void
setPrincipalPropertyName
(String principalPropertyName) Overwrite default in shiro.ini this is the property we extract from JSON response and use as a principalvoid
setSkipBcdSecUserTest
(boolean skipBcdSecUserTest) void
setTokenEndpoint
(String tokenEndpoint) Set in shiro.ini sets the endpoint to /token APIvoid
setUserInfoEndpoint
(String apiEndPoint) Set in shiro.ini the API endpoint to obtain information about user basically providing JSON data containinggetPrincipalPropertyName()
to extractboolean
supports
(org.apache.shiro.authc.AuthenticationToken token) acceptsOAuthToken
and only from given authenticator, if providedMethods inherited from class org.apache.shiro.realm.AuthenticatingRealm
afterCacheManagerSet, assertCredentialsMatch, clearCachedAuthenticationInfo, doClearCache, getAuthenticationCache, getAuthenticationCacheKey, getAuthenticationCacheKey, getAuthenticationCacheName, getAuthenticationInfo, getAuthenticationTokenClass, getCredentialsMatcher, init, isAuthenticationCachingEnabled, isAuthenticationCachingEnabled, onInit, setAuthenticationCache, setAuthenticationCacheName, setAuthenticationCachingEnabled, setAuthenticationTokenClass, setCredentialsMatcher, setName
Methods inherited from class org.apache.shiro.realm.CachingRealm
clearCache, getAvailablePrincipal, getCacheManager, getName, isCachingEnabled, onLogout, setCacheManager, setCachingEnabled
-
Field Details
-
ACCESS_TOKEN_NAME
- See Also:
-
ID_TOKEN_NAME
- See Also:
-
-
Constructor Details
-
OAuthRealm
public OAuthRealm()
-
-
Method Details
-
setDisableSslValidation
public void setDisableSslValidation(boolean disableSslValidation) -
setAuthenticator
Set in shiro.ini We need to set the instance of filter class to determine in realm if that should support processing, in case we have many realms bound with multiple authenticators for given token- Parameters:
authenticator
-
-
getAuthenticator
-
setTokenEndpoint
Set in shiro.ini sets the endpoint to /token API- Parameters:
tokenEndpoint
-
-
getTokenEndpoint
-
setUserInfoEndpoint
Set in shiro.ini the API endpoint to obtain information about user basically providing JSON data containinggetPrincipalPropertyName()
to extract- Parameters:
apiEndPoint
- The endpoint to get principal's JSON
-
getUserInfoEndpoint
-
setClientSecret
Set in shiro.ini secret we need to obtain access token from /token endpoint, used incallTokenEndpoint(OAuthToken, String)
- Parameters:
clientSecret
-
-
setPrincipalPropertyName
Overwrite default in shiro.ini this is the property we extract from JSON response and use as a principal- Parameters:
principalPropertyName
-
-
getPrincipalPropertyName
-
setFullNamePropertyName
Overwrite default in shiro.ini this is the property we extract from JSON response and use as a full name "Jon Doe"- Parameters:
principalNameName
-
-
getFullNamePropertyName
-
setEmailPropertyName
Overwrite default in shiro.ini this is the property we extract from JSON response and use as email- Parameters:
emailPropertyName
-
-
getEmailPropertyName
-
getSkipBcdSecUserTest
public boolean getSkipBcdSecUserTest() -
setSkipBcdSecUserTest
public void setSkipBcdSecUserTest(boolean skipBcdSecUserTest) -
supports
public boolean supports(org.apache.shiro.authc.AuthenticationToken token) acceptsOAuthToken
and only from given authenticator, if provided- Specified by:
supports
in interfaceorg.apache.shiro.realm.Realm
- Overrides:
supports
in classorg.apache.shiro.realm.AuthenticatingRealm
-
doGetAuthenticationInfo
protected org.apache.shiro.authc.AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken authToken) creates access token viacallTokenEndpoint(OAuthToken, String)
and passes toretrieveUserPrincipal(OAuthToken)
then constructsSimpleAuthenticationInfo
with returned principal if oauth AND bcd_sec_user exist: - local userId is being used - login_name is the same for local and oauth - fullName we use from oauth if configured (fullNamePropertyName is set), otherwise local - same for email. Whie standard bcd_sec_user does not local email, it may exists if getPrincipleInfo() is overwritten in a project- Specified by:
doGetAuthenticationInfo
in classorg.apache.shiro.realm.AuthenticatingRealm
-
getPrincipleInfo
Unless explicitly disabled, we do enforce existence of ui in bcd_sec_user here to avoid everybody getting a session who is known to the remote system but not ours Can also be used as an extension point for example test for an additional flag ncd bcd_sec_user etc by overwriting this class for an oAuth provider in shiro.ini- Parameters:
ui
-- Returns:
- null if no information in bcd_sec_user should be or can be found
- Throws:
Exception
-
assurePrincipleIsAllowed
protected boolean assurePrincipleIsAllowed(OAuthRealm.OAuthUserInfo providedPrincipal, OAuthRealm.PrincipalInfo localPi) throws Exception Unless explicitly disabled, we do enforce existence of principal in bcd_sec_user here to avoid everybody getting a session who is known to the remote system but not ours Can also be used as an extension point for example test for an additional flag ncd bcd_sec_user etc by overwriting this class for an oAuth provider in shiro.ini- Parameters:
localPi
-- Returns:
- Throws:
Exception
-
retrieveUserPrincipal
implements retrieving user principal from resource server First gets the id-token from the token endpoint and then either retrieves it from the token or does an extra call to the info endpoint, if that is configured, which allows extracting more info in case we are overwritten- Parameters:
oauthToken
- - which is already obtained from authority server and ready to be used as a bearer- Returns:
- user principal, OAuthUserInfo
- Throws:
IOException
Exception
-
getUserPrincipalFromIdToken
protected OAuthRealm.OAuthUserInfo getUserPrincipalFromIdToken(String accessToken, String origNonce) throws Exception Default. Derive name of the principal from the ID token- Parameters:
accessToken
-- Returns:
- Throws:
Exception
-
getUserPrincipalFromUserInfoEndpoint
protected OAuthRealm.OAuthUserInfo getUserPrincipalFromUserInfoEndpoint(String accessToken) throws Exception Useful if overwriting this class to retrieve more properties of the principal than its unique name Derive name of the principal from an extra call to the user info endpoint like https://graph.microsoft.com/v1.0/me/ Make sure the token we received earlier is allowed to access it, i.e. requested scope for example includes https://graph.microsoft.com/user.read- Parameters:
accessToken
-- Returns:
- Throws:
Exception
-
callTokenEndpoint
call against oauth2.0 "/token" endpoint to obtain an access token- Parameters:
oauthToken
-- Returns:
- The access token
- Throws:
IOException
-
readJsonProperty
protected com.google.gson.JsonElement readJsonProperty(String jsonString, String propertyName, boolean isPropertyRequired) helper to access plain property of a json string, i.e. readJsonProperty('{ foo:"bar" }', "foo").getAsString() returns "bar"- Parameters:
jsonString
-propertyName
-isPropertyRequired
-- Returns:
- The property
-