Browse Source

Merge pull request #5572 from nocodb/fix/passport-strategies

fix: api-token strategy correction
pull/5581/head
Pranav C 2 years ago committed by GitHub
parent
commit
cc37b70814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      packages/nocodb-nest/src/guards/global/global.guard.ts
  2. 65
      packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts
  3. 4
      packages/nocodb-nest/src/strategies/jwt.strategy.ts

13
packages/nocodb-nest/src/guards/global/global.guard.ts

@ -13,14 +13,17 @@ export class GlobalGuard extends AuthGuard(['jwt']) {
async canActivate(context: ExecutionContext) {
let result;
try {
result = await this.extractBoolVal(super.canActivate(context));
} catch (e) {
console.log(e);
}
const req = context.switchToHttp().getRequest();
if (req.headers?.['xc-auth']) {
try {
result = await this.extractBoolVal(super.canActivate(context));
} catch (e) {
console.log(e);
}
}
if (result && !req.headers['xc-shared-base-id']) {
if (
req.path.indexOf('/user/me') === -1 &&

65
packages/nocodb-nest/src/strategies/authtoken.strategy/authtoken.strategy.ts

@ -2,53 +2,44 @@ 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) {
async validate(req: any, callback: Function) {
try {
const apiToken = await ApiToken.getByToken(token);
if (!apiToken) {
return done({ msg: 'Invalid token' });
}
let user;
if (req.headers['xc-token']) {
const apiToken = await ApiToken.getByToken(req.headers['xc-token']);
if (!apiToken) {
return callback({ msg: 'Invalid token' });
}
const user: any = {};
if (!apiToken.fk_user_id) {
user.roles = 'editor';
return done(null, user);
}
user = {};
if (!apiToken.fk_user_id) {
user.roles = 'editor';
return callback(null, user);
}
const dbUser: Record<string, any> = await User.get(apiToken.fk_user_id);
if (!dbUser) {
return done({ msg: 'User not found' });
}
const dbUser: Record<string, any> = await User.get(apiToken.fk_user_id);
if (!dbUser) {
return callback({ 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);
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;
return callback(null, user);
}
}
return done(null, dbUser);
return callback(null, user);
} catch (error) {
return done(error);
return callback(error);
}
}
}

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

@ -1,6 +1,6 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { Strategy } from 'passport-jwt';
import { OrgUserRoles } from 'nocodb-sdk';
import NocoCache from '../cache/NocoCache';
import { ProjectUser, User } from '../models';

Loading…
Cancel
Save