Skip to content

Commit

Permalink
Add Zitadel user roles (#145)
Browse files Browse the repository at this point in the history
* Add Zitadel user roles

* Code review fixes
  • Loading branch information
stankis authored Jun 24, 2024
1 parent 9caca7e commit 768efc0
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/utils/zitadel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,42 @@ import {Utils} from './utils';
import axios from 'axios';
import axiosRetry from 'axios-retry';

enum ZitadelUserRole {
Creator = 'creator',
Admin = 'admin',
Viewer = 'viewer',
}

type IntrospectionResult = {
active: boolean;
userId?: string;
username?: string;
role?: ZitadelUserRole;
};

const axiosInstance = axios.create();
axiosRetry(axiosInstance, {retries: 3});

const getRole = (data: any): ZitadelUserRole => {
const scope = 'urn:zitadel:iam:org:project:roles';

const roles = data[scope];

if (!roles) {
return ZitadelUserRole.Viewer;
}

if (roles['admin']) {
return ZitadelUserRole.Admin;
}

if (roles['creator']) {
return ZitadelUserRole.Creator;
}

return ZitadelUserRole.Viewer;
};

export const introspect = async (ctx: AppContext, token?: string): Promise<IntrospectionResult> => {
ctx.log('Token introspection');

Expand Down Expand Up @@ -47,7 +74,10 @@ export const introspect = async (ctx: AppContext, token?: string): Promise<Intro
ctx.log(`Token introspected successfully within: ${Utils.getDuration(hrStart)} ms`);

const {active, username, sub} = response.data;
return {active: Boolean(active), userId: sub, username};

const role = getRole(response.data);

return {active: Boolean(active), userId: sub, username, role};
} catch (e) {
ctx.logError('Failed to introspect token', e);
return {active: false};
Expand Down

0 comments on commit 768efc0

Please sign in to comment.