use of com.okta.spring.boot.oauth.config.OktaOAuth2Properties in project okta-spring-boot by okta.
the class OktaOAuth2Configurer method init.
@SuppressWarnings("rawtypes")
@Override
public void init(HttpSecurity http) throws Exception {
ApplicationContext context = http.getSharedObject(ApplicationContext.class);
// make sure OktaOAuth2Properties are available
if (!context.getBeansOfType(OktaOAuth2Properties.class).isEmpty()) {
OktaOAuth2Properties oktaOAuth2Properties = context.getBean(OktaOAuth2Properties.class);
// Auth Code Flow Config
// if OAuth2ClientProperties bean is not available do NOT configure
OAuth2ClientProperties.Provider propertiesProvider;
OAuth2ClientProperties.Registration propertiesRegistration;
if (!context.getBeansOfType(OAuth2ClientProperties.class).isEmpty() && (propertiesProvider = context.getBean(OAuth2ClientProperties.class).getProvider().get("okta")) != null && (propertiesRegistration = context.getBean(OAuth2ClientProperties.class).getRegistration().get("okta")) != null && !isEmpty(propertiesProvider.getIssuerUri()) && !isEmpty(propertiesRegistration.getClientId())) {
// configure Okta user services
configureLogin(http, oktaOAuth2Properties, context.getBean(Environment.class));
// check for RP-Initiated logout
if (!context.getBeansOfType(OidcClientInitiatedLogoutSuccessHandler.class).isEmpty()) {
http.logout().logoutSuccessHandler(context.getBean(OidcClientInitiatedLogoutSuccessHandler.class));
}
// if issuer is root org, use opaque token validation
if (TokenUtil.isRootOrgIssuer(propertiesProvider.getIssuerUri())) {
log.debug("Opaque Token validation/introspection will be configured.");
configureResourceServerForOpaqueTokenValidation(http, oktaOAuth2Properties);
return;
}
OAuth2ResourceServerConfigurer oAuth2ResourceServerConfigurer = http.getConfigurer(OAuth2ResourceServerConfigurer.class);
if (getJwtConfigurer(oAuth2ResourceServerConfigurer).isPresent()) {
log.debug("JWT configurer is set in OAuth resource server configuration. " + "JWT validation will be configured.");
configureResourceServerForJwtValidation(http, oktaOAuth2Properties);
} else if (getOpaqueTokenConfigurer(oAuth2ResourceServerConfigurer).isPresent()) {
log.debug("Opaque Token configurer is set in OAuth resource server configuration. " + "Opaque Token validation/introspection will be configured.");
configureResourceServerForOpaqueTokenValidation(http, oktaOAuth2Properties);
} else {
log.debug("OAuth2ResourceServerConfigurer bean not configured, Resource Server support will not be enabled.");
}
} else {
log.debug("OAuth/OIDC Login not configured due to missing issuer, client-id, or client-secret property");
}
}
}
use of com.okta.spring.boot.oauth.config.OktaOAuth2Properties in project okta-spring-boot by okta.
the class OktaOAuth2ResourceServerAutoConfig method restTemplate.
static RestTemplate restTemplate(OktaOAuth2Properties oktaOAuth2Properties) {
Proxy proxy;
OktaOAuth2Properties.Proxy proxyProperties = oktaOAuth2Properties.getProxy();
Optional<BasicAuthenticationInterceptor> basicAuthenticationInterceptor = Optional.empty();
if (proxyProperties != null && Strings.hasText(proxyProperties.getHost()) && proxyProperties.getPort() > 0) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyProperties.getHost(), proxyProperties.getPort()));
if (Strings.hasText(proxyProperties.getUsername()) && Strings.hasText(proxyProperties.getPassword())) {
basicAuthenticationInterceptor = Optional.of(new BasicAuthenticationInterceptor(proxyProperties.getUsername(), proxyProperties.getPassword()));
}
} else {
proxy = Proxy.NO_PROXY;
}
RestTemplate restTemplate = new RestTemplate(Arrays.asList(new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter(), new StringHttpMessageConverter()));
restTemplate.getInterceptors().add(new UserAgentRequestInterceptor());
basicAuthenticationInterceptor.ifPresent(restTemplate.getInterceptors()::add);
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setProxy(proxy);
restTemplate.setRequestFactory(requestFactory);
return restTemplate;
}
use of com.okta.spring.boot.oauth.config.OktaOAuth2Properties in project okta-spring-boot by okta.
the class OktaOAuth2ResourceServerAutoConfig method opaqueTokenIntrospector.
@Bean
@Conditional(OktaOpaqueTokenIntrospectConditional.class)
OpaqueTokenIntrospector opaqueTokenIntrospector(OktaOAuth2Properties oktaOAuth2Properties, OAuth2ResourceServerProperties oAuth2ResourceServerProperties) {
RestTemplate restTemplate = restTemplate(oktaOAuth2Properties);
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(oAuth2ResourceServerProperties.getOpaquetoken().getClientId(), oAuth2ResourceServerProperties.getOpaquetoken().getClientSecret()));
OpaqueTokenIntrospector delegate = new NimbusOpaqueTokenIntrospector(oAuth2ResourceServerProperties.getOpaquetoken().getIntrospectionUri(), restTemplate);
return token -> {
OAuth2AuthenticatedPrincipal principal = delegate.introspect(token);
Collection<GrantedAuthority> mappedAuthorities = Collections.unmodifiableCollection(TokenUtil.opaqueTokenClaimsToAuthorities(principal.getAttributes(), oktaOAuth2Properties.getGroupsClaim(), principal.getAuthorities()));
return new DefaultOAuth2AuthenticatedPrincipal(principal.getName(), principal.getAttributes(), mappedAuthorities);
};
}
Aggregations