return credentialInstance; if (isServicePrincipal()) { credentialInstance = new ApplicationTokenCredentials( clientId(), tenant(), servicePrincipalToken.accessToken(), environment() ); } else { credentialInstance = new UserTokenCredentials(clientId(), tenant(), null, null, environment()) { @Override public synchronized String getToken(String resource) throws IOException {
private synchronized void loadAccessTokens() throws IOException { try { AzureCliSubscription.Wrapper wrapper = MAPPER.readValue(azureProfile, AzureCliSubscription.Wrapper.class); List<AzureCliToken> tokens = MAPPER.readValue(accessTokens, new TypeReference<List<AzureCliToken>>() { }); while (wrapper == null || tokens == null || tokens.isEmpty() || wrapper.subscriptions == null || wrapper.subscriptions.isEmpty()) { System.err.println("Please login in Azure CLI and press any key to continue after you've successfully logged in."); System.in.read(); wrapper = MAPPER.readValue(azureProfile, AzureCliSubscription.Wrapper.class); tokens = MAPPER.readValue(accessTokens, new TypeReference<List<AzureCliToken>>() { }); } for (AzureCliSubscription subscription : wrapper.subscriptions) { for (AzureCliToken token : tokens) { // Find match of user and tenant if (subscription.isServicePrincipal() == token.isServicePrincipal() && subscription.userName().equalsIgnoreCase(token.user()) && subscription.tenant().equalsIgnoreCase(token.tenant())) { subscriptions.put(subscription.id(), subscription.withToken(token)); if (subscription.isDefault()) { withDefaultSubscriptionId(subscription.id()); } } } } } catch (IOException e) { System.err.println(String.format("Cannot read files %s and %s. Are you logged in Azure CLI?", azureProfile.getAbsolutePath(), accessTokens.getAbsolutePath())); throw e; } }
/** * @return the active directory application client id */ public String clientId() { return subscriptions.get(defaultSubscriptionId()).clientId(); }
/** * @return the Azure environment to authenticate with */ public AzureEnvironment environment() { return subscriptions.get(defaultSubscriptionId()).environment(); }
/** * @return the tenant or domain the containing the application */ @Override public String domain() { return subscriptions.get(defaultSubscriptionId()).tenant(); }
@Override public synchronized String getToken(String resource) throws IOException { String token = subscriptions.get(defaultSubscriptionId()).credentialInstance().getToken(resource); if (token == null) { System.err.println("Please login in Azure CLI and press any key to continue after you've successfully logged in."); System.in.read(); loadAccessTokens(); token = subscriptions.get(defaultSubscriptionId()).credentialInstance().getToken(resource); } return token; } }
AzureCliSubscription withToken(AzureCliToken token) { if (isServicePrincipal()) { this.servicePrincipalToken = token; } else { if (token.resource() != null) { this.userTokens.put(token.resource(), token); } if (this.clientId == null) { this.clientId = token.clientId(); } } return this; }