Hey devs! π Today, weβre diving into how to secure your API using OAuth 2.0 and OpenID Connect (OIDC) with Okta. Whether youβre building an Angular app or setting up RBAC (Role-Based Access Control), this guide will walk you through the Okta setup from scratchβno prior experience needed!
π What Weβll Cover
β
Setting up Okta from the very beginning (no account? No problem!)
β
Creating user groups and assigning access (RBAC in action)
β
Setting up an OAuth 2.0 app in Okta
β
Configuring authentication & API policies
π§ Step 1: Get an Okta Developer Account
First thing we need is an Okta Developer Edition account. Itβs free and gives us everything we need for secure authentication.
1οΈβ£ Go to Okta Developer and click Sign Up.
2οΈβ£ Select Developer Edition
3οΈβ£ Fill in your email, company name, and region. Note: Okta requires business email, if you don't have one you can quickly buy it for a cheap price (e.g. domain can be bought on GoDaddy while custom email address on proton)
4οΈβ£ Verify your email and log in to your Okta Admin Dashboard.
Once youβre in, youβll see your Okta domain at the top (something like https://dev-xxxxxx.okta.com). Save thisβitβs important!
π₯ Step 2: Set Up User Groups for RBAC
Using groups in Okta makes access control easy. Instead of manually managing users, weβll create a group and assign access at the group level.
π Create a Group
1οΈβ£ In the Okta Admin Dashboard, go to Directory β Groups.
2οΈβ£ Click "Add Group".
3οΈβ£ Give it a name and add short description.
4οΈβ£ Your new group can be found here β¬οΈ
π Add Users to the Group
1οΈβ£ Go to Directory β Groups.
2οΈβ£ Inside groups locate the desired group and click on it
3οΈβ£ Click "Assign people".
4οΈβ£ Click on plus button near the desired user.
π― Why This Matters:
RBAC (Role-Based Access Control) ensures only the right users can log in to your app. Later, weβll restrict API access to this group.
π οΈ Step 3: Create an OAuth 2.0 App in Okta
Now, letβs register our Angular app in Okta so it can use OAuth 2.0 and OpenID Connect (OIDC) for authentication.
π Create a New App
1οΈβ£ Go to Applications β Applications β Create App Integration.
2οΈβ£ Choose "OIDC - OpenID Connect" as the sign-in method.
3οΈβ£ Select "Single Page Application (SPA)" as the app type.
π Give it a name and configure redirect URIs
1οΈβ£ Base URI β http://localhost:4200
2οΈβ£ Login Redirect URI β http://localhost:4200/login/callback
3οΈβ£ Logout Redirect URI β http://localhost:4200
π Enable OAuth 2.0 Grant Types
1οΈβ£ Inside grant type section enable:
β
Authorization Code (more secure than implicit flow).
β
Refresh Token (for session persistence).
π Set Controlled Access
1οΈβ£ Select "Limit access to selected groups".
2οΈβ£ Choose the groups (this restricts access to only assigned users).
3οΈβ£ Click Save.
Now our Angular app is registered with Okta and ready to authenticate users! π
π Step 4: Configure Authentication & API Access Policies
Okta uses authentication policies to control who can log in and API policies to manage who gets access tokens. Letβs configure both!
π Set Up Authentication Policy (2FA Required!)
We want our users to log in with password + Okta Verify (TOTP) for extra security. By default Okta applies 'Any two factors' to new apps, but we have to ensure on it.
1οΈβ£ Go to Security β Authentication Policies and ensure the 'Any two factors' policy was applied to our app.
If not - then create the new one and apply it to the app:
2οΈβ£ Click Create Policy β Name it 'Angular Users Policy'.
3οΈβ£ Under Rules, set:
β
Require Password (Knowledge Factor).
β
Require Okta Verify - TOTP (Possession Factor).
4οΈβ£ Click Save and assign this policy to our Angular app.
π Configure API Authorization Policies
π Why Restrict API Tokens at the User Level, Not Just the App?
Imagine an attacker registers another app inside Oktaβif API policies only checked app authentication, this fake app could get valid access tokens! π±
By locking API tokens to specific user groups, we ensure that only authorized users within trusted apps can access our API. π
1οΈβ£ Prepare the ClientId of our angular app
2οΈβ£ Go to Security β API β Authorization Servers.
3οΈβ£ Click "default" Authorization Server β Go to Access Policies.
4οΈβ£ Click "Add Policy", give it a name (e.g. 'angular_app_policy'), and assign it to your angular app using its ClientID.
5οΈβ£ Add a Rule:
β
Allow Authorization Code with PKCE.
β
Apply it only to desired groups.
β
Allow Scopes (optional): openid, profile, email (restrict access to only necessary data).
6οΈβ£ Click Save.
π― Why This Matters:
β
Ensures only authenticated users can get API tokens.
β
Restricts API access based on user groups.
β
Uses scopes to limit what data users can access.
π₯ Whatβs Next? Frontend & Backend Integration!
Now that Okta is fully set up, itβs time to connect it to our Angular app and backend.
π Next Article: How to authenticate users in Angular & secure API requests.
π Upcoming: Backend token verification & advanced security best practices.
π Stay tuned & follow for updates!
π Wrapping Up
In this guide, we:
β
Created an Okta Developer account from scratch.
β
Set up user groups for RBAC.
β
Registered an OAuth 2.0 app in Okta.
β
Configured authentication policies (password + 2FA).
β
Set API authorization rules to restrict access.
With this setup, your app is now secure using OAuth 2.0, OpenID Connect, and Role-Based Access Control. π
Let me know in the comments if you have any questions, and check out my next posts on frontend and backend authentication & token verification! π₯
Top comments (0)