View Javadoc
1   // Copyright (c) Microsoft Corporation. All rights reserved.
2   // Licensed under the MIT License.
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   * The base class for Managed Service Identity token based credentials.
17   */
18  public final class ManagedIdentityCredential implements TokenCredential {
19      private final AppServiceMSICredential appServiceMSICredential;
20      private final VirtualMachineMSICredential virtualMachineMSICredential;
21  
22      /**
23       * Creates an instance of the ManagedIdentityCredential with default identity client options.
24       */
25      public ManagedIdentityCredential() {
26          this(new IdentityClientOptions());
27      }
28  
29      /**
30       * Creates an instance of the ManagedIdentityCredential.
31       * @param identityClientOptions the options for configuring the identity client.
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       * @return the client id of user assigned or system assigned identity.
47       */
48      public String clientId() {
49          return this.appServiceMSICredential != null ? this.appServiceMSICredential.clientId() : this.virtualMachineMSICredential.clientId();
50      }
51  
52      /**
53       * Specifies the client id of user assigned or system assigned identity.
54       *
55       * @param clientId the client id
56       * @return ManagedIdentityCredential
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       * @return the endpoint from which token needs to be retrieved.
69       */
70      public String msiEndpoint() {
71          return this.appServiceMSICredential == null ? null : this.appServiceMSICredential.msiEndpoint();
72      }
73      /**
74       * @return the secret to use to retrieve the token.
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  }