Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: update org-token api response

Pranav Cba62bb1b8a01
diff --git a/packages/nocodb/src/controllers/org-tokens.controller.ts b/packages/nocodb/src/controllers/org-tokens.controller.ts
index be600a63f1..41e8e2c35d 100644
--- a/packages/nocodb/src/controllers/org-tokens.controller.ts
+++ b/packages/nocodb/src/controllers/org-tokens.controller.ts
@@ -37,6 +37,7 @@ export class OrgTokensController {
this.orgTokensEeService.apiTokenListEE,
)({
query: req.query,
+ req: req,
user: req['user'],
});
}
diff --git a/packages/nocodb/src/meta/migrations/v2/nc_088_add_sso_client_to_api_tokens.ts b/packages/nocodb/src/meta/migrations/v2/nc_088_add_sso_client_to_api_tokens.ts
index 348ef7af2e..9eb3100853 100644
--- a/packages/nocodb/src/meta/migrations/v2/nc_088_add_sso_client_to_api_tokens.ts
+++ b/packages/nocodb/src/meta/migrations/v2/nc_088_add_sso_client_to_api_tokens.ts
@@ -3,7 +3,7 @@ import { MetaTable } from '~/utils/globals';
const up = async (knex: Knex) => {
await knex.schema.alterTable(MetaTable.API_TOKENS, (table) => {
- table.string('fk_sso_client_id', 20)
+ table.string('fk_sso_client_id', 20);
table.index('fk_sso_client_id');
});
};
diff --git a/packages/nocodb/src/models/ApiToken.ts b/packages/nocodb/src/models/ApiToken.ts
index 3783890bb2..13c8310dfc 100644
--- a/packages/nocodb/src/models/ApiToken.ts
+++ b/packages/nocodb/src/models/ApiToken.ts
@@ -123,7 +123,12 @@ export default class ApiToken implements ApiTokenType {
{
fk_user_id,
includeUnmappedToken = false,
- }: { fk_user_id?: string; includeUnmappedToken?: boolean } = {},
+ ssoClientId,
+ }: {
+ fk_user_id?: string;
+ includeUnmappedToken?: boolean;
+ ssoClientId?: string;
+ } = {},
ncMeta = Noco.ncMeta,
): Promise<number> {
const qb = ncMeta.knex(MetaTable.API_TOKENS);
@@ -132,6 +137,12 @@ export default class ApiToken implements ApiTokenType {
qb.where(`${MetaTable.API_TOKENS}.fk_user_id`, fk_user_id);
}
+ // SSO filtering logic - if no ssoClientId, user is non-SSO and should only see non-SSO tokens
+ if (!ssoClientId) {
+ qb.whereNull(`${MetaTable.API_TOKENS}.fk_sso_client_id`);
+ }
+ // If ssoClientId exists, user logged via SSO and sees all tokens (no additional filtering)
+
if (includeUnmappedToken) {
qb.orWhereNull(`${MetaTable.API_TOKENS}.fk_user_id`);
}
@@ -145,11 +156,13 @@ export default class ApiToken implements ApiTokenType {
offset = 0,
fk_user_id,
includeUnmappedToken = false,
+ ssoClientId,
}: {
limit: number;
offset: number;
fk_user_id?: string;
includeUnmappedToken: boolean;
+ ssoClientId?: string;
},
ncMeta = Noco.ncMeta,
) {
@@ -162,6 +175,7 @@ export default class ApiToken implements ApiTokenType {
`${MetaTable.API_TOKENS}.token`,
`${MetaTable.API_TOKENS}.description`,
`${MetaTable.API_TOKENS}.fk_user_id`,
+ `${MetaTable.API_TOKENS}.fk_sso_client_id`,
`${MetaTable.API_TOKENS}.base_id`,
`${MetaTable.API_TOKENS}.created_at`,
`${MetaTable.API_TOKENS}.updated_at`,
@@ -180,6 +194,12 @@ export default class ApiToken implements ApiTokenType {
queryBuilder.where(`${MetaTable.API_TOKENS}.fk_user_id`, fk_user_id);
}
+ // SSO filtering logic - if no ssoClientId, user is non-SSO and should only see non-SSO tokens
+ if (!ssoClientId) {
+ queryBuilder.whereNull(`${MetaTable.API_TOKENS}.fk_sso_client_id`);
+ }
+ // If ssoClientId exists, user logged via SSO and sees all tokens (no additional filtering)
+
if (includeUnmappedToken) {
queryBuilder.orWhereNull(`${MetaTable.API_TOKENS}.fk_user_id`);
}
diff --git a/packages/nocodb/src/services/org-tokens.service.ts b/packages/nocodb/src/services/org-tokens.service.ts
index 6459aa0fd7..8a4afdb886 100644
--- a/packages/nocodb/src/services/org-tokens.service.ts
+++ b/packages/nocodb/src/services/org-tokens.service.ts
@@ -13,24 +13,30 @@ import { ApiToken } from '~/models';
export class OrgTokensService {
constructor(protected readonly appHooksService: AppHooksService) {}
- async apiTokenList(param: { user: User; query: any }) {
+ async apiTokenList(param: { user: User; query: any; req?: NcRequest }) {
const fk_user_id = param.user.id;
let includeUnmappedToken = false;
if (extractRolesObj(param.user.roles)[OrgUserRoles.SUPER_ADMIN]) {
includeUnmappedToken = true;
}
+ // Check if user logged in via SSO
+ const ssoClientId = param.req?.user
+ ? (param.req.user as any)?.extra?.sso_client_id
+ : undefined;
return new PagedResponseImpl(
await ApiToken.listWithCreatedBy({
...param.query,
fk_user_id,
includeUnmappedToken,
+ ssoClientId,
}),
{
...param.query,
count: await ApiToken.count({
includeUnmappedToken,
fk_user_id,
+ ssoClientId,
}),
},
);
@@ -48,7 +54,7 @@ export class OrgTokensService {
// Get SSO client ID if user logged in via SSO
const ssoClientId = (param.req.user as any)?.extra?.sso_client_id;
-
+
const apiToken = await ApiToken.insert({
...param.apiToken,
fk_user_id: param['user'].id,