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.realm.Realm
,org.apache.shiro.util.Initializable
,org.apache.shiro.util.Nameable
public class OAuthRealm extends org.apache.shiro.realm.AuthenticatingRealm
This one is an authenticating realm only as such we obtain identity from resource server and let other realms obtain authorization data from elsewhere
-
-
Constructor Summary
Constructors Constructor Description OAuthRealm()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
createAccessToken(OAuthToken oauthToken)
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 viacreateAccessToken(OAuthToken)
and passes togetUserPrincipal(String)
then constructsSimpleAuthenticationInfo
with returned principaljava.lang.String
getApiEndpoint()
OAuthAuthenticatingFilter
getAuthenticator()
java.lang.String
getPrincipalPropertyName()
java.lang.String
getTokenEndpoint()
protected java.lang.Object
getUserPrincipal(java.lang.String accessToken)
implements getting user principal from resource serverprotected com.google.gson.JsonElement
readJsonProperty(java.lang.String jsonString, java.lang.String propertyName)
helper to access plain property of a json string, i.e. readJsonProperty('{ foo:"bar" }', "foo").getAsString() returns "bar"void
setApiEndpoint(java.lang.String apiEndPoint)
the API endpoint to obtain information about user basically providing JSON data containinggetPrincipalPropertyName()
to extractvoid
setAuthenticator(OAuthAuthenticatingFilter authenticator)
we need to set the instance of filter class to determine in realm if that should support processing, incase we have many realms bound with multiple authenticators for given tokenvoid
setClientSecret(java.lang.String clientSecret)
secret we need to obtain access token from /token endpoint, used increateAccessToken(OAuthToken)
void
setPrincipalPropertyName(java.lang.String principalPropertyName)
this is the property we extract from JSON response and use as a principalvoid
setTokenEndpoint(java.lang.String tokenEndpoint)
sets the endpoint to /token APIboolean
supports(org.apache.shiro.authc.AuthenticationToken token)
acceptsOAuthToken
and only from given authenticator, if provided-
Methods 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
-
-
-
-
Method Detail
-
setAuthenticator
public void setAuthenticator(OAuthAuthenticatingFilter authenticator)
we need to set the instance of filter class to determine in realm if that should support processing, incase we have many realms bound with multiple authenticators for given token- Parameters:
authenticator
-
-
getAuthenticator
public OAuthAuthenticatingFilter getAuthenticator()
-
setTokenEndpoint
public void setTokenEndpoint(java.lang.String tokenEndpoint)
sets the endpoint to /token API- Parameters:
tokenEndpoint
-
-
getTokenEndpoint
public java.lang.String getTokenEndpoint()
-
setApiEndpoint
public void setApiEndpoint(java.lang.String apiEndPoint)
the API endpoint to obtain information about user basically providing JSON data containinggetPrincipalPropertyName()
to extract- Parameters:
apiEndPoint
- The endpoint to get principal's JSON
-
getApiEndpoint
public java.lang.String getApiEndpoint()
-
setClientSecret
public void setClientSecret(java.lang.String clientSecret)
secret we need to obtain access token from /token endpoint, used increateAccessToken(OAuthToken)
- Parameters:
clientSecret
-
-
setPrincipalPropertyName
public void setPrincipalPropertyName(java.lang.String principalPropertyName)
this is the property we extract from JSON response and use as a principal- Parameters:
principalPropertyName
-
-
getPrincipalPropertyName
public java.lang.String getPrincipalPropertyName()
-
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 viacreateAccessToken(OAuthToken)
and passes togetUserPrincipal(String)
then constructsSimpleAuthenticationInfo
with returned principal- Specified by:
doGetAuthenticationInfo
in classorg.apache.shiro.realm.AuthenticatingRealm
-
getUserPrincipal
protected java.lang.Object getUserPrincipal(java.lang.String accessToken) throws java.io.IOException
implements getting user principal from resource server- Parameters:
accessToken
- - which is already obtained from authority server and ready to be used as a bearer- Returns:
- user principal, i.e. the email-address or other identifier
- Throws:
java.io.IOException
-
createAccessToken
protected java.lang.String createAccessToken(OAuthToken oauthToken) throws java.io.IOException
call against oauth2.0 "/token" endpoint to obtain an access token- Parameters:
oauthToken
-- Returns:
- The access token
- Throws:
java.io.IOException
-
readJsonProperty
protected com.google.gson.JsonElement readJsonProperty(java.lang.String jsonString, java.lang.String propertyName)
helper to access plain property of a json string, i.e. readJsonProperty('{ foo:"bar" }', "foo").getAsString() returns "bar"- Parameters:
jsonString
-propertyName
-- Returns:
- The property
-
-