Skip to content

Commit cd2526e

Browse files
authored
Merge branch 'dev' into rapong/revert
2 parents e9f0aba + 13c86dd commit cd2526e

16 files changed

Lines changed: 163 additions & 96 deletions

File tree

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
vNext
22
----------
3+
- [MINOR] Add telemetry for the switch browser protocol (#2612)
34

45
Version 21.0.0
56
----------
@@ -11,6 +12,7 @@ Version 21.0.0
1112
- [PATCH] Track MAM flow in telemetry (#2608)
1213
- [PATCH] Fix multiple prompts issue in cross cloud request (#2599)
1314
- [PATCH] Corrected error handling in cross cloud scenario (#2602)
15+
- [MINOR] Native auth: Add claimsRequest also to getAccessToken and signIn after signUp/SSPR flows (#2622)
1416

1517
Version 20.1.1
1618
----------

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/WebViewAuthorizationFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
import static com.microsoft.identity.common.java.AuthenticationConstants.SdkPlatformFields.PRODUCT;
8686
import static com.microsoft.identity.common.java.AuthenticationConstants.SdkPlatformFields.VERSION;
8787

88+
import io.opentelemetry.api.trace.SpanContext;
89+
8890
/**
8991
* Authorization fragment with embedded webview.
9092
*/
@@ -536,7 +538,8 @@ public void setPKeyAuthStatus(final boolean status) {
536538

537539
private SwitchBrowserProtocolCoordinator getSwitchBrowserCoordinator() {
538540
if (mSwitchBrowserProtocolCoordinator == null) {
539-
mSwitchBrowserProtocolCoordinator = new SwitchBrowserProtocolCoordinator(requireActivity());
541+
final SpanContext spanContext = requireActivity() instanceof AuthorizationActivity ? ((AuthorizationActivity) requireActivity()).getSpanContext() : null;
542+
mSwitchBrowserProtocolCoordinator = new SwitchBrowserProtocolCoordinator(requireActivity(), spanContext);
540543
}
541544
return mSwitchBrowserProtocolCoordinator;
542545
}

common/src/main/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserRequestHandler.kt

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ import com.microsoft.identity.common.internal.ui.browser.CustomTabsManager
3131
import com.microsoft.identity.common.internal.ui.webview.switchbrowser.SwitchBrowserUriHelper.isSwitchBrowserRedirectUrl
3232
import com.microsoft.identity.common.java.browser.IBrowserSelector
3333
import com.microsoft.identity.common.java.exception.ClientException
34+
import com.microsoft.identity.common.java.opentelemetry.AttributeName
35+
import com.microsoft.identity.common.java.opentelemetry.OTelUtility
36+
import com.microsoft.identity.common.java.opentelemetry.SpanExtension
37+
import com.microsoft.identity.common.java.opentelemetry.SpanName
3438
import com.microsoft.identity.common.java.ui.BrowserDescriptor
3539
import com.microsoft.identity.common.logging.Logger
40+
import io.opentelemetry.api.trace.Span
41+
import io.opentelemetry.api.trace.SpanContext
42+
import io.opentelemetry.api.trace.StatusCode
3643

3744
/**
3845
* SwitchBrowserRequestHandler is a challenge handler for SwitchBrowserChallenge.
@@ -42,9 +49,13 @@ class SwitchBrowserRequestHandler(
4249
private val activity: Activity,
4350
private val context: Context,
4451
private val customTabsManager: CustomTabsManager,
45-
private val browserSelector: IBrowserSelector
52+
private val browserSelector: IBrowserSelector,
53+
private val spanContext: SpanContext?
4654
) : IChallengeHandler<SwitchBrowserChallenge, Unit> {
4755

56+
val span: Span by lazy {
57+
OTelUtility.createSpanFromParent(SpanName.SwitchBrowserProcess.name, spanContext)
58+
}
4859

4960
var isChallengeHandled: Boolean = false
5061
private set
@@ -53,11 +64,12 @@ class SwitchBrowserRequestHandler(
5364
private val TAG = SwitchBrowserRequestHandler::class.simpleName
5465
}
5566

56-
constructor(activity: Activity) : this(
67+
constructor(activity: Activity, spanContext: SpanContext?) : this(
5768
activity,
5869
activity.applicationContext,
5970
CustomTabsManager(activity.applicationContext),
60-
AndroidBrowserSelector(activity.applicationContext)
71+
AndroidBrowserSelector(activity.applicationContext),
72+
spanContext
6173
)
6274

6375
/**
@@ -69,49 +81,68 @@ class SwitchBrowserRequestHandler(
6981
*/
7082
@Throws(ClientException::class)
7183
override fun processChallenge(switchBrowserChallenge: SwitchBrowserChallenge) {
72-
val methodTag = "$TAG:processChallenge"
84+
SpanExtension.makeCurrentSpan(span).use {
85+
val methodTag = "$TAG:processChallenge"
7386

74-
// Select a browser to handle the switch browser challenge
75-
val browser = browserSelector.selectBrowser(
76-
BrowserDescriptor.getBrowserSafeListForSwitchBrowser(),
77-
null
78-
)
79-
if (browser == null) {
80-
val exception = ClientException(
81-
ClientException.NO_BROWSERS_AVAILABLE,
82-
"No browser found for SwitchBrowserChallenge."
83-
)
84-
Logger.error(
85-
methodTag,
86-
"No browser found for SwitchBrowserChallenge.",
87-
exception
87+
// Select a browser to handle the switch browser challenge
88+
val browser = browserSelector.selectBrowser(
89+
BrowserDescriptor.getBrowserSafeListForSwitchBrowser(),
90+
null
8891
)
89-
throw exception
90-
}
92+
if (browser == null) {
93+
val exception = ClientException(
94+
ClientException.NO_BROWSERS_AVAILABLE,
95+
"No browser found for SwitchBrowserChallenge."
96+
)
97+
Logger.error(
98+
methodTag,
99+
"No browser found for SwitchBrowserChallenge.",
100+
exception
101+
)
102+
span.setStatus(StatusCode.ERROR)
103+
span.recordException(exception)
104+
span.end()
105+
throw exception
106+
}
91107

92-
// Create an intent to launch the browser
93-
val browserIntent: Intent
94-
if (browser.isCustomTabsServiceSupported) {
95-
Logger.info(methodTag, "CustomTabsService is supported.")
96-
//create customTabsIntent
97-
if (!customTabsManager.bind(context, browser.packageName)) {
98-
Logger.warn(methodTag, "Failed to bind CustomTabsService.")
99-
browserIntent = Intent(Intent.ACTION_VIEW)
108+
// Create an intent to launch the browser
109+
val browserIntent: Intent
110+
if (browser.isCustomTabsServiceSupported) {
111+
Logger.info(methodTag, "CustomTabsService is supported.")
112+
//create customTabsIntent
113+
if (!customTabsManager.bind(context, browser.packageName)) {
114+
Logger.warn(methodTag, "Failed to bind CustomTabsService.")
115+
browserIntent = Intent(Intent.ACTION_VIEW)
116+
} else {
117+
browserIntent = customTabsManager.customTabsIntent.intent
118+
}
100119
} else {
101-
browserIntent = customTabsManager.customTabsIntent.intent
120+
Logger.warn(methodTag, "CustomTabsService is NOT supported")
121+
browserIntent = Intent(Intent.ACTION_VIEW)
102122
}
103-
} else {
104-
Logger.warn(methodTag, "CustomTabsService is NOT supported")
105-
browserIntent = Intent(Intent.ACTION_VIEW)
123+
Logger.info(
124+
methodTag,
125+
"Launching switch browser request on browser: ${browser.packageName}"
126+
)
127+
browserIntent.setPackage(browser.packageName)
128+
browserIntent.setData(switchBrowserChallenge.uri)
129+
activity.startActivity(browserIntent)
130+
isChallengeHandled = true
131+
span.setAttribute(
132+
AttributeName.is_switch_browser_request_handled.name,
133+
isChallengeHandled
134+
)
135+
span.setAttribute(
136+
AttributeName.browser_package_name.name,
137+
browser.packageName
138+
)
139+
span.setAttribute(
140+
AttributeName.is_custom_tabs_supported.name,
141+
browser.isCustomTabsServiceSupported
142+
)
143+
span.setStatus(StatusCode.OK)
144+
span.end()
106145
}
107-
Logger.info(
108-
methodTag,
109-
"Launching switch browser request on browser: ${browser.packageName}"
110-
)
111-
browserIntent.setPackage(browser.packageName)
112-
browserIntent.setData(switchBrowserChallenge.uri)
113-
activity.startActivity(browserIntent)
114-
isChallengeHandled = true
115146
}
116147

117148
/**

common/src/main/java/com/microsoft/identity/common/internal/ui/webview/switchbrowser/SwitchBrowserProtocolCoordinator.kt

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,29 @@ import com.microsoft.identity.common.internal.ui.webview.switchbrowser.SwitchBro
3535
import com.microsoft.identity.common.internal.ui.webview.switchbrowser.SwitchBrowserUriHelper.isSwitchBrowserRedirectUrl
3636
import com.microsoft.identity.common.java.AuthenticationConstants.AAD.AUTHORIZATION
3737
import com.microsoft.identity.common.java.exception.ClientException
38+
import com.microsoft.identity.common.java.opentelemetry.AttributeName
39+
import com.microsoft.identity.common.java.opentelemetry.OTelUtility
40+
import com.microsoft.identity.common.java.opentelemetry.SpanExtension
41+
import com.microsoft.identity.common.java.opentelemetry.SpanName
3842
import com.microsoft.identity.common.java.ui.AuthorizationAgent
3943
import com.microsoft.identity.common.logging.Logger
44+
import io.opentelemetry.api.trace.Span
45+
import io.opentelemetry.api.trace.SpanContext
46+
import io.opentelemetry.api.trace.StatusCode
4047

4148
/**
4249
* SwitchBrowserProtocolCoordinator is responsible for coordinating the switch browser protocol.
4350
* Contains the handler to process the switch browser request and resume action.
4451
*/
4552
class SwitchBrowserProtocolCoordinator(
46-
val switchBrowserRequestHandler: SwitchBrowserRequestHandler) {
47-
constructor(activity: Activity) : this(SwitchBrowserRequestHandler(activity))
53+
val switchBrowserRequestHandler: SwitchBrowserRequestHandler,
54+
private val spanContext: SpanContext? = null) {
55+
56+
constructor(activity: Activity, spanContext: SpanContext?) : this(SwitchBrowserRequestHandler(activity, spanContext), spanContext)
57+
58+
val span: Span by lazy {
59+
OTelUtility.createSpanFromParent(SpanName.SwitchBrowserResume.name, spanContext)
60+
}
4861

4962
companion object {
5063
private const val TAG = "SwitchBrowserProtocolCoordinator"
@@ -99,21 +112,30 @@ class SwitchBrowserProtocolCoordinator(
99112
extras: Bundle,
100113
onSuccessAction: (Uri, HashMap<String, String>) -> Unit
101114
) {
102-
val methodTag = "$TAG:processSwitchBrowserResume"
103-
val actionUri = extras.getString(SWITCH_BROWSER.ACTION_URI)
104-
val code = extras.getString(SWITCH_BROWSER.CODE)
105-
if (actionUri.isNullOrEmpty() || code.isNullOrEmpty()) {
106-
throw ClientException(
107-
ClientException.MISSING_PARAMETER,
108-
"Action URI is null/empty: ${actionUri == null}, code is null/empty: ${code == null}"
109-
)
115+
SpanExtension.makeCurrentSpan(span).use {
116+
val methodTag = "$TAG:processSwitchBrowserResume"
117+
val actionUri = extras.getString(SWITCH_BROWSER.ACTION_URI)
118+
val code = extras.getString(SWITCH_BROWSER.CODE)
119+
if (actionUri.isNullOrEmpty() || code.isNullOrEmpty()) {
120+
val clientException = ClientException(
121+
ClientException.MISSING_PARAMETER,
122+
"Action URI is null/empty: ${actionUri == null}, code is null/empty: ${code == null}"
123+
)
124+
span.setStatus(StatusCode.ERROR)
125+
span.recordException(clientException)
126+
span.end()
127+
throw clientException
128+
}
129+
val resumeUri = buildResumeUri(actionUri)
130+
val headers = hashMapOf(AUTHORIZATION to code)
131+
onSuccessAction(resumeUri, headers)
132+
// Reset the challenge state after processing the resume action
133+
switchBrowserRequestHandler.resetChallengeState()
134+
Logger.info(methodTag, "Switch browser resume action processed successfully.")
135+
span.setAttribute(AttributeName.is_switch_browser_resume_handled.name, true)
136+
span.setStatus(StatusCode.OK)
137+
span.end()
110138
}
111-
val resumeUri = buildResumeUri(actionUri)
112-
val headers = hashMapOf(AUTHORIZATION to code)
113-
onSuccessAction(resumeUri, headers)
114-
// Reset the challenge state after processing the resume action
115-
switchBrowserRequestHandler.resetChallengeState()
116-
Logger.info(methodTag, "Switch browser resume action processed successfully.")
117139
}
118140

119141
/**

common/src/test/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserRequestHandlerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SwitchBrowserRequestHandlerTest {
6060
`when`(challenge.uri).thenReturn(Uri.parse("https://example.com"))
6161
val browserSelector = // Browser available
6262
IBrowserSelector { _, _ -> Browser("fakeBrowser", emptySet(), "browser", false) }
63-
val handler = SwitchBrowserRequestHandler(mockActivity, context, customTabsManager, browserSelector)
63+
val handler = SwitchBrowserRequestHandler(mockActivity, context, customTabsManager, browserSelector, null)
6464
handler.processChallenge(challenge)
6565
Assert.assertTrue(activityExecuted)
6666
}
@@ -75,7 +75,7 @@ class SwitchBrowserRequestHandlerTest {
7575
val challenge = mock(SwitchBrowserChallenge::class.java)
7676
`when`(challenge.uri).thenReturn(Uri.parse("https://example.com"))
7777
val browserSelector = IBrowserSelector { _, _ -> null } // No browser available
78-
val handler = SwitchBrowserRequestHandler(activity, context, customTabsManager, browserSelector)
78+
val handler = SwitchBrowserRequestHandler(activity, context, customTabsManager, browserSelector, null)
7979
val exception = Assert.assertThrows(ClientException::class.java) {
8080
handler.processChallenge(challenge)
8181
}

common4j/src/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/BaseSignInTokenCommandParameters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import java.util.List;
2828

29+
import javax.annotation.Nullable;
30+
2931
import lombok.EqualsAndHashCode;
3032
import lombok.Getter;
3133
import lombok.experimental.SuperBuilder;
@@ -44,5 +46,11 @@ public abstract class BaseSignInTokenCommandParameters extends BaseNativeAuthCom
4446
*/
4547
public final List<String> scopes;
4648

49+
/**
50+
* Claims to send to the token endpoint.
51+
*/
52+
@Nullable
53+
public final String claimsRequestJson;
54+
4755
private final AbstractAuthenticationScheme authenticationScheme;
4856
}

common4j/src/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/MFADefaultChallengeCommandParameters.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ public class MFADefaultChallengeCommandParameters extends BaseSignInTokenCommand
4747
@NonNull
4848
public final String continuationToken;
4949

50-
/**
51-
* Claims to send to the token endpoint.
52-
*/
53-
@Nullable
54-
public final String claimsRequestJson;
55-
5650
@NonNull
5751
@Override
5852
public String toUnsanitizedString() {

common4j/src/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/MFASubmitChallengeCommandParameters.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ public class MFASubmitChallengeCommandParameters extends BaseSignInTokenCommandP
5151
@NonNull
5252
public final String continuationToken;
5353

54-
/**
55-
* Claims to send to the token endpoint.
56-
*/
57-
@Nullable
58-
public final String claimsRequestJson;
59-
6054
@NonNull
6155
@Override
6256
public String toUnsanitizedString() {

common4j/src/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/SignInStartCommandParameters.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ public class SignInStartCommandParameters extends BaseSignInTokenCommandParamete
5454
@Nullable
5555
public final char[] password;
5656

57-
/**
58-
* Claims to send to the token endpoint.
59-
*/
60-
@Nullable
61-
public final String claimsRequestJson;
62-
6357
@NonNull
6458
@Override
6559
public String toUnsanitizedString() {

common4j/src/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/SignInSubmitCodeCommandParameters.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ public class SignInSubmitCodeCommandParameters extends BaseSignInTokenCommandPar
5151
@NonNull
5252
public final String continuationToken;
5353

54-
/**
55-
* Claims to send to the token endpoint.
56-
*/
57-
@Nullable
58-
public final String claimsRequestJson;
59-
6054
@NonNull
6155
@Override
6256
public String toUnsanitizedString() {

0 commit comments

Comments
 (0)