mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
6 changed files with 145 additions and 0 deletions
@ -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(); |
||||
}); |
||||
}); |
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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(); |
||||
}); |
||||
}); |
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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(); |
||||
}); |
||||
}); |
Loading…
Reference in new issue