use of com.microsoft.aad.adal4j.AuthenticationException in project autorest-clientruntime-for-java by Azure.
the class DelegatedTokenCredentials method acquireNewAccessToken.
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();
}
}
use of com.microsoft.aad.adal4j.AuthenticationException in project autorest-clientruntime-for-java by Azure.
the class ApplicationTokenCredentials method acquireAccessToken.
Future<AuthenticationResult> acquireAccessToken(String resource, ExecutorService executor) throws IOException {
String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain();
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(clientId(), clientSecret()), null);
} else if (clientCertificate() != null && clientCertificatePassword() != null) {
return context.acquireToken(resource, AsymmetricKeyCredential.create(clientId(), new ByteArrayInputStream(clientCertificate()), clientCertificatePassword()), null);
} else if (clientCertificate() != null) {
return context.acquireToken(resource, AsymmetricKeyCredential.create(clientId(), privateKeyFromPem(new String(clientCertificate())), publicKeyFromPem(new String(clientCertificate()))), null);
}
throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}
use of com.microsoft.aad.adal4j.AuthenticationException in project autorest-clientruntime-for-java by Azure.
the class ApplicationTokenCredentials method acquireAccessToken.
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();
}
}
use of com.microsoft.aad.adal4j.AuthenticationException in project mssql-jdbc by Microsoft.
the class SQLServerADAL4JUtils method getSqlFedAuthTokenIntegrated.
static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo, String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newFixedThreadPool(1);
try {
// principal name does not matter, what matters is the realm name
// it gets the username in principal_name@realm_name format
KerberosPrincipal kerberosPrincipal = new KerberosPrincipal("username");
String username = kerberosPrincipal.getName();
if (adal4jLogger.isLoggable(Level.FINE)) {
adal4jLogger.fine(adal4jLogger.toString() + " realm name is:" + kerberosPrincipal.getRealm());
}
AuthenticationContext context = new AuthenticationContext(fedAuthInfo.stsurl, false, executorService);
Future<AuthenticationResult> future = context.acquireToken(fedAuthInfo.spn, ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID, username, null, null);
AuthenticationResult authenticationResult = future.get();
SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
return fedAuthToken;
} catch (InterruptedException | IOException e) {
throw new SQLServerException(e.getMessage(), e);
} catch (ExecutionException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
Object[] msgArgs = { "", authenticationString };
if (null == e.getCause() || null == e.getCause().getMessage()) {
// the case when Future's outcome has no AuthenticationResult but exception
throw new SQLServerException(form.format(msgArgs), null);
} else {
// the cause error message uses \\n\\r which does not give correct format
// change it to \r\n to provide correct format
String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
AuthenticationException correctedAuthenticationException = new AuthenticationException(correctedErrorMessage);
// SQLServerException is caused by ExecutionException, which is caused by
// AuthenticationException
// to match the exception tree before error message correction
ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException);
throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
}
} finally {
executorService.shutdown();
}
}
use of com.microsoft.aad.adal4j.AuthenticationException in project mssql-jdbc by Microsoft.
the class SQLServerADAL4JUtils method getSqlFedAuthToken.
static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String user, String password, String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newFixedThreadPool(1);
try {
AuthenticationContext context = new AuthenticationContext(fedAuthInfo.stsurl, false, executorService);
Future<AuthenticationResult> future = context.acquireToken(fedAuthInfo.spn, ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID, user, password, null);
AuthenticationResult authenticationResult = future.get();
SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
return fedAuthToken;
} catch (MalformedURLException | InterruptedException e) {
throw new SQLServerException(e.getMessage(), e);
} catch (ExecutionException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
Object[] msgArgs = { user, authenticationString };
// the cause error message uses \\n\\r which does not give correct format
// change it to \r\n to provide correct format
String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
AuthenticationException correctedAuthenticationException = new AuthenticationException(correctedErrorMessage);
// SQLServerException is caused by ExecutionException, which is caused by
// AuthenticationException
// to match the exception tree before error message correction
ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException);
throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
} finally {
executorService.shutdown();
}
}
Aggregations