MastraAuthOkta & MastraRBACOkta class
MastraAuthOkta classDirect link to MastraAuthOkta class
The MastraAuthOkta class provides authentication for Mastra using Okta. It implements an OAuth 2.0 / OIDC login flow with encrypted session cookies and integrates with the Mastra server using the auth option.
Usage exampleDirect link to Usage example
import { Mastra } from '@mastra/core'
import { MastraAuthOkta } from '@mastra/auth-okta'
export const mastra = new Mastra({
server: {
auth: new MastraAuthOkta({
domain: process.env.OKTA_DOMAIN,
clientId: process.env.OKTA_CLIENT_ID,
clientSecret: process.env.OKTA_CLIENT_SECRET,
redirectUri: process.env.OKTA_REDIRECT_URI,
}),
},
})
You can omit the constructor parameters if you have the appropriately named environment variables set. In that case, use new MastraAuthOkta() without any arguments.
Constructor parametersDirect link to Constructor parameters
domain?:
clientId?:
clientSecret?:
issuer?:
redirectUri?:
scopes?:
apiToken?:
session?:
cookieName?:
cookieMaxAge?:
cookiePassword?:
secureCookies?:
name?:
Environment variablesDirect link to Environment variables
The following environment variables are automatically used when constructor options are not provided:
OKTA_DOMAIN:
OKTA_CLIENT_ID:
OKTA_CLIENT_SECRET:
OKTA_ISSUER?:
OKTA_REDIRECT_URI:
OKTA_COOKIE_PASSWORD?:
OKTA_API_TOKEN?:
Authentication flowDirect link to Authentication flow
MastraAuthOkta authenticates requests in the following order:
- Session cookie: Reads the encrypted session cookie and decrypts it. If the session is valid and not expired, the user is authenticated.
- JWT fallback: If no session cookie is present, verifies the
Authorizationheader token against Okta's JWKS endpoint.
After authentication, authorizeUser checks that the user has a valid oktaId. Provide a custom authorizeUser function to implement additional logic.
OktaUser typeDirect link to oktauser-type
The OktaUser type extends the base EEUser interface with Okta-specific fields:
id:
oktaId:
email?:
name?:
avatarUrl?:
groups?:
MastraRBACOkta classDirect link to MastraRBACOkta class
The MastraRBACOkta class maps Okta groups to Mastra permissions. It fetches user groups from the Okta API and resolves them against a configurable role mapping. Use it with MastraAuthOkta or any other auth provider.
RBAC requires a valid Enterprise Edition license. It works without a license in development so you can try it locally, but you’ll need a license for production. Contact sales for more information.
Usage exampleDirect link to Usage example
Use MastraRBACOkta alongside an auth provider by passing it to the rbac option:
import { Mastra } from '@mastra/core'
import { MastraAuthOkta, MastraRBACOkta } from '@mastra/auth-okta'
export const mastra = new Mastra({
server: {
auth: new MastraAuthOkta(),
rbac: new MastraRBACOkta({
roleMapping: {
Admin: ['*'],
Engineering: ['agents:*', 'workflows:*', 'tools:*'],
Viewer: ['agents:read', 'workflows:read'],
_default: [],
},
}),
},
})
To use Okta RBAC with a different auth provider, pass a getUserId function to resolve the Okta user ID from the other provider's user object:
import { MastraAuthAuth0 } from '@mastra/auth-auth0'
import { MastraRBACOkta } from '@mastra/auth-okta'
export const mastra = new Mastra({
server: {
auth: new MastraAuthAuth0(),
rbac: new MastraRBACOkta({
getUserId: user => user.metadata?.oktaUserId || user.email,
roleMapping: {
Engineering: ['agents:*', 'workflows:*'],
Admin: ['*'],
_default: [],
},
}),
},
})