/** * @return the URL to authenticate through OAuth2 */ public String generateAuthenticationUrl() { return String.format("%s/%s/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s&response_mode=query&state=%s", environment().activeDirectoryEndpoint(), domain(), clientId(), this.redirectUrl, UUID.randomUUID()); }
protected AuthContext createContext(@NotNull final String tid, final UUID corrId) throws IOException { String authority = null; final String endpoint = env.activeDirectoryEndpoint(); if (StringUtils.isNullOrEmpty(endpoint)) { throw new IOException("Azure authority endpoint is empty"); } if (endpoint.endsWith("/")) { authority = endpoint + tid; } else { authority = endpoint + "/" + tid; } return new AuthContext(authority, Constants.clientId, true, corrId); } }
void exportAuthFile(ServicePrincipalImpl servicePrincipal) { if (authFile == null) { return; } RestClient restClient = servicePrincipal.manager().roleInner().restClient(); AzureEnvironment environment = Utils.extractAzureEnvironment(restClient); StringBuilder builder = new StringBuilder("{\n"); builder.append(" ").append(String.format("\"clientId\": \"%s\",", servicePrincipal.applicationId())).append("\n"); builder.append(" ").append(String.format("\"clientSecret\": \"%s\",", value())).append("\n"); builder.append(" ").append(String.format("\"tenantId\": \"%s\",", servicePrincipal.manager().tenantId())).append("\n"); builder.append(" ").append(String.format("\"subscriptionId\": \"%s\",", servicePrincipal.assignedSubscription)).append("\n"); builder.append(" ").append(String.format("\"activeDirectoryEndpointUrl\": \"%s\",", environment.activeDirectoryEndpoint())).append("\n"); builder.append(" ").append(String.format("\"resourceManagerEndpointUrl\": \"%s\",", environment.resourceManagerEndpoint())).append("\n"); builder.append(" ").append(String.format("\"activeDirectoryGraphResourceId\": \"%s\",", environment.graphEndpoint())).append("\n"); builder.append(" ").append(String.format("\"managementEndpointUrl\": \"%s\"", environment.managementEndpoint())).append("\n"); builder.append("}"); try { authFile.write(builder.toString().getBytes()); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Initializes a new instance of the UserTokenCredentials. * * @param clientId the active directory application client id. * @param domain the domain or tenant id containing this application. * @param username the user name for the Organization Id account. * @param password the password for the Organization Id account. * @param environment the Azure environment to authenticate with. * If null is provided, AzureEnvironment.AZURE will be used. */ public UserTokenCredentials(String clientId, String domain, String username, String password, AzureEnvironment environment) { super(environment, domain); // defer token acquisition this.clientId = clientId; this.username = username; this.password = password; this.tokens = new ConcurrentHashMap<>(); this.refreshTokenClient = new RefreshTokenClient(environment.activeDirectoryEndpoint(), proxy()); }
void exportAuthFile(ServicePrincipalImpl servicePrincipal) { if (authFile == null) { return; } RestClient restClient = servicePrincipal.manager().roleInner().restClient(); AzureEnvironment environment = Utils.extractAzureEnvironment(restClient); StringBuilder builder = new StringBuilder("{\n"); builder.append(" ").append(String.format("\"clientId\": \"%s\",", servicePrincipal.applicationId())).append("\n"); builder.append(" ").append(String.format("\"clientCertificate\": \"%s\",", privateKeyPath.replace("\\", "\\\\"))).append("\n"); builder.append(" ").append(String.format("\"clientCertificatePassword\": \"%s\",", privateKeyPassword)).append("\n"); builder.append(" ").append(String.format("\"tenantId\": \"%s\",", servicePrincipal.manager().tenantId())).append("\n"); builder.append(" ").append(String.format("\"subscriptionId\": \"%s\",", servicePrincipal.assignedSubscription)).append("\n"); builder.append(" ").append(String.format("\"activeDirectoryEndpointUrl\": \"%s\",", environment.activeDirectoryEndpoint())).append("\n"); builder.append(" ").append(String.format("\"resourceManagerEndpointUrl\": \"%s\",", environment.resourceManagerEndpoint())).append("\n"); builder.append(" ").append(String.format("\"activeDirectoryGraphResourceId\": \"%s\",", environment.graphEndpoint())).append("\n"); builder.append(" ").append(String.format("\"managementEndpointUrl\": \"%s\"", environment.managementEndpoint())).append("\n"); builder.append("}"); try { authFile.write(builder.toString().getBytes()); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Generate the URL to authenticate through OAuth2. * * @param responseMode the method that should be used to send the resulting token back to your app * @param state a value included in the request that is also returned in the token response * @return the URL to authenticate through OAuth2 */ public String generateAuthenticationUrl(ResponseMode responseMode, String state) { return String.format("%s/%s/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s&response_mode=%s&state=%s", environment().activeDirectoryEndpoint(), domain(), clientId(), this.redirectUrl, responseMode.value, state); }
prop.setProperty("managementURI", azureEnv.managementEndpoint()); prop.setProperty("baseURL", azureEnv.resourceManagerEndpoint()); prop.setProperty("authURL", azureEnv.activeDirectoryEndpoint()); prop.setProperty("graphURL", azureEnv.graphEndpoint());
/** * Initializes a new instance of the DelegatedTokenCredentials. * * @param applicationCredentials the credentials representing a service principal * @param redirectUrl the URL to redirect to after authentication in Active Directory */ public DelegatedTokenCredentials(ApplicationTokenCredentials applicationCredentials, String redirectUrl) { super(applicationCredentials.environment(), applicationCredentials.domain()); // defer token acquisition this.applicationCredentials = applicationCredentials; this.tokens = new ConcurrentHashMap<>(); this.redirectUrl = redirectUrl; this.refreshTokenClient = new RefreshTokenClient(applicationCredentials.environment().activeDirectoryEndpoint(), proxy()); }
AuthenticationResult acquireNewAccessToken(String resource) throws IOException { String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain(); ExecutorService executor = Executors.newSingleThreadExecutor(); AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor); if (proxy() != null) { context.setProxy(proxy()); } try { return context.acquireToken( resource, this.clientId(), this.username(), this.password, null).get(); } catch (Exception e) { throw new IOException(e.getMessage(), e); } finally { executor.shutdown(); } }
authSettings.put(CredentialSettings.AUTH_URL.toString(), AzureEnvironment.AZURE.activeDirectoryEndpoint()); authSettings.put(CredentialSettings.BASE_URL.toString(), AzureEnvironment.AZURE.resourceManagerEndpoint()); authSettings.put(CredentialSettings.MANAGEMENT_URI.toString(), AzureEnvironment.AZURE.managementEndpoint());
private AuthenticationResult acquireAccessToken(String resource) throws IOException { String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain(); ExecutorService executor = Executors.newSingleThreadExecutor(); AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);
throw new IllegalArgumentException("You must acquire an authorization code by redirecting to the authentication URL"); String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain(); ExecutorService executor = Executors.newSingleThreadExecutor(); AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);