-
Notifications
You must be signed in to change notification settings - Fork 46
Implement TenantUtil object with methods to extract tenant and tenantID from identifiers, Fixes AB#3370810 #2761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1087721
Implement TenantUtil object with methods to extract tenant and tenant…
p3dr0rv a7a1f89
Fix missing newline at end of file in TenantUtil.kt
p3dr0rv fe36d61
Update common4j/src/main/com/microsoft/identity/common/java/util/Tena…
p3dr0rv 4971bdf
Update common4j/src/main/com/microsoft/identity/common/java/util/Tena…
p3dr0rv 217d977
Refactor formatting in getTenantIdFromLoginHint function for consistency
p3dr0rv 7a337e6
Add entry for TenantUtil implementation in changelog
p3dr0rv e467cfe
Refactor TenantUtil to improve identifier handling and enhance docume…
p3dr0rv d424fbb
Refactor documentation in getTenantIdFromLoginHint to simplify parame…
p3dr0rv 084cc3c
Update common4j/src/main/com/microsoft/identity/common/java/util/Tena…
p3dr0rv f941750
Update common4j/src/main/com/microsoft/identity/common/java/util/Tena…
p3dr0rv 2af90e3
Add unit tests for TenantUtil methods to enhance coverage and validation
p3dr0rv baeab16
Add tests for getTenantIdFromLoginHint to validate handling of null c…
p3dr0rv e006a35
Merge branch 'dev' into pedroro/tenantUtil
p3dr0rv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
common4j/src/main/com/microsoft/identity/common/java/util/TenantUtil.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // All rights reserved. | ||
| // | ||
| // This code is licensed under the MIT License. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files(the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions : | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| package com.microsoft.identity.common.java.util | ||
|
|
||
| import com.microsoft.identity.common.java.authorities.AzureActiveDirectoryAudience | ||
| import com.microsoft.identity.common.java.logging.Logger | ||
| import com.microsoft.identity.common.java.providers.microsoft.azureactivedirectory.AzureActiveDirectory | ||
|
|
||
| /** | ||
| * Utility object for tenant-related operations. | ||
| * | ||
| * Provides methods to extract tenant information from various identifier formats | ||
| * such as email addresses, UPNs (User Principal Names), and tenant GUIDs. | ||
| */ | ||
| object TenantUtil { | ||
| private const val TAG: String = "TenantUtil" | ||
| private val EMAIL_REGEX = Regex("""^.*@.*$""") | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private val UUID_REGEX = Regex("""^[0-9A-Fa-f\-]{36}$""") | ||
|
|
||
|
|
||
| /** | ||
| * Extracts tenant information from an identifier. | ||
| * | ||
| * This method can handle two types of identifiers: | ||
| * - Email addresses/UPNs: Returns the domain part after the "@" symbol | ||
| * - Tenant GUIDs: Returns the GUID as-is | ||
| * | ||
| * @param identifier The identifier string which could be: | ||
| * - An email address or UPN (e.g., "[email protected]") | ||
| * - A GUID representing a tenant ID (e.g., "12345678-1234-1234-1234-123456789012") | ||
| * - Can be null or blank | ||
| * @return The extracted tenant (hostname for UPNs or tenant ID for GUIDs), | ||
| * or null if the identifier is invalid, null, or blank | ||
| */ | ||
| fun getTenantFromIdentifier(identifier: String?): String? { | ||
p3dr0rv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val methodTag = "$TAG:getTenantFromIdentifier" | ||
| if (identifier.isNullOrBlank()) { | ||
| return null | ||
| } | ||
|
|
||
| if (UUID_REGEX.matches(identifier)) { | ||
| return identifier | ||
| } | ||
|
|
||
| if (EMAIL_REGEX.matches(identifier)) { | ||
| identifier.substringAfter("@").trim() | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Logger.warn(methodTag, "Identifier is neither a valid email/UPN nor a GUID.") | ||
| return null | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Extracts tenant ID from a login hint by resolving the tenant information. | ||
| * | ||
| * This method first extracts the tenant name from the login hint, then attempts to | ||
| * resolve it to a tenant ID by loading the OpenID provider configuration metadata | ||
| * for the specified tenant. | ||
| * | ||
| * @param loginHint The login hint string which could be: | ||
| * - An email address or UPN (e.g., "[email protected]") | ||
| * - A GUID representing a tenant ID | ||
| * - Can be null or blank | ||
| * @param correlationId Correlation ID for the request, used for logging and debugging purposes. | ||
| * Can be null. | ||
| * @return The resolved tenant ID if successful, null if the login hint is invalid, | ||
| * the tenant cannot be resolved, or if an error occurs during resolution | ||
| */ | ||
| fun getTenantIdFromLoginHint(loginHint: String?, correlationId: String?): String? { | ||
| val methodTag = "$TAG:getTenantIdFromLoginHint" | ||
| val tenantName = getTenantFromIdentifier(loginHint) ?: run { | ||
| Logger.warn(methodTag, correlationId, "Login hint is invalid or empty.") | ||
| return null | ||
| } | ||
| try { | ||
| val configuration = | ||
| AzureActiveDirectory.loadOpenIdProviderConfigurationMetadataForTenant(tenantName) | ||
| val tenantId = | ||
| AzureActiveDirectoryAudience.getTenantIdFromOpenIdProviderConfiguration(configuration) | ||
| Logger.info(methodTag, correlationId, "Successfully got tenant ID from login hint.") | ||
| return tenantId | ||
| } catch (e: Exception) { | ||
| Logger.error(methodTag, correlationId, "Failed to get tenant ID from login hint.", e) | ||
| return null | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.