Skip to content

Refactor tenant domain handling in TOTP authentication flow to support shared user direct login to organizations.#232

Open
Yasasr1 wants to merge 1 commit intowso2-extensions:masterfrom
Yasasr1:v3.4.2-shared-user-dl-wrpper
Open

Refactor tenant domain handling in TOTP authentication flow to support shared user direct login to organizations.#232
Yasasr1 wants to merge 1 commit intowso2-extensions:masterfrom
Yasasr1:v3.4.2-shared-user-dl-wrpper

Conversation

@Yasasr1
Copy link
Copy Markdown
Contributor

@Yasasr1 Yasasr1 commented Apr 8, 2026

This pull request refactors how tenant domains are handled within the TOTPAuthenticator class to properly handle shared user direct login flows to sub-organizations. The main focus is on distinguishing between the user's tenant domain and the authentication context's tenant domain, ensuring the correct domain is used throughout the authentication process.

Tenant domain handling improvements:

  • Introduced a clear distinction between userTenantDomain (from the authenticated user) and tenantDomain (from the authentication context), and consistently use userTenantDomain where appropriate.

Dependent on

Related issue

Copilot AI review requested due to automatic review settings April 8, 2026 04:38
Comment on lines +224 to 227
String userTenantDomain = authenticatedUserFromContext.getTenantDomain();
String tenantDomain = context.getTenantDomain();
if (StringUtils.isBlank(userTenantDomain)) {
throw new AuthenticationFailedException(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 1

Suggested change
String userTenantDomain = authenticatedUserFromContext.getTenantDomain();
String tenantDomain = context.getTenantDomain();
if (StringUtils.isBlank(userTenantDomain)) {
throw new AuthenticationFailedException(
String userTenantDomain = authenticatedUserFromContext.getTenantDomain();
String tenantDomain = context.getTenantDomain();
log.debug("Initiating TOTP authentication for user from tenant domain: " + userTenantDomain);
if (StringUtils.isBlank(userTenantDomain)) {

Comment on lines 384 to +386
Map<String, String> claims;
if (isInitialFederationAttempt) {
claims = TOTPKeyGenerator.generateClaimsForFedUserVerifySecretKey(username, tenantDomain,
context);
claims = TOTPKeyGenerator.generateClaimsForFedUserVerifySecretKey(username,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 2

Suggested change
Map<String, String> claims;
if (isInitialFederationAttempt) {
claims = TOTPKeyGenerator.generateClaimsForFedUserVerifySecretKey(username, tenantDomain,
context);
claims = TOTPKeyGenerator.generateClaimsForFedUserVerifySecretKey(username,
Map<String, String> claims;
if (isInitialFederationAttempt) {
if (log.isDebugEnabled()) {
log.debug("Generating claims for federated user verification for user: " + loggableUsername);
}
claims = TOTPKeyGenerator.generateClaimsForFedUserVerifySecretKey(username,

Copy link
Copy Markdown

@wso2-engineering wso2-engineering bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Agent Log Improvement Checklist

⚠️ Warning: AI-Generated Review Comments

  • The log-related comments and suggestions in this review were generated by an AI tool to assist with identifying potential improvements. Purpose of reviewing the code for log improvements is to improve the troubleshooting capabilities of our products.
  • Please make sure to manually review and validate all suggestions before applying any changes. Not every code suggestion would make sense or add value to our purpose. Therefore, you have the freedom to decide which of the suggestions are helpful.

✅ Before merging this pull request:

  • Review all AI-generated comments for accuracy and relevance.
  • Complete and verify the table below. We need your feedback to measure the accuracy of these suggestions and the value they add. If you are rejecting a certain code suggestion, please mention the reason briefly in the suggestion for us to capture it.
Comment Accepted (Y/N) Reason
#### Log Improvement Suggestion No: 1
#### Log Improvement Suggestion No: 2

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors tenant-domain usage in TOTPAuthenticator to correctly support shared-user direct login flows by separating the authenticated user’s tenant domain from the authentication-context tenant domain.

Changes:

  • Introduces userTenantDomain (from AuthenticatedUser) alongside tenantDomain (from AuthenticationContext).
  • Loads application-authentication configuration using the context tenant domain, while resolving usernames/claims using the user tenant domain.
  • Replaces a direct .equals(...) check with StringUtils.equals(...) for safer super-tenant comparison.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 232 to 236
TOTPAuthenticatorConstants.AUTHENTICATOR_NAME);
if (!tenantDomain.equals(TOTPAuthenticatorConstants.SUPER_TENANT_DOMAIN)) {
if (!StringUtils.equals(TOTPAuthenticatorConstants.SUPER_TENANT_DOMAIN, tenantDomain)) {
IdentityHelperUtil
.loadApplicationAuthenticationXMLFromRegistry(context, getName(), tenantDomain);
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tenantDomain is taken from context.getTenantDomain() and can be blank (there are other places in the codebase that explicitly return early when context.getTenantDomain() is blank). In that case, !StringUtils.equals(SUPER_TENANT_DOMAIN, tenantDomain) evaluates true and loadApplicationAuthenticationXMLFromRegistry(...) is invoked with a blank/null tenant domain, which can lead to incorrect config resolution or failures. Please guard against a blank tenantDomain here (e.g., fail fast with a clear AuthenticationFailedException, or fall back to a safe default if that’s the intended behavior) before calling into the registry loader.

Copilot uses AI. Check for mistakes.
Comment on lines +224 to +226
String userTenantDomain = authenticatedUserFromContext.getTenantDomain();
String tenantDomain = context.getTenantDomain();
if (StringUtils.isBlank(userTenantDomain)) {
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactor introduces two tenant domains (userTenantDomain vs context tenant domain) but the existing unit tests for initiateAuthenticationRequest appear to only exercise the super-tenant/same-tenant path. Please add a test that covers a shared-user scenario where authenticatedUserFromContext.getTenantDomain() differs from context.getTenantDomain(), asserting that application-authentication.xml is loaded using the context tenant while username/claims are resolved using the user tenant (and that no registry load happens when the context tenant is super tenant).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants