15
15
16
16
class OSBrokerCredential implements TokenCredential {
17
17
private static final ClientLogger LOGGER = new ClientLogger (OSBrokerCredential .class );
18
- private static final String BROKER_BUILDER_CLASS
19
- = "com.azure.identity.broker.InteractiveBrowserBrokerCredentialBuilder" ;
18
+ private static final String BROKER_BUILDER_CLASS = "com.azure.identity.broker.InteractiveBrowserBrokerCredentialBuilder" ;
20
19
private final String tenantId ;
21
20
private final AtomicReference <TokenCredential > cached = new AtomicReference <>();
22
21
@@ -28,7 +27,18 @@ class OSBrokerCredential implements TokenCredential {
28
27
public Mono <AccessToken > getToken (TokenRequestContext request ) {
29
28
TokenCredential credential = cached .get ();
30
29
if (credential == null ) {
31
- TokenCredential newCredential = createBrokerCredential ();
30
+ TokenCredential newCredential ;
31
+ try {
32
+ // Create the broker credential dynamically
33
+ newCredential = createBrokerCredential ();
34
+ } catch (CredentialUnavailableException e ) {
35
+ // If the broker is unavailable, throw the exception
36
+ return Mono .error (e );
37
+ } catch (Exception e ) {
38
+ // Log and throw any other exceptions that occur during credential creation
39
+ return Mono .error (LOGGER .logExceptionAsError (
40
+ new CredentialUnavailableException ("Failed to create OS Broker credential." , e )));
41
+ }
32
42
if (cached .compareAndSet (null , newCredential )) {
33
43
credential = newCredential ;
34
44
} else {
@@ -39,12 +49,11 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
39
49
}
40
50
41
51
private TokenCredential createBrokerCredential () {
42
- final String troubleshoot
43
- = " To mitigate this issue, refer to http://aka.ms/azsdk/java/identity/dacbrokerauth/troubleshoot" ;
52
+ final String troubleshoot = " To mitigate this issue, refer to http://aka.ms/azsdk/java/identity/dacbrokerauth/troubleshoot" ;
44
53
if (!IdentityUtil .isBrokerAvailable ()) {
45
54
throw LOGGER .logExceptionAsError (
46
55
new CredentialUnavailableException ("azure-identity-broker dependency is not available. "
47
- + "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot ));
56
+ + "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot ));
48
57
}
49
58
try {
50
59
Class <?> builderClass = Class .forName (BROKER_BUILDER_CLASS );
@@ -58,14 +67,12 @@ private TokenCredential createBrokerCredential() {
58
67
}
59
68
return browserCredentialBuilder .build ();
60
69
} catch (ClassNotFoundException e ) {
61
- throw LOGGER
62
- .logExceptionAsError (new CredentialUnavailableException (
63
- "InteractiveBrowserBrokerCredentialBuilder class not found. "
64
- + "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot ,
65
- e ));
70
+ throw LOGGER .logExceptionAsError (
71
+ new CredentialUnavailableException ("InteractiveBrowserBrokerCredentialBuilder class not found. "
72
+ + "Ensure you have azure-identity-broker dependency added to your application." + troubleshoot , e ));
66
73
} catch (Exception e ) {
67
- throw LOGGER .logExceptionAsError (new CredentialUnavailableException (
68
- "Failed to create InteractiveBrowserBrokerCredential dynamically." + troubleshoot , e ));
74
+ throw LOGGER .logExceptionAsError (
75
+ new CredentialUnavailableException ( "Failed to create InteractiveBrowserBrokerCredential dynamically." + troubleshoot , e ));
69
76
}
70
77
}
71
78
}
0 commit comments