com.microsoft.aad.adal4j.AsymmetricKeyCredential Java Examples

The following examples show how to use com.microsoft.aad.adal4j.AsymmetricKeyCredential. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApplicationTokenCredentials.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
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);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    if (sslSocketFactory() != null) {
        context.setSslSocketFactory(sslSocketFactory());
    }
    try {
        if (clientSecret != null) {
            return context.acquireToken(
                    resource,
                    new ClientCredential(this.clientId(), clientSecret),
                    null).get();
        } else if (clientCertificate != null && clientCertificatePassword != null) {
            return context.acquireToken(
                    resource,
                    AsymmetricKeyCredential.create(clientId, new ByteArrayInputStream(clientCertificate), clientCertificatePassword),
                    null).get();
        } else if (clientCertificate != null) {
            return context.acquireToken(
                    resource,
                    AsymmetricKeyCredential.create(clientId(), privateKeyFromPem(new String(clientCertificate)), publicKeyFromPem(new String(clientCertificate))),
                    null).get();
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        executor.shutdown();
    }
}
 
Example #2
Source File: DelegatedTokenCredentials.java    From autorest-clientruntime-for-java with MIT License 4 votes vote down vote up
AuthenticationResult acquireNewAccessToken(String resource) throws IOException {
    if (authorizationCode == null) {
        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);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    try {
        if (applicationCredentials.clientSecret() != null) {
            return context.acquireTokenByAuthorizationCode(
                    authorizationCode,
                    new URI(redirectUrl),
                    new ClientCredential(applicationCredentials.clientId(), applicationCredentials.clientSecret()),
                    resource, null).get();
        } else if (applicationCredentials.clientCertificate() != null && applicationCredentials.clientCertificatePassword() != null) {
            return context.acquireTokenByAuthorizationCode(
                    authorizationCode,
                    new URI(redirectUrl),
                    AsymmetricKeyCredential.create(
                            applicationCredentials.clientId(),
                            new ByteArrayInputStream(applicationCredentials.clientCertificate()),
                            applicationCredentials.clientCertificatePassword()),
                    resource,
                    null).get();
        } else if (applicationCredentials.clientCertificate() != null) {
            return context.acquireTokenByAuthorizationCode(
                    authorizationCode,
                    new URI(redirectUrl),
                    AsymmetricKeyCredential.create(
                            clientId(),
                            ApplicationTokenCredentials.privateKeyFromPem(new String(applicationCredentials.clientCertificate())),
                            ApplicationTokenCredentials.publicKeyFromPem(new String(applicationCredentials.clientCertificate()))),
                    resource,
                    null).get();
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        executor.shutdown();
    }
}
 
Example #3
Source File: ServicePrincipalWithClientCertificate.java    From azure-sdk-for-media-services-java-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	ExecutorService executorService = Executors.newFixedThreadPool(1);

    try {
        String tenant = "tenant.domain.com";
        String clientId = "%client_id%";
        String restApiEndpoint = "https://account.restv2.region.media.azure.net/api/";
        String pfxFilename = "%path_to_keystore.pfx%";
        String pfxPassword = "%keystore_password%";
        InputStream pfx = new FileInputStream(pfxFilename);

        // Connect to Media Services API with service principal and client certificate
        AzureAdTokenCredentials credentials = new AzureAdTokenCredentials(
                tenant,
                AsymmetricKeyCredential.create(clientId, pfx, pfxPassword),
                AzureEnvironments.AZURE_CLOUD_ENVIRONMENT);

        TokenProvider provider = new AzureAdTokenProvider(credentials, executorService);

        // create a new configuration with the new credentials
        Configuration configuration = MediaConfiguration.configureWithAzureAdTokenProvider(
                new URI(restApiEndpoint),
                provider);

        // create the media service with the new configuration
        MediaContract mediaService = MediaService.create(configuration);

        System.out.println("Listing assets");

        ListResult<AssetInfo> assets = mediaService.list(Asset.list());

        for (AssetInfo asset : assets) {
            System.out.println(asset.getId());
        }

    } catch (ServiceException se) {
        System.out.println("ServiceException encountered.");
        System.out.println(se.toString());
    } catch (Throwable e) {
        System.out.println("Exception encountered.");
        e.printStackTrace();
        System.out.println(e.toString());
    } finally {
        executorService.shutdown();
    }
}