1
2
3
4 package com.azure.identity.credential;
5
6 import com.azure.core.credentials.AccessToken;
7 import com.azure.core.credentials.TokenCredential;
8 import com.azure.core.util.configuration.BaseConfigurations;
9 import com.azure.core.util.configuration.Configuration;
10 import com.azure.core.util.configuration.ConfigurationManager;
11 import com.azure.identity.IdentityClient;
12 import com.azure.identity.IdentityClientOptions;
13 import reactor.core.publisher.Mono;
14
15
16
17
18 public final class ManagedIdentityCredential implements TokenCredential {
19 private final AppServiceMSICredential appServiceMSICredential;
20 private final VirtualMachineMSICredential virtualMachineMSICredential;
21
22
23
24
25 public ManagedIdentityCredential() {
26 this(new IdentityClientOptions());
27 }
28
29
30
31
32
33 public ManagedIdentityCredential(IdentityClientOptions identityClientOptions) {
34 IdentityClientityClient">IdentityClient identityClient = new IdentityClient(identityClientOptions);
35 Configuration configuration = ConfigurationManager.getConfiguration();
36 if (configuration.contains(BaseConfigurations.MSI_ENDPOINT)) {
37 appServiceMSICredential = new AppServiceMSICredential(identityClient);
38 virtualMachineMSICredential = null;
39 } else {
40 virtualMachineMSICredential = new VirtualMachineMSICredential(identityClient);
41 appServiceMSICredential = null;
42 }
43 }
44
45
46
47
48 public String clientId() {
49 return this.appServiceMSICredential != null ? this.appServiceMSICredential.clientId() : this.virtualMachineMSICredential.clientId();
50 }
51
52
53
54
55
56
57
58 public ManagedIdentityCredential clientId(String clientId) {
59 if (this.appServiceMSICredential != null) {
60 this.appServiceMSICredential.clientId(clientId);
61 } else {
62 this.virtualMachineMSICredential.clientId(clientId);
63 }
64 return this;
65 }
66
67
68
69
70 public String msiEndpoint() {
71 return this.appServiceMSICredential == null ? null : this.appServiceMSICredential.msiEndpoint();
72 }
73
74
75
76 public String msiSecret() {
77 return this.appServiceMSICredential == null ? null : this.appServiceMSICredential.msiSecret();
78 }
79
80 @Override
81 public Mono<AccessToken> getToken(String... scopes) {
82 return (appServiceMSICredential != null
83 ? appServiceMSICredential.authenticate(scopes)
84 : virtualMachineMSICredential.authenticate(scopes));
85 }
86 }