@Override public Future<AuthenticationResult> acquireToken(AuthenticationContext context, String resource) { return context.acquireToken(resource, credential, null); } }
@Override public Future<AuthenticationResult> acquireToken(AuthenticationContext context, String resource) { return context.acquireToken(resource, credential, null); } }
@NotNull private static AuthenticationResult acquireToken(@NotNull final AuthenticationContext context, @NotNull final AuthorizationTokenInputs inputs, @NotNull ClientCredential credential, @NotNull ExecutorService service) throws Exception { final Future<AuthenticationResult> future = context.acquireToken(inputs.getResource(), credential, null); service.shutdown(); return future.get(); }
@Override public CompletableFuture<SecurityToken> getSecurityTokenAsync(String audience) { String addAudienceForSB = SecurityConstants.SERVICEBUS_AAD_AUDIENCE_RESOURCE_URL; CompletableFuture<SecurityToken> tokenGeneratingFuture = new CompletableFuture<>(); switch(this.authenticationMode) { case CLIENT_CREDENTIAL: this.authenticationContext.acquireToken(addAudienceForSB, this.clientCredential, new FutureCompletingAuthenticationCallback(tokenGeneratingFuture, audience)); break; case USER_PASSWORD_CREDENTIAL: this.authenticationContext.acquireToken(addAudienceForSB, this.clientId, this.userName, this.password, new FutureCompletingAuthenticationCallback(tokenGeneratingFuture, audience)); break; case CERTIFICATE: this.authenticationContext.acquireToken(addAudienceForSB, this.asymmetricKeyCredential, new FutureCompletingAuthenticationCallback(tokenGeneratingFuture, audience)); break; } return tokenGeneratingFuture; }
@NotNull private static AuthenticationResult acquireToken(@NotNull final AuthenticationContext context, @NotNull final AuthorizationTokenInputs inputs, @NotNull ExecutorService service) throws Exception { final Future<AuthenticationResult> future = context.acquireToken(inputs.getResource(), inputs.getClientId(), inputs.getUsername(), inputs.getPassword(), null); service.shutdown(); return future.get(); } }
@Override public String doAuthenticate(String authorization, String resource, String scope) { if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set"); } try { if(Objects.isNull(authenticationContext)) { this.authenticationContext = new AuthenticationContext(authorization, false, executorService); } ClientCredential credential = new ClientCredential(clientId,clientSecret); return authenticationContext.acquireToken(resource, credential, null).get().getAccessToken(); } catch (ExecutionException | MalformedURLException ex) { throw new RuntimeException(ex); } catch(InterruptedException ex) { LOGGER.warn("Key vault executor executorService interrupted"); throw new RuntimeException(ex); } }
@Override public String doAuthenticate(String authorization, String resource, String scope) { AuthenticationContext context = null; AuthenticationResult result = null; String token = ""; final ExecutorService executorService = Executors.newSingleThreadExecutor(); try { context = new AuthenticationContext(authorization, false, executorService); final ClientCredential credential = new ClientCredential(this.clientId, this.clientKey); final Future<AuthenticationResult> future = context.acquireToken(resource, credential, null); result = future.get(timeoutInSeconds, TimeUnit.SECONDS); token = result.getAccessToken(); } catch (MalformedURLException | TimeoutException | InterruptedException | ExecutionException ex) { throw new IllegalStateException("Failed to do authentication.", ex); } finally { executorService.shutdown(); } return token; } }
@Override public String doAuthenticate(String authorization, String resource, String scope) { AuthenticationContext context = null; AuthenticationResult result = null; String token = ""; final ExecutorService executorService = Executors.newSingleThreadExecutor(); try { context = new AuthenticationContext(authorization, false, executorService); final ClientCredential credential = new ClientCredential(this.clientId, this.clientKey); final Future<AuthenticationResult> future = context.acquireToken(resource, credential, null); result = future.get(timeoutInSeconds, TimeUnit.SECONDS); token = result.getAccessToken(); } catch (MalformedURLException | TimeoutException | InterruptedException | ExecutionException ex) { throw new IllegalStateException("Failed to do authentication.", ex); } finally { executorService.shutdown(); } return token; } }
@Override public String doAuthenticate(String authorization, String resource, String scope) { if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set"); } try { if(Objects.isNull(authenticationContext)) { this.authenticationContext = new AuthenticationContext(authorization, false, executorService); } ClientCredential credential = new ClientCredential(clientId,clientSecret); return authenticationContext.acquireToken(resource, credential, null).get().getAccessToken(); } catch (ExecutionException | MalformedURLException ex) { throw new RuntimeException(ex); } catch(InterruptedException ex) { LOGGER.warn("Key vault executor executorService interrupted"); throw new RuntimeException(ex); } }
/** * Acquires security token from the authority. * * @param resource * Identifier of the target resource that is the recipient of the * requested token. * @param credential * object representing Private Key to use for token acquisition. * @param callback * optional callback object for non-blocking execution. * @return A {@link Future} object representing the * {@link AuthenticationResult} of the call. It contains Access * Token and the Access Token's expiration time. Refresh Token * property will be null for this overload. * @throws AuthenticationException {@link AuthenticationException} */ public Future<AuthenticationResult> acquireToken(final String resource, final AsymmetricKeyCredential credential, final AuthenticationCallback callback) throws AuthenticationException { return this.acquireToken(resource, JwtHelper.buildJwt(credential, this.authenticationAuthority.getSelfSignedJwtAudience()), callback); }
public Future<AuthenticationResult> get(AuthenticationContext context, String resource) { return context .acquireToken(resource, azureLoginConfiguration.getClientId(), credentialDTO.getUsername(), credentialDTO.getPassword(), null); } }
@Override public String doAuthenticate(String authorization, String resource, String scope) { final String certFileName = certResource.getFilename(); final KeyCertReader certReader = KeyCertReaderFactory.getReader(certFileName); final KeyCert keyCert = certReader.read(certResource, certPassword); try { final AuthenticationContext context = new AuthenticationContext(authorization, false, Executors.newSingleThreadExecutor()); final AsymmetricKeyCredential asymmetricKeyCredential = AsymmetricKeyCredential.create(clientId, keyCert.getKey(), keyCert.getCertificate()); final AuthenticationResult authResult = context.acquireToken(resource, asymmetricKeyCredential, null) .get(timeoutInSeconds, TimeUnit.SECONDS); return authResult.getAccessToken(); } catch (MalformedURLException | InterruptedException | ExecutionException | TimeoutException e) { final String errMsg = String.format("Failed to authenticate with Key Vault using certificate %s", certFileName); log.error(errMsg, e); throw new IllegalStateException(errMsg, e); } } }
@Override public String doAuthenticate(String authorization, String resource, String scope) { final String certFileName = certResource.getFilename(); final KeyCertReader certReader = KeyCertReaderFactory.getReader(certFileName); final KeyCert keyCert = certReader.read(certResource, certPassword); try { final AuthenticationContext context = new AuthenticationContext(authorization, false, Executors.newSingleThreadExecutor()); final AsymmetricKeyCredential asymmetricKeyCredential = AsymmetricKeyCredential.create(clientId, keyCert.getKey(), keyCert.getCertificate()); final AuthenticationResult authResult = context.acquireToken(resource, asymmetricKeyCredential, null) .get(timeoutInSeconds, TimeUnit.SECONDS); return authResult.getAccessToken(); } catch (MalformedURLException | InterruptedException | ExecutionException | TimeoutException e) { final String errMsg = String.format("Failed to authenticate with Key Vault using certificate %s", certFileName); log.error(errMsg, e); throw new IllegalStateException(errMsg, e); } } }
private Future<AuthenticationResult> acquireTokenOnBehalfOf(final String resource, final UserAssertion userAssertion, final ClientAuthentication clientAuthentication, final AuthenticationCallback callback) { Map<String, String> params = new HashMap<String, String>(); params.put("resource", resource); params.put("requested_token_use", "on_behalf_of"); try { AdalOAuthAuthorizationGrant grant = new AdalOAuthAuthorizationGrant( new JWTBearerGrant(SignedJWT.parse(userAssertion.getAssertion())), params); return this.acquireToken(grant, clientAuthentication, callback); } catch (final Exception e) { throw new AuthenticationException(e); } }
private Future<AuthenticationResult> acquireTokenOnBehalfOf(final String resource, final UserAssertion userAssertion, final ClientAuthentication clientAuthentication, final AuthenticationCallback callback) { Map<String, String> params = new HashMap<String, String>(); params.put("resource", resource); params.put("requested_token_use", "on_behalf_of"); try { AdalOAuthAuthorizationGrant grant = new AdalOAuthAuthorizationGrant( new JWTBearerGrant(SignedJWT.parse(userAssertion.getAssertion())), params); return this.acquireToken(grant, clientAuthentication, callback); } catch (final Exception e) { throw new AuthenticationException(e); } }
@NotNull public static AuthenticationResult getToken(@NotNull final AuthorizationTokenInputs inputs) throws Exception { final ExecutorService service = Executors.newSingleThreadExecutor(); final AuthenticationContext context = new AuthenticationContext(inputs.getAuthority(), false, service); context.setProxy(getProxy(inputs.getProxyHost(), inputs.getProxyPort(), inputs.getProxyUsername(), inputs.getProxyPassword())); final Future<AuthenticationResult> future = context.acquireToken(inputs.getResource(), inputs.getClientId(), inputs.getUsername(), inputs.getPassword(), null); service.shutdown(); return future.get(); } }
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(); } }
public AuthenticationResult acquireTokenForGraphApi(String idToken, String tenantId) throws MalformedURLException, ServiceUnavailableException, InterruptedException, ExecutionException { final ClientCredential credential = new ClientCredential(clientId, clientSecret); final UserAssertion assertion = new UserAssertion(idToken); AuthenticationResult result = null; ExecutorService service = null; try { service = Executors.newFixedThreadPool(1); final AuthenticationContext context = new AuthenticationContext( serviceEndpoints.getAadSigninUri() + tenantId + "/", true, service); context.setCorrelationId(getCorrelationId()); final Future<AuthenticationResult> future = context .acquireToken(serviceEndpoints.getAadGraphApiUri(), assertion, credential, null); result = future.get(); } finally { if (service != null) { service.shutdown(); } } if (result == null) { throw new ServiceUnavailableException("unable to acquire on-behalf-of token for client " + clientId); } return result; }
/** * Acquires security token from the authority using an device code previously received. * * @param deviceCode The device code result received from calling acquireDeviceCode. * @param callback optional callback object for non-blocking execution. * @return A {@link Future} object representing the {@link AuthenticationResult} of the call. * It contains AccessToken, Refresh Token and the Access Token's expiration time. * @throws AuthenticationException thrown if authorization is pending or another error occurred. * If the errorCode of the exception is AdalErrorCode.AUTHORIZATION_PENDING, * the call needs to be retried until the AccessToken is returned. * DeviceCode.interval - The minimum amount of time in seconds that the client * SHOULD wait between polling requests to the token endpoin */ public Future<AuthenticationResult> acquireTokenByDeviceCode( final DeviceCode deviceCode, final AuthenticationCallback callback) throws AuthenticationException { final ClientAuthentication clientAuth = new ClientAuthenticationPost( ClientAuthenticationMethod.NONE, new ClientID(deviceCode.getClientId())); this.validateDeviceCodeRequestInput(deviceCode, clientAuth, deviceCode.getResource()); final AdalDeviceCodeAuthorizationGrant deviceCodeGrant = new AdalDeviceCodeAuthorizationGrant(deviceCode, deviceCode.getResource()); return this.acquireToken(deviceCodeGrant, clientAuth, callback); }
/** * Acquires security token from the authority using an device code previously received. * * @param deviceCode The device code result received from calling acquireDeviceCode. * @param callback optional callback object for non-blocking execution. * @return A {@link Future} object representing the {@link AuthenticationResult} of the call. * It contains AccessToken, Refresh Token and the Access Token's expiration time. * @throws AuthenticationException thrown if authorization is pending or another error occurred. * If the errorCode of the exception is AdalErrorCode.AUTHORIZATION_PENDING, * the call needs to be retried until the AccessToken is returned. * DeviceCode.interval - The minimum amount of time in seconds that the client * SHOULD wait between polling requests to the token endpoin */ public Future<AuthenticationResult> acquireTokenByDeviceCode( final DeviceCode deviceCode, final AuthenticationCallback callback) throws AuthenticationException { final ClientAuthentication clientAuth = new ClientAuthenticationPost( ClientAuthenticationMethod.NONE, new ClientID(deviceCode.getClientId())); this.validateDeviceCodeRequestInput(deviceCode, clientAuth, deviceCode.getResource()); final AdalDeviceCodeAuthorizationGrant deviceCodeGrant = new AdalDeviceCodeAuthorizationGrant(deviceCode, deviceCode.getResource()); return this.acquireToken(deviceCodeGrant, clientAuth, callback); }