This guide provides details on how to create an Okta OpenID Connect (OIDC) app, run a Python Flask script to authenticate with your Okta app, retrieve an authorization code, and exchange it for an access token.
- Okta account
- Python 3.11 installed
- Log in to your Okta developers account
- Navigate to Applications and click on Create App Integration
- Select OIDC - OpenID Connect and Web Application
- Configure the application settings:
- Sign-in redirect URIs: http://localhost:8080/callback
- Sign-out redirect URIs: http://localhost:8080
- Save the client ID, client secret, and authorization server URL
The list of variables required to run this script are:
OKTA_OIDC_CLIENT_ID
- The client id of your OIDC applicationOKTA_OIDC_TOKEN_URL
- The token url suchhttps://dev-batman.okta.com/oauth2/v1/token
OKTA_OIDC_AUTHORIZATION_URL
- The authorization url suchhttps://dev-batman.okta.com/oauth2/v1/authorize
OKTA_OIDC_AUTHORIZATION_SCOPE
- A comma separated list of valid authorization scope. If not provided, the default will beopenid
OKTA_OIDC_REDIRECT_URI
- The redirect URI. If not provided, the default will be http://localhost:8080/callback
To retrieve an authorization code, and exchange it for an access token using a Python Flask application, run the following commands:
export OKTA_OIDC_CLIENT_ID=<ENTER CLIENT ID>
export OKTA_OIDC_TOKEN_URL=<ENTER TOKEN URL>
export OKTA_OIDC_AUTHORIZATION_URL=<ENTER AUTHENTICATION URL>
export OKTA_OIDC_AUTHORIZATION_SCOPE=<ENTER AUTHENTICATION SCOPE>
export OKTA_OIDC_REDIRECT_URI=<ENTER REDIRECT URI>
git clone https://github.com/port-labs/okta-oidc-key-exchange.git
cd okta-oidc-key-exchange
pip install -r ./requirements.txt
flask --app app run -p 8080
Navigate to http://localhost:8080/login to initiate the login process and generate the authorization code. After a successful authorization, you will be redirected to the redirect URI. On this page, you will be able to see the access token that was exchanged.
By following these steps, you can successfully authenticate users with your Okta app, retrieve an authorization code, and exchange it for an access token using a Python Flask application.