DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • What D'Hack Is DPoP?
  • Guide to Integrating OKTA OAuth 2.0 OIDC With Mulesoft API Anypoint Platform (Mule 4)
  • Secure Your API With JWT: Kong OpenID Connect
  • Configuring SSO Using WSO2 Identity Server

Trending

  • OWASP Top 10 Explained: SQL Injection
  • Data Flow Diagrams for Software Engineering
  • Running LLMs Locally: A Step-by-Step Guide
  • Spring Strategy Pattern Example
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Using OKTA as Client Provider in Mulesoft

Using OKTA as Client Provider in Mulesoft

There are many ways to secure your API in Mulesoft and in this article we are going to address the topic of identity and access control with OpenID connect.

By 
Gaurav Dhimate user avatar
Gaurav Dhimate
DZone Core CORE ·
Aug. 12, 21 · Tutorial
Like (7)
Save
Tweet
Share
13.8K Views

Join the DZone community and get the full member experience.

Join For Free

There are many ways to secure your API in Mulesoft and in this article we are going to address the topic of identity and access control with OpenID connect. We are going to integrate our AnyPoint platform with an OpenID Connect Provider (OKTA) to provide the OAuth feature to our Anypoint platform.

 OpenID Connect is an identity layer on top of the OAuth 2.0 protocol and it has all the OAuth 2.0 capabilities integrated with the protocol itself.

Before we begin let's look into the basics of OAuth2

Basics of OAuth2

Roles:

  • Resource Owner: The owner of the resources that we are trying to access, in our case it will be the Anypoint platform.
  • Client: The application/user that wants to access the resource, in our case it will be API users.
  • Authorization Server: The authorization service that authenticates the resource owner and/or the client, issues access tokens to the client, and tracks their validity, in our case, it will be the OKTA server.
  • Resource Server:  The service hosting the protected resources. The resource server must be able to validate the tokens issued by the authorization server, in our case, it will be API deployed on the Runtime manager 

Access token:

Access token prove that the resource owner authorizes the client to act on their behalf over specific resources during a limited amount of time.

OAuth 2.0 Authorization Grant Types :

In OAuth 2.0, the term grant type refers to the way an application gets an access token

  •  Authorization Code: The Authorization Code grant type is used by confidential and public clients to exchange an authorization code for an access token.
  •  Implicit: The authorization server gives the access token to the user-agent so it can forward the token to the client
  •  Resource Owner Password Credentials: The Resource Owner Credentials Grant Type uses the username and the password credentials of a Resource Owner (user) to authorize and access protected data from a Resource Server
  •  Client Credentials: The Client Credentials Grant Type uses the client_id and the client_secret credentials of a Client to authorize and access protected data from a Resource Server

Now let's see the step-by-step configuration of the same.

STEPS:

Create an account in Okta: Create a developer account at https://developer.okta.com/signup/  or if you have enterprise account you can use that.

Create Authorization server: Once we have created the OKTA account we need to create an Authorization server which is going to validate our client.

Go to Security --> API --> Authorization Servers

Create Authorization Server


Authorization Server

Add scope: For the auth-server-demo server let's create a custom scope.

 auth-server-demo --> scope

Add scope for auth-server-demo server


read

Create “read” scope


Create “read” scope

Let's add policy

 Add policy


We will create a generic policy for all clients to this server.


Generic policy for all clients


access tokenrefresh token

Define access token and refresh token validity


Define access token and refresh token validity


Get metadata URL: Meta data URL will have all the details we need to configure OKTA with the Anypoint platform. All the required URL's will be in this file.

Get metadata URL


Sample content of the file is as below:

JSON
 
{
   "issuer":"https://dev-1703011.okta.com/oauth2/aus1fe8x0enTNEfcl5d7",
   "authorization_endpoint":"https://dev-1703011.okta.com/oauth2/aus1fe8x0enTNEfcl5d7/v1/authorize",
   "token_endpoint":"https://dev-1703011.okta.com/oauth2/aus1fe8x0enTNEfcl5d7/v1/token",
   "registration_endpoint":"https://dev-1703011.okta.com/oauth2/v1/clients",
   "jwks_uri":"https://dev-1703011.okta.com/oauth2/aus1fe8x0enTNEfcl5d7/v1/keys",
   "introspection_endpoint":"https://dev-1703011.okta.com/oauth2/aus1fe8x0enTNEfcl5d7/v1/introspect",
   "scopes_supported":[
      "read",
      "openid",
      "profile",
      "email",
      "address",
      "phone",
      "offline_access"
   ]
}}


Note: This content is not complete, you will have other fields as well.

Create a new app in OKTA: Applications -- > create app Integration --> demo-app.

This app will act as introspect app and will be used to introspect the access token.

New app in OKTA

          

Select sign-on method as “OIDC - OpenID connect” and application type as “web application”

     New app integration


New app integration


client credentials” 

Client credentials


Assigning access

   

 “ClientID/Client secret” that we get below will be used while configuring the provider in                       Anypoint Platform


ClientID/Client secret


Create introspective token: This token that we are going to generate will be used by Anypoint platform to connect OKTA

API --> token --> demo-token 

Create introspective token



Create introspective token


Config client provider in access management: Use config obtain from auth server URL.

 Anypoint platform --> Access management --> Client provider 

 Config client provider in access management


Config client provider in access management

Fill in the details on the form.

Fill in the details on the form.

Authorization token will be "SSWS  <introspective token that we got in the previous step>"

SSWS token authorization


SSWS token authorization


SSWS token authorization

Note: If you see the assigned environment is 0 for our new client provider, that means we haven't associated any environment yet to this provider.

Assign provider to the environment where you intend to use it.

Access management --> Environment --> Sandbox --> client-provider

Assign provider to the environment


Assign provider to the environment

Update RAML to have a security header: We need to update RAML to use the OpenId policy in our API.

JSON
 
securitySchemes:
  oauth_2_0:
        description: |
            Mule OAuth 2.0.
        type: OAuth 2.0
        describedBy:
            headers:
                Authorization:
                    description: |
                      Used to send a valid OAuth 2 access token. Do not use
                      with the "access_token" query string parameter.
                    type: string
            queryParameters:
                access_token:
                    description: |
                      Used to send a valid OAuth 2 access token. Do not use together with
                      the "Authorization" header
                    type: string
            responses:
                401:
                    description: |
                        Bad or expired token.
                403:
                    description: |
                        Bad OAuth request.
        settings:
          authorizationUri: https://dev-1703011.okta.com/oauth2/default/v1/authorize
          accessTokenUri: https://dev-1703011.okta.com/oauth2/default/v1/token
          authorizationGrants: [authorization_code, password, client_credentials, implicit]


Add this security schema as:

JSON
 
/flights:
  get:
    securedBy: [oauth_2_0]


Import RAML project into API manager, select provider while creating an instance: Select client-provider while importing.

Import RAML project into API manager


Select client-provider while importing


Deploy application the auto-discovery id (17138632): The next step is to deploy an API in Runtime manager with the Auto discovery id that we got from above step.

Apply OpenId policy: Api manager -- > api --> policy

Apply OpenId policy


Note: If OpenId is not configured then this policy won't show up.

Add accepted scopes:

Add accepted scopes


Request access from Exchange with option Authorization code grant type.

Request access from Exchange


Requesting access

Create a new application which will also be created in OKTA under the application.

Creating new application


Request API Access

Enable client credentials to grant type in OKTA for above demo-app-mule

Enabling client credentials

Since mulesoft doesn't allow you to create a client app with "client credentials" we need to enable it from OKTA.

Client credentials

  • Get access token from  OKTA

          Add header -- > content-type:application/x-www-form-urlencoded

         Add basic auth -->  clientid/clientsecret - retro token creds

         Add query params -->  grant_type:client_credentialsscope:read

  Sample curl command for getting OKTA token will be below:

Plain Text
 
curl --location --request POST 'https://<token_url>?grant_type=client_credentials&scope=read' \
--header 'content-type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic MG9hMWZldDR6NjFEbWR3bFQ1ZDc6QkMxUlZtNHNLcDRYZExOZ2tDOVZ2SFd2blZKYVpPZi1SdC1pb2IzVg==' \
--header 'Cookie: JSESSIONID=CAC5CF30DE7C6DCB186FEC7461A18788'

 

Note: Just replace <token_url> with your token endpoint from metadata URL JSON. In response, you will get the below response:

JSON
 
{
    "token_type": "Bearer",
    "expires_in": 3600,
    "access_token": "eyJraWQiOiJyNU8tcmg1RVR4aHNLR3JlNGZRWWMyT1lmU2lSZGZlSDZuZHFmU2VMajBZIiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULlVUSG05TUgwSlRJNTNVcDl5RGdaWl9iN0VwVHlPbVhrUmFMTzA1UHNrWEEiLCJpc3MiOiJodHRwczovL2Rldi0xNzAzMDExLm9rdGEuY29tL29hdXRoMi9hdXMxZmU4eDBlblRORWZjbDVkNyIsImF1ZCI6ImFwaTovL2RlZmF1bHQiLCJpYXQiOjE2Mjg0NDUwMDQsImV4cCI6MTYyODQ0ODYwNCwiY2lkIjoiMG9hMWZldDR6NjFEbWR3bFQ1ZDciLCJzY3AiOlsicmVhZCJdLCJzdWIiOiIwb2ExZmV0NHo2MURtZHdsVDVkNyJ9.AG5tXTFnbn5p-uh40pkIDYHUo7QFO2z-IZ92MVJFWMFHER_oF-Tc2-YgoZBUCA9AUUgoRGimq937qc9JJ8Z0SzxG9jqEAMhk1iFdX3KrgM0wbkhScFJDZjGdZuSCIjRVuVF8ZuRxcZh7HKuEubrenat6ybchw7HzGHkktuBzdmg4iQXg66kskGz6Qk1LX_xAXvMUmTipESpWaCl8uqUXDkbN0hWSLsjlD67V0CvZko5wuIYIXLNuPg7RlEdPydKtEuZZsF0NRXxvs3xGb2NmIuGNTjhogwJmEhh7AEW-NF7Tri5qdHiyP70AYgaKxtmAW5ojyafOcF6DurWE0cyQ0Q",
    "scope": "read"
}


Call API with the access token: Use access_token from the above step to access the API, if everything is good then you will get proper response from API.

Call API with the access token


Conclusion 

We have successfully configured OKTA as a client-provider for our Anypoint platform.

authentication MuleSoft API security application app OpenID JSON Plain text

Opinions expressed by DZone contributors are their own.

Related

  • What D'Hack Is DPoP?
  • Guide to Integrating OKTA OAuth 2.0 OIDC With Mulesoft API Anypoint Platform (Mule 4)
  • Secure Your API With JWT: Kong OpenID Connect
  • Configuring SSO Using WSO2 Identity Server

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: