API Security Using OIDC
API Security Using OIDC
Table of Contents
API Security using OIDC..............................................................................................................................1
1. Create and configure and API Oauth Client in OKTA......................................................................2
2. Configure resource server to protect API resources using OKTA...................................................2
SpringBoot example :..............................................................................................................................2
i) Maven Dependencies to enable Oauth2 Resource server using OKTA OIDC client:.....................2
iv) Test your application:.................................................................................................................3
3. Using different types of Token Validations:...................................................................................3
i) JWT token validation (Local):......................................................................................................4
Note : When using PingFederate as Authorization server.................................................................4
ii) Using Token introspection endpoint (Remote):.........................................................................4
iii) Using UserInfo endpoint (Remote):........................................................................................5
Extract Username Attribute........................................................................................................................5
API Access Management............................................................................................................................5
1. Restrict access to API based on ‘Scope’:.........................................................................................5
2. Restrict access to API based on ‘ClientID’:......................................................................................5
3. Restrict access to API based on Username/Principal attribute :....................................................6
4. Restrict API access based on User’s group membership :..............................................................6
Method 2 : Another method is explained at...........................................................................................6
5. Add Scope/Claim based access on Individual API :.......................................................................7
Please follow below steps to secure an API resource server using OKTA.
- Follow below documentation to setup and protect your resource server using OKTA
https://developer.okta.com/docs/guides/protect-your-api/aspnet/before-you-begin/
https://github.com/okta/samples-java-spring/tree/master/resource-server
i) Maven Dependencies to enable Oauth2 Resource server using OKTA OIDC client: Edit
the pom.xml file and add dependencies for Spring Security and Okta. They will enable
the Spring AND Okta OAuth 2.0 for your application.
<!-- security - begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>com.okta.spring</groupId>
<artifactId>okta-spring-boot-starter</artifactId>
<version>0.6.1</version>
</dependency>
<!-- security - end -->
@Configuration
static class OktaOAuth2WebSecurityConfigurerAdapter extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt(); //or .opaqueToken();
- Call your API by passing the Access token in headers as shown below
> export TOKEN=${YOUR_TOKEN}
> curl http://localhost:8080 -H "Authorization: Bearer $TOKEN"
Hello World
1. Using pingfederate admin console, get the JWKS endpoint from JWT token manager
config as shown below.
e.g. https://cloudsso-test.cisco.com/ext/access/jwks
.oauth2ResourceServer().opaqueToken();
To enable userinfo for token validation, you need to add below entry in your
application.yml file
security:
oauth2:
resource:
user-info-uri: https://{yourOktaDomain}/oauth2/default/v1/userinfo
# Below flag is needed only when you have both userinfo and tokeninfo entries
prefer-token-info: false
1. Restrict access to API based on ‘Scope’: Add below line at route level security config in
WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**").hasAuthority("SCOPE_profile")
4. Restrict API access based on User’s group membership : This method works when
userinfo URI is used for token validation (See Section 3 above)
if (principal.contains("Workforce-okta-admins-testasf")){
return principal;
}else{
return "Access Denied : You are not part of Workforce-okta-admins-test ";
}
@GetMapping("/api/messages")
@PreAuthorize("hasAuthority('SCOPE_profile')")