-
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 1 commit
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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
| // 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 | ||
| import java.util.regex.Matcher | ||
| import java.util.regex.Pattern | ||
|
|
||
| object TenantUtil { | ||
| private const val TAG: String = "TenantUtil" | ||
| private const val IDENTIFIER_REGEX: String = "(.*@.*|^[0-9A-Fa-f\\-]{36}$)" | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private val PAIR_REGEX: Pattern = Pattern.compile(IDENTIFIER_REGEX) | ||
|
|
||
| /** | ||
| * Extracts tenant from an identifier. | ||
| * | ||
| * @param identifier {@link String} This could be an email address/UPN or a GUID (tenant ID). | ||
| * @return a tenant (hostname or tenant ID). | ||
| */ | ||
| fun getTenantFromIdentifier(identifier: String?): String? { | ||
p3dr0rv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (identifier.isNullOrBlank()) { | ||
| return null | ||
| } | ||
| val matcher: Matcher = PAIR_REGEX.matcher(identifier) | ||
| if (!matcher.matches()) { | ||
| return null | ||
| } | ||
| // If identifier is a UPN, extracts a host from it. | ||
| if (identifier.contains("@")) { | ||
| return identifier.substring(identifier.indexOf("@") + 1).trim() | ||
| } | ||
| return identifier | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Extracts tenant ID from login hint. | ||
| * | ||
| * @param loginHint {@link String} This could be an email address/UPN or a GUID (tenant ID). | ||
| * @param correlationId Correlation ID for the request to be logged. | ||
| * @return a tenant ID if found, null otherwise. | ||
| */ | ||
| fun getTenantIdFromLoginHnt(loginHint: String?, correlationId : String?): String? { | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val methodTag = "$TAG:getTenantIdFromLoginHnt" | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (loginHint.isNullOrBlank()) { | ||
| Logger.info(methodTag, correlationId, "Login hint is empty") | ||
| return null | ||
| } | ||
| val tenantName = getTenantFromIdentifier(loginHint) | ||
| if (tenantName.isNullOrBlank()) { | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Logger.warn(methodTag, correlationId, "Tenant name is 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.