MastraAuthOkta and 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 the required environment variables are set. In that case, use new MastraAuthOkta() without any arguments.
Constructor parametersDirect link to Constructor parameters
domain?:
dev-123456.okta.com). Used to construct the issuer URL and API endpoints.clientId?:
clientSecret?:
issuer?:
redirectUri?:
scopes?:
apiToken?:
getUser() to return user data by ID.session?:
cookieName?:
cookieMaxAge?:
cookiePassword?:
secureCookies?:
Secure flag on session cookies.name?:
Environment variablesDirect link to Environment variables
The following environment variables are automatically used when constructor options aren't provided:
OKTA_DOMAIN:
dev-123456.okta.com). Found in your Okta admin console.OKTA_CLIENT_ID:
OKTA_CLIENT_SECRET:
OKTA_ISSUER?:
https://{domain}/oauth2/default if not set.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:
sub claim).oktaId:
id).email?:
name?:
avatarUrl?:
groups?:
groups claim.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: [],
},
}),
},
})
Constructor parametersDirect link to Constructor parameters
roleMapping:
'_default' to assign permissions to users who do not match any group. Supports wildcards like '*' (full access) and 'agents:*' (all agent actions).domain?:
apiToken?:
getUserId?:
oktaId or id on the user object.