Java源码示例:com.microsoft.aad.adal4j.AsymmetricKeyCredential

示例1
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();
    }
}
 
示例2
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();
    }
}
 
示例3
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();
    }
}