-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into pedroro/browser-selector
- Loading branch information
Showing
10 changed files
with
257 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
92 changes: 92 additions & 0 deletions
92
...m/microsoft/identity/common/internal/ui/webview/challengehandlers/NonceRedirectHandler.kt
This file contains 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,92 @@ | ||
// 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.internal.ui.webview.challengehandlers | ||
|
||
import android.webkit.WebView | ||
import com.microsoft.identity.common.java.broker.CommonRefreshTokenCredentialProvider | ||
import com.microsoft.identity.common.adal.internal.AuthenticationConstants | ||
import com.microsoft.identity.common.adal.internal.AuthenticationConstants.Broker.SSO_NONCE_PARAMETER | ||
import com.microsoft.identity.common.adal.internal.util.StringExtensions | ||
import com.microsoft.identity.common.java.opentelemetry.AttributeName | ||
import com.microsoft.identity.common.logging.Logger | ||
import io.opentelemetry.api.trace.Span | ||
import java.net.URL | ||
|
||
/** | ||
* Handler for processing nonce from redirect and attaching new prt credential header on web view. | ||
*/ | ||
class NonceRedirectHandler( | ||
private val webView: WebView, | ||
private val headers: HashMap<String, String>, | ||
private val span : Span | ||
) : IChallengeHandler<URL, Void> { | ||
private val TAG = NonceRedirectHandler::class.java.simpleName | ||
|
||
override fun processChallenge(input: URL) : Void? { | ||
val nonce = getNonceFromRedirectUrl(input) | ||
if (nonce != null) { | ||
modifyHeadersWithNewRefreshTokenCredential(nonce, input.toString()) | ||
} | ||
webView.loadUrl(input.toString(), headers) | ||
return null | ||
} | ||
|
||
private fun getNonceFromRedirectUrl(url: URL): String? { | ||
val parameters = StringExtensions.getUrlParameters(url.toString()) | ||
return parameters[SSO_NONCE_PARAMETER] | ||
} | ||
|
||
private fun getPrtHeader(requestHeaders: HashMap<String, String>): String? { | ||
return requestHeaders[AuthenticationConstants.Broker.PRT_RESPONSE_HEADER] | ||
} | ||
|
||
// Updates the headers by attaching a new refresh token credential header (Generated using the new nonce). | ||
private fun modifyHeadersWithNewRefreshTokenCredential( | ||
nonce: String, | ||
url: String | ||
) { | ||
val methodTag = "$TAG:getHeadersWithNewRefreshTokenCredential" | ||
val prtHeader = getPrtHeader(headers) | ||
if (!prtHeader.isNullOrEmpty()) { | ||
Logger.info(methodTag, "PRT credential header found in headers!") | ||
val username = getUserNameFromWebViewUrl(url) | ||
if (username != null) { | ||
val updatedRefreshTokenCredentialHeader = | ||
CommonRefreshTokenCredentialProvider.getRefreshTokenCredentialUsingNewNonce( | ||
url, username, | ||
nonce | ||
) | ||
if (updatedRefreshTokenCredentialHeader != null) { | ||
headers[AuthenticationConstants.Broker.PRT_RESPONSE_HEADER] = | ||
updatedRefreshTokenCredentialHeader | ||
span.setAttribute(AttributeName.is_new_refresh_token_cred_header_attached.name, true) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getUserNameFromWebViewUrl(url: String): String? { | ||
val parameters: Map<String, String> = StringExtensions.getUrlParameters(url) | ||
return parameters["login_hint"] | ||
} | ||
} |
This file contains 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
52 changes: 52 additions & 0 deletions
52
...rc/main/com/microsoft/identity/common/java/broker/CommonRefreshTokenCredentialProvider.kt
This file contains 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,52 @@ | ||
// 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.broker | ||
|
||
import com.microsoft.identity.common.java.interfaces.IRefreshTokenCredentialProvider | ||
import com.microsoft.identity.common.java.logging.Logger | ||
|
||
/** | ||
* Consumer of commons needs to implement [IRefreshTokenCredentialProvider] interface | ||
* and set it using CommonRefreshTokenCredentialProvider.initializeCommonRefreshTokenCredentialProvider(@NonNull refreshTokenCredentialProvider: IRefreshTokenCredentialProvider) | ||
* to provide prtCredentialHolder to common module. | ||
*/ | ||
object CommonRefreshTokenCredentialProvider : IRefreshTokenCredentialProvider { | ||
private val TAG = CommonRefreshTokenCredentialProvider::class.java.simpleName | ||
private var mRefreshTokenCredentialProvider: IRefreshTokenCredentialProvider? = null | ||
|
||
// Note : This method should only be invoked by broker module. | ||
fun initializeCommonRefreshTokenCredentialProvider(refreshTokenCredentialProvider: IRefreshTokenCredentialProvider) { | ||
val methodTag = "$TAG:initializeCommonRefreshTokenCredentialProvider" | ||
Logger.info(methodTag, "Initializing common prt credential provider with " + refreshTokenCredentialProvider.javaClass.simpleName) | ||
mRefreshTokenCredentialProvider = refreshTokenCredentialProvider | ||
} | ||
|
||
override fun getRefreshTokenCredentialUsingNewNonce(inputUrl : String, username : String, nonce : String) : String? { | ||
val methodTag = "$TAG:getRefreshTokenCredentialUsingNewNonce"; | ||
if (mRefreshTokenCredentialProvider != null) { | ||
return mRefreshTokenCredentialProvider!!.getRefreshTokenCredentialUsingNewNonce(inputUrl, username, nonce) | ||
} | ||
Logger.warn(methodTag, "mRefreshTokenCredentialHolder is not initialized!") | ||
return null | ||
} | ||
} |
This file contains 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
37 changes: 37 additions & 0 deletions
37
...src/main/com/microsoft/identity/common/java/interfaces/IRefreshTokenCredentialProvider.kt
This file contains 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,37 @@ | ||
// 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.interfaces; | ||
|
||
|
||
/** | ||
* Consumer of commons needs to implement [IRefreshTokenCredentialProvider] interface | ||
* and set it using CommonRefreshTokenCredentialProvider.initializeCommonRefreshTokenCredentialProvider(@NonNull refreshTokenCredentialProvider: IRefreshTokenCredentialProvider) | ||
* to provide prtCredentialHolder to common module. | ||
*/ | ||
interface IRefreshTokenCredentialProvider { | ||
|
||
/** | ||
* Gets refresh token credential using nonce retrieved from webview. | ||
*/ | ||
fun getRefreshTokenCredentialUsingNewNonce(inputUrl : String, username : String, nonce : String) : String? | ||
} |
This file contains 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
This file contains 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