Package org.codelibs.fess.sso.entraid
Class EntraIdAuthenticator
java.lang.Object
org.codelibs.fess.sso.entraid.EntraIdAuthenticator
- All Implemented Interfaces:
SsoAuthenticator
Microsoft Entra ID SSO authenticator implementation.
Handles OAuth2/OpenID Connect authentication flow with Entra ID.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classRepresents state data stored during the OAuth2 authentication flow. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringLegacy configuration key for Azure AD authority URL.protected static final StringLegacy configuration key for Azure AD client ID.protected static final StringLegacy configuration key for Azure AD client secret.protected static final StringLegacy configuration key for Azure AD default groups.protected static final StringLegacy configuration key for Azure AD default roles.protected static final StringLegacy configuration key for Azure AD reply URL.protected static final StringLegacy configuration key for Azure AD state time-to-live.protected static final StringLegacy configuration key for Azure AD tenant ID.protected longTimeout for token acquisition in milliseconds.protected static final StringOAuth2 authorization code parameter name.protected static final StringConfiguration key for Entra ID authority URL.protected static final StringConfiguration key for Entra ID client ID.protected static final StringConfiguration key for Entra ID client secret.protected static final StringConfiguration key for Entra ID default groups.protected static final StringConfiguration key for Entra ID default roles.protected static final StringConfiguration key for Entra ID reply URL.protected static final StringConfiguration key for Entra ID state time-to-live.protected static final StringConfiguration key for Entra ID tenant ID.protected static final StringOAuth2 error parameter name.protected static final StringOAuth2 error description parameter name.protected static final StringOAuth2 error URI parameter name.Cache for storing group information to reduce API calls.protected longGroup cache expiry time in seconds.protected static final StringOpenID Connect ID token parameter name.protected intMaximum depth for processing nested groups to prevent infinite loops.protected static final StringOAuth2 state parameter name.protected static final StringSession attribute key for storing Entra ID states.protected booleanUse V2 endpoint. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddGroupOrRoleName(List<String> list, String value, boolean useDomainServices) Adds a group or role name to the specified list.protected booleancontainsAuthenticationData(jakarta.servlet.http.HttpServletRequest request) Checks if the request contains authentication data from Entra ID.protected com.microsoft.aad.msal4j.IAuthenticationResultgetAccessToken(com.nimbusds.oauth2.sdk.AuthorizationCode authorizationCode, String currentUri) Obtains an access token using an authorization code.com.microsoft.aad.msal4j.IAuthenticationResultgetAccessToken(String refreshToken) Obtains an access token using a refresh token.protected StringGets the Entra ID authority URL from configuration.protected StringgetAuthUrl(jakarta.servlet.http.HttpServletRequest request) Generates the Entra ID authorization URL for the authentication request.protected StringGets the Entra ID client ID from configuration.protected StringGets the Entra ID client secret from configuration.Gets the default group list for users.Gets the default role list for users.org.lastaflute.web.login.credential.LoginCredentialGets the login credential for SSO authentication.Retrieves parent group information for the specified group ID.getParentGroup(EntraIdCredential.EntraIdUser user, String id, int depth) Retrieves parent group information for the specified group ID with depth tracking.protected StringgetReplyUrl(jakarta.servlet.http.HttpServletRequest request) Gets the reply URL for Entra ID authentication.org.lastaflute.web.response.ActionResponsegetResponse(SsoResponseType responseType) Gets the action response for the specified SSO response type.protected longGets the state time-to-live from configuration.protected StringGets the Entra ID tenant ID from configuration.voidinit()Initializes the Entra ID authenticator.logout(org.codelibs.fess.mylasta.action.FessUserBean user) Performs logout for the specified user.protected com.nimbusds.openid.connect.sdk.AuthenticationResponseParses the authentication response from Entra ID.protected org.lastaflute.web.login.credential.LoginCredentialprocessAuthenticationData(jakarta.servlet.http.HttpServletRequest request) Processes authentication data from the OAuth2 callback.protected voidprocessDirectMemberOf(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, List<String> groupIdsForParentLookup, String url) Processes direct member-of information from Microsoft Graph API without parent group lookup.protected voidprocessGroup(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String id) Processes individual group information.protected voidprocessMemberOf(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String url) Processes member-of information from Microsoft Graph API.protected voidprocessParentGroup(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String id) Processes parent group information for nested groups.protected voidprocessParentGroup(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String id, int depth) Processes parent group information for nested groups with depth tracking.com.microsoft.aad.msal4j.IAuthenticationResultAttempts to refresh tokens silently using the MSAL4J silent authentication flow.protected EntraIdAuthenticator.StateDataremoveStateFromSession(jakarta.servlet.http.HttpSession session, String state) Removes and returns state data from the HTTP session.voidResolves credential using the provided resolver.protected voidscheduleParentGroupLookup(EntraIdCredential.EntraIdUser user, List<String> initialGroups, List<String> initialRoles, List<String> groupIds) Schedules asynchronous parent group lookup using TimeoutManager.voidsetAcquisitionTimeout(long acquisitionTimeout) Sets the token acquisition timeout.voidsetGroupCacheExpiry(long groupCacheExpiry) Sets the group cache expiry time.voidsetMaxGroupDepth(int maxGroupDepth) Sets the maximum group depth for nested group processing.voidsetUseV2Endpoint(boolean useV2Endpoint) Enable to use V2 endpoint.protected voidstoreStateInSession(jakarta.servlet.http.HttpSession session, String state, String nonce) Stores state and nonce information in the HTTP session.voidUpdates the user's group and role membership information with lazy loading for parent groups.protected voidvalidateAuthRespMatchesCodeFlow(com.nimbusds.openid.connect.sdk.AuthenticationSuccessResponse oidcResponse) Validates that the authentication response matches the authorization code flow.protected voidvalidateNonce(EntraIdAuthenticator.StateData stateData, com.microsoft.aad.msal4j.IAuthenticationResult authData) Validates the nonce in the authentication result.protected EntraIdAuthenticator.StateDatavalidateState(jakarta.servlet.http.HttpSession session, String state) Validates the OAuth2 state parameter.
-
Field Details
-
ENTRAID_STATE_TTL
Configuration key for Entra ID state time-to-live.- See Also:
-
ENTRAID_AUTHORITY
Configuration key for Entra ID authority URL.- See Also:
-
ENTRAID_TENANT
Configuration key for Entra ID tenant ID.- See Also:
-
ENTRAID_CLIENT_SECRET
Configuration key for Entra ID client secret.- See Also:
-
ENTRAID_CLIENT_ID
Configuration key for Entra ID client ID.- See Also:
-
ENTRAID_REPLY_URL
Configuration key for Entra ID reply URL.- See Also:
-
ENTRAID_DEFAULT_GROUPS
Configuration key for Entra ID default groups.- See Also:
-
ENTRAID_DEFAULT_ROLES
Configuration key for Entra ID default roles.- See Also:
-
AAD_STATE_TTL
Legacy configuration key for Azure AD state time-to-live.- See Also:
-
AAD_AUTHORITY
Legacy configuration key for Azure AD authority URL.- See Also:
-
AAD_TENANT
Legacy configuration key for Azure AD tenant ID.- See Also:
-
AAD_CLIENT_SECRET
Legacy configuration key for Azure AD client secret.- See Also:
-
AAD_CLIENT_ID
Legacy configuration key for Azure AD client ID.- See Also:
-
AAD_REPLY_URL
Legacy configuration key for Azure AD reply URL.- See Also:
-
AAD_DEFAULT_GROUPS
Legacy configuration key for Azure AD default groups.- See Also:
-
AAD_DEFAULT_ROLES
Legacy configuration key for Azure AD default roles.- See Also:
-
STATES
Session attribute key for storing Entra ID states.- See Also:
-
STATE
OAuth2 state parameter name.- See Also:
-
ERROR
OAuth2 error parameter name.- See Also:
-
ERROR_DESCRIPTION
OAuth2 error description parameter name.- See Also:
-
ERROR_URI
OAuth2 error URI parameter name.- See Also:
-
ID_TOKEN
OpenID Connect ID token parameter name.- See Also:
-
CODE
OAuth2 authorization code parameter name.- See Also:
-
acquisitionTimeout
protected long acquisitionTimeoutTimeout for token acquisition in milliseconds. -
groupCache
protected com.google.common.cache.Cache<String,org.codelibs.core.misc.Pair<String[], groupCacheString[]>> Cache for storing group information to reduce API calls. -
groupCacheExpiry
protected long groupCacheExpiryGroup cache expiry time in seconds. -
maxGroupDepth
protected int maxGroupDepthMaximum depth for processing nested groups to prevent infinite loops. -
useV2Endpoint
protected boolean useV2EndpointUse V2 endpoint.
-
-
Constructor Details
-
EntraIdAuthenticator
public EntraIdAuthenticator()Default constructor for EntraIdAuthenticator.
-
-
Method Details
-
init
@PostConstruct public void init()Initializes the Entra ID authenticator. Registers this authenticator with the SSO manager and sets up group cache. -
getLoginCredential
public org.lastaflute.web.login.credential.LoginCredential getLoginCredential()Description copied from interface:SsoAuthenticatorGets the login credential for SSO authentication.- Specified by:
getLoginCredentialin interfaceSsoAuthenticator- Returns:
- The login credential.
-
getAuthUrl
Generates the Entra ID authorization URL for the authentication request.- Parameters:
request- The HTTP servlet request.- Returns:
- The authorization URL to redirect the user to.
-
storeStateInSession
protected void storeStateInSession(jakarta.servlet.http.HttpSession session, String state, String nonce) Stores state and nonce information in the HTTP session.- Parameters:
session- The HTTP session.state- The OAuth2 state parameter.nonce- The OpenID Connect nonce parameter.
-
processAuthenticationData
protected org.lastaflute.web.login.credential.LoginCredential processAuthenticationData(jakarta.servlet.http.HttpServletRequest request) Processes authentication data from the OAuth2 callback.- Parameters:
request- The HTTP servlet request containing authentication data.- Returns:
- The login credential or null if processing fails.
-
parseAuthenticationResponse
protected com.nimbusds.openid.connect.sdk.AuthenticationResponse parseAuthenticationResponse(String url, Map<String, List<String>> params) Parses the authentication response from Entra ID.- Parameters:
url- The response URL.params- The response parameters.- Returns:
- The parsed authentication response.
-
validateNonce
protected void validateNonce(EntraIdAuthenticator.StateData stateData, com.microsoft.aad.msal4j.IAuthenticationResult authData) Validates the nonce in the authentication result.- Parameters:
stateData- The stored state data containing the expected nonce.authData- The authentication result containing the actual nonce.
-
getAccessToken
Obtains an access token using a refresh token.- Parameters:
refreshToken- The refresh token to use for token acquisition.- Returns:
- The authentication result containing the access token.
-
getAccessToken
protected com.microsoft.aad.msal4j.IAuthenticationResult getAccessToken(com.nimbusds.oauth2.sdk.AuthorizationCode authorizationCode, String currentUri) Obtains an access token using an authorization code.- Parameters:
authorizationCode- The authorization code received from Entra ID.currentUri- The current URI for the redirect.- Returns:
- The authentication result containing the access token.
-
refreshTokenSilently
public com.microsoft.aad.msal4j.IAuthenticationResult refreshTokenSilently(EntraIdCredential.EntraIdUser user) Attempts to refresh tokens silently using the MSAL4J silent authentication flow.- Parameters:
user- The Entra ID user whose tokens need to be refreshed.- Returns:
- The new authentication result, or null if silent refresh failed.
-
validateAuthRespMatchesCodeFlow
protected void validateAuthRespMatchesCodeFlow(com.nimbusds.openid.connect.sdk.AuthenticationSuccessResponse oidcResponse) Validates that the authentication response matches the authorization code flow.- Parameters:
oidcResponse- The OpenID Connect authentication success response.
-
validateState
protected EntraIdAuthenticator.StateData validateState(jakarta.servlet.http.HttpSession session, String state) Validates the OAuth2 state parameter.- Parameters:
session- The HTTP session containing stored state data.state- The state parameter to validate.- Returns:
- The validated state data.
-
removeStateFromSession
protected EntraIdAuthenticator.StateData removeStateFromSession(jakarta.servlet.http.HttpSession session, String state) Removes and returns state data from the HTTP session.- Parameters:
session- The HTTP session.state- The state parameter to remove.- Returns:
- The removed state data or null if not found.
-
containsAuthenticationData
protected boolean containsAuthenticationData(jakarta.servlet.http.HttpServletRequest request) Checks if the request contains authentication data from Entra ID.- Parameters:
request- The HTTP servlet request to check.- Returns:
- True if authentication data is present, false otherwise.
-
updateMemberOf
Updates the user's group and role membership information with lazy loading for parent groups. Direct groups are retrieved synchronously, while parent groups are fetched asynchronously to avoid login delays when users have many nested group memberships.- Parameters:
user- The Entra ID user to update.
-
processMemberOf
protected void processMemberOf(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String url) Processes member-of information from Microsoft Graph API.- Parameters:
user- The Entra ID user.groupList- The list to add group names to.roleList- The list to add role names to.url- The Microsoft Graph API URL.
-
addGroupOrRoleName
Adds a group or role name to the specified list.- Parameters:
list- The list to add the group or role name to.value- The group or role name value.useDomainServices- Whether to use domain services for group resolution.
-
processDirectMemberOf
protected void processDirectMemberOf(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, List<String> groupIdsForParentLookup, String url) Processes direct member-of information from Microsoft Graph API without parent group lookup. This method retrieves only direct group memberships and collects group IDs for later asynchronous parent group lookup.- Parameters:
user- The Entra ID user.groupList- The list to add group names to.roleList- The list to add role names to.groupIdsForParentLookup- The list to collect group IDs for later parent lookup.url- The Microsoft Graph API URL.
-
scheduleParentGroupLookup
protected void scheduleParentGroupLookup(EntraIdCredential.EntraIdUser user, List<String> initialGroups, List<String> initialRoles, List<String> groupIds) Schedules asynchronous parent group lookup using TimeoutManager. This method defers the retrieval of nested group information to avoid login delays.- Parameters:
user- The Entra ID user.initialGroups- The initial group list to be updated.initialRoles- The initial role list to be updated.groupIds- The list of group IDs to lookup parent groups for.
-
processParentGroup
protected void processParentGroup(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String id) Processes parent group information for nested groups.- Parameters:
user- The Entra ID user.groupList- The list to add group names to.roleList- The list to add role names to.id- The group ID to process.
-
processParentGroup
protected void processParentGroup(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String id, int depth) Processes parent group information for nested groups with depth tracking.- Parameters:
user- The Entra ID user.groupList- The list to add group names to.roleList- The list to add role names to.id- The group ID to process.depth- The current recursion depth.
-
getParentGroup
protected org.codelibs.core.misc.Pair<String[],String[]> getParentGroup(EntraIdCredential.EntraIdUser user, String id) Retrieves parent group information for the specified group ID.- Parameters:
user- The Entra ID user.id- The group ID to get parent information for.- Returns:
- A pair containing group names and role names.
-
getParentGroup
protected org.codelibs.core.misc.Pair<String[],String[]> getParentGroup(EntraIdCredential.EntraIdUser user, String id, int depth) Retrieves parent group information for the specified group ID with depth tracking.- Parameters:
user- The Entra ID user.id- The group ID to get parent information for.depth- The current recursion depth.- Returns:
- A pair containing group names and role names.
-
processGroup
protected void processGroup(EntraIdCredential.EntraIdUser user, List<String> groupList, List<String> roleList, String id) Processes individual group information.- Parameters:
user- The Entra ID user.groupList- The list to add group names to.roleList- The list to add role names to.id- The group ID to process.
-
getDefaultGroupList
Gets the default group list for users. Uses new entraid.default.groups key with fallback to legacy aad.default.groups.- Returns:
- The default group list.
-
getDefaultRoleList
Gets the default role list for users. Uses new entraid.default.roles key with fallback to legacy aad.default.roles.- Returns:
- The default role list.
-
getClientId
Gets the Entra ID client ID from configuration. Uses new entraid.client.id key with fallback to legacy aad.client.id.- Returns:
- The client ID.
-
getClientSecret
Gets the Entra ID client secret from configuration. Uses new entraid.client.secret key with fallback to legacy aad.client.secret.- Returns:
- The client secret.
-
getTenant
Gets the Entra ID tenant ID from configuration. Uses new entraid.tenant key with fallback to legacy aad.tenant.- Returns:
- The tenant ID.
-
getAuthority
Gets the Entra ID authority URL from configuration. Uses new entraid.authority key with fallback to legacy aad.authority.- Returns:
- The authority URL.
-
getStateTtl
protected long getStateTtl()Gets the state time-to-live from configuration. Uses new entraid.state.ttl key with fallback to legacy aad.state.ttl.- Returns:
- The state TTL in milliseconds.
-
getReplyUrl
Gets the reply URL for Entra ID authentication. Uses new entraid.reply.url key with fallback to legacy aad.reply.url.- Parameters:
request- The HTTP servlet request.- Returns:
- The reply URL.
-
resolveCredential
Description copied from interface:SsoAuthenticatorResolves credential using the provided resolver.- Specified by:
resolveCredentialin interfaceSsoAuthenticator- Parameters:
resolver- The login credential resolver.
-
setAcquisitionTimeout
public void setAcquisitionTimeout(long acquisitionTimeout) Sets the token acquisition timeout.- Parameters:
acquisitionTimeout- The timeout in milliseconds.
-
setGroupCacheExpiry
public void setGroupCacheExpiry(long groupCacheExpiry) Sets the group cache expiry time.- Parameters:
groupCacheExpiry- The cache expiry time in seconds.
-
setMaxGroupDepth
public void setMaxGroupDepth(int maxGroupDepth) Sets the maximum group depth for nested group processing.- Parameters:
maxGroupDepth- The maximum depth for nested groups.
-
getResponse
Description copied from interface:SsoAuthenticatorGets the action response for the specified SSO response type.- Specified by:
getResponsein interfaceSsoAuthenticator- Parameters:
responseType- The type of SSO response required.- Returns:
- The action response.
-
logout
Description copied from interface:SsoAuthenticatorPerforms logout for the specified user.- Specified by:
logoutin interfaceSsoAuthenticator- Parameters:
user- The user to logout.- Returns:
- The logout URL or null if not applicable.
-
setUseV2Endpoint
public void setUseV2Endpoint(boolean useV2Endpoint) Enable to use V2 endpoint.- Parameters:
useV2Endpoint- true if using V2 endpoint.
-