Browse Source

feat: add remaining strategies

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 1 year ago
parent
commit
2100044323
  1. 18
      packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.spec.ts
  2. 54
      packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts
  3. 18
      packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.spec.ts
  4. 33
      packages/nocodb-nest/src/strategies/base-view.strategy/base-view.strategy.ts
  5. 18
      packages/nocodb-nest/src/strategies/google.strategy/google.strategy.spec.ts
  6. 4
      packages/nocodb-nest/src/strategies/google.strategy/google.strategy.ts

18
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>(AuthtokenStrategy);
});
it('should be defined', () => {
expect(provider).toBeDefined();
});
});

54
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<string, any> = 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);
}
}
}

18
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>(BaseViewStrategy);
});
it('should be defined', () => {
expect(provider).toBeDefined();
});
});

33
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);
}
}
}

18
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>(GoogleStrategy);
});
it('should be defined', () => {
expect(provider).toBeDefined();
});
});

4
packages/nocodb-nest/src/strategies/google.strategy/google.strategy.ts

@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class GoogleStrategy {}
Loading…
Cancel
Save