diff --git a/packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.spec.ts b/packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.spec.ts new file mode 100644 index 0000000000..6875554765 --- /dev/null +++ b/packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { AuthtokenStrategy } from './authtoken.strategy'; + +describe('AuthtokenStrategy', () => { + let provider: AuthtokenStrategy; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [AuthtokenStrategy], + }).compile(); + + provider = module.get(AuthtokenStrategy); + }); + + it('should be defined', () => { + expect(provider).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts b/packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts new file mode 100644 index 0000000000..32282162f3 --- /dev/null +++ b/packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts @@ -0,0 +1,54 @@ +import { Injectable } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Strategy } from 'passport-custom'; +import { ApiToken, ProjectUser, User } from '../../models'; +import type { Request } from 'express'; + +@Injectable() +export class AuthTokenStrategy extends PassportStrategy(Strategy, 'authtoken') { + constructor() { + super({ + headerFields: ['xc-token'], + passReqToCallback: true, + }); + } + + // eslint-disable-next-line @typescript-eslint/ban-types + async validate(req: Request, token: string, done: Function) { + try { + const apiToken = await ApiToken.getByToken(token); + if (!apiToken) { + return done({ msg: 'Invalid token' }); + } + + const user: any = {}; + if (!apiToken.fk_user_id) { + user.roles = 'editor'; + return done(null, user); + } + + const dbUser: Record = await User.get(apiToken.fk_user_id); + if (!dbUser) { + return done({ msg: 'User not found' }); + } + + dbUser.is_api_token = true; + if (req['ncProjectId']) { + const projectUser = await ProjectUser.get( + req['ncProjectId'], + dbUser.id, + ); + user.roles = projectUser?.roles || dbUser.roles; + user.roles = user.roles === 'owner' ? 'owner,creator' : user.roles; + // + (user.roles ? `,${user.roles}` : ''); + // todo : cache + // await NocoCache.set(`${CacheScope.USER}:${key}`, user); + return done(null, user); + } + + return done(null, dbUser); + } catch (error) { + return done(error); + } + } +} diff --git a/packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.spec.ts b/packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.spec.ts new file mode 100644 index 0000000000..10671b2a1e --- /dev/null +++ b/packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { BaseViewStrategy } from './base-view.strategy'; + +describe('BaseViewStrategy', () => { + let provider: BaseViewStrategy; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [BaseViewStrategy], + }).compile(); + + provider = module.get(BaseViewStrategy); + }); + + it('should be defined', () => { + expect(provider).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.ts b/packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.ts new file mode 100644 index 0000000000..6b11e115ac --- /dev/null +++ b/packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Strategy } from 'passport-custom'; +import { Project } from '../../models' +import extractRolesObj from '../../utils/extractRolesObj' + +@Injectable() +export class BaseViewStrategy extends PassportStrategy(Strategy, 'base-view') { + // eslint-disable-next-line @typescript-eslint/ban-types + async validate(req: any, callback: Function) { + try { + let user; + if (req.headers['xc-shared-base-id']) { + // const cacheKey = `nc_shared_bases||${req.headers['xc-shared-base-id']}`; + + let sharedProject = null; + + if (!sharedProject) { + sharedProject = await Project.getByUuid( + req.headers['xc-shared-base-id'] + ); + } + user = { + roles: extractRolesObj(sharedProject?.roles), + }; + } + + callback(null, user); + } catch (error) { + callback(error); + } + } +} diff --git a/packages/nocodb-nest/src/strategies/google.strategy/google.strategy.spec.ts b/packages/nocodb-nest/src/strategies/google.strategy/google.strategy.spec.ts new file mode 100644 index 0000000000..fb588c18c3 --- /dev/null +++ b/packages/nocodb-nest/src/strategies/google.strategy/google.strategy.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GoogleStrategy } from './google.strategy'; + +describe('GoogleStrategy', () => { + let provider: GoogleStrategy; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [GoogleStrategy], + }).compile(); + + provider = module.get(GoogleStrategy); + }); + + it('should be defined', () => { + expect(provider).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/strategies/google.strategy/google.strategy.ts b/packages/nocodb-nest/src/strategies/google.strategy/google.strategy.ts new file mode 100644 index 0000000000..91c7a1515f --- /dev/null +++ b/packages/nocodb-nest/src/strategies/google.strategy/google.strategy.ts @@ -0,0 +1,4 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class GoogleStrategy {}