Browse Source

fix: jwt strategy init

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
7f7eb73fa2
  1. 31
      packages/nocodb-nest/src/app.module.ts
  2. 49
      packages/nocodb-nest/src/db/BaseModelSqlv2.ts
  3. 110
      packages/nocodb-nest/src/models/Model.ts
  4. 8
      packages/nocodb-nest/src/modules/auth/auth.module.ts
  5. 15
      packages/nocodb-nest/src/modules/global/global.module.ts
  6. 8
      packages/nocodb-nest/src/modules/users/users.controller.ts
  7. 13
      packages/nocodb-nest/src/modules/users/users.module.ts
  8. 8
      packages/nocodb-nest/src/modules/users/users.service.ts
  9. 24
      packages/nocodb-nest/src/strategies/jwt.strategy.ts

31
packages/nocodb-nest/src/app.module.ts

@ -1,5 +1,6 @@
import { Module, RequestMethod } from '@nestjs/common'; import { Module, RequestMethod } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core'; import { APP_FILTER } from '@nestjs/core';
import { ExtractJwt } from 'passport-jwt';
import { Connection } from './connection/connection'; import { Connection } from './connection/connection';
import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter'; import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter';
import { GlobalMiddleware } from './middlewares/global/global.middleware'; import { GlobalMiddleware } from './middlewares/global/global.middleware';
@ -7,6 +8,7 @@ import { AuthModule } from './modules/auth/auth.module';
import { ExtractProjectIdMiddleware } from './middlewares/extract-project-id/extract-project-id.middleware'; import { ExtractProjectIdMiddleware } from './middlewares/extract-project-id/extract-project-id.middleware';
import { UsersModule } from './modules/users/users.module'; import { UsersModule } from './modules/users/users.module';
import { MetaService } from './meta/meta.service'; import { MetaService } from './meta/meta.service';
import { UsersService } from './modules/users/users.service';
import { UtilsModule } from './modules/utils/utils.module'; import { UtilsModule } from './modules/utils/utils.module';
import { ProjectsModule } from './modules/projects/projects.module'; import { ProjectsModule } from './modules/projects/projects.module';
import Noco from './Noco'; import Noco from './Noco';
@ -47,17 +49,38 @@ import { ImportModule } from './modules/import/import.module';
import { CachesModule } from './modules/caches/caches.module'; import { CachesModule } from './modules/caches/caches.module';
import { TestModule } from './modules/test/test.module'; import { TestModule } from './modules/test/test.module';
import { PluginsModule } from './modules/plugins/plugins.module'; import { PluginsModule } from './modules/plugins/plugins.module';
import { GlobalModule } from './modules/global/global.module';
import NcConfigFactory from './utils/NcConfigFactory'
import NcUpgrader from './version-upgrader/NcUpgrader';
import type { import type {
MiddlewareConsumer, MiddlewareConsumer,
OnApplicationBootstrap, OnApplicationBootstrap,
Provider,
} from '@nestjs/common'; } from '@nestjs/common';
import { GlobalModule } from './modules/global/global.module';
import NcUpgrader from './version-upgrader/NcUpgrader'; export const JwtStrategyProvider: Provider = {
provide: JwtStrategy,
useFactory: async (usersService: UsersService) => {
const config = await NcConfigFactory.make()
const options = {
// ignoreExpiration: false,
jwtFromRequest: ExtractJwt.fromHeader('xc-auth'),
expiresIn: '10h',
passReqToCallback: true,
secretOrKey: config.auth.jwt.secret,
...config.auth.jwt.options,
};
return new JwtStrategy(options, usersService);
},
inject: [UsersService],
};
@Module({ @Module({
imports: [ imports: [
GlobalModule, GlobalModule,
AuthModule, // AuthModule,
UsersModule, UsersModule,
UtilsModule, UtilsModule,
ProjectsModule, ProjectsModule,
@ -104,7 +127,7 @@ import NcUpgrader from './version-upgrader/NcUpgrader';
provide: APP_FILTER, provide: APP_FILTER,
useClass: GlobalExceptionFilter, useClass: GlobalExceptionFilter,
}, },
JwtStrategy, JwtStrategyProvider,
ExtractProjectIdMiddleware, ExtractProjectIdMiddleware,
], ],
}) })

49
packages/nocodb-nest/src/db/BaseModelSqlv2.ts

@ -50,6 +50,8 @@ import genRollupSelectv2 from './genRollupSelectv2';
import conditionV2 from './conditionV2'; import conditionV2 from './conditionV2';
import sortV2 from './sortV2'; import sortV2 from './sortV2';
import { customValidators } from './util/customValidators'; import { customValidators } from './util/customValidators';
import type LookupColumn from '../../../nocodb/src/lib/models/LookupColumn';
import type { XKnex } from '../../../nocodb/src/lib/db/sql-data-mapper';
import type { import type {
XcFilter, XcFilter,
XcFilterWithAlias, XcFilterWithAlias,
@ -118,7 +120,7 @@ function checkColumnRequired(
* @classdesc Base class for models * @classdesc Base class for models
*/ */
class BaseModelSqlv2 { class BaseModelSqlv2 {
protected dbDriver: Knex; protected dbDriver: XKnex;
protected model: Model; protected model: Model;
protected viewId: string; protected viewId: string;
private _proto: any; private _proto: any;
@ -1404,7 +1406,6 @@ class BaseModelSqlv2 {
return []; return [];
} }
}); });
const self: BaseModelSqlv2 = this; const self: BaseModelSqlv2 = this;
proto[column.title] = async function (args): Promise<any> { proto[column.title] = async function (args): Promise<any> {
@ -1891,7 +1892,7 @@ class BaseModelSqlv2 {
await this.execAndParse(query); await this.execAndParse(query);
const newData = await this.readByPk(id); const newData = await this.readByPk(id);
await this.afterUpdate(prevData, newData, trx, cookie); await this.afterUpdate(prevData, newData, trx, cookie, updateObj);
return newData; return newData;
} catch (e) { } catch (e) {
console.log(e); console.log(e);
@ -2323,7 +2324,7 @@ class BaseModelSqlv2 {
op_type: AuditOperationTypes.DATA, op_type: AuditOperationTypes.DATA,
op_sub_type: AuditOperationSubTypes.INSERT, op_sub_type: AuditOperationSubTypes.INSERT,
description: DOMPurify.sanitize( description: DOMPurify.sanitize(
`${id} inserted into ${this.model.title}`, `Record with ID ${id} has been inserted into Table ${this.model.title}`,
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
@ -2349,7 +2350,9 @@ class BaseModelSqlv2 {
op_type: AuditOperationTypes.DATA, op_type: AuditOperationTypes.DATA,
op_sub_type: AuditOperationSubTypes.BULK_UPDATE, op_sub_type: AuditOperationSubTypes.BULK_UPDATE,
description: DOMPurify.sanitize( description: DOMPurify.sanitize(
`${noOfUpdatedRecords} records bulk updated in ${this.model.title}`, `${noOfUpdatedRecords} ${
noOfUpdatedRecords > 1 ? 'records have' : 'record has'
} been bulk updated in ${this.model.title}`,
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
@ -2374,7 +2377,9 @@ class BaseModelSqlv2 {
op_type: AuditOperationTypes.DATA, op_type: AuditOperationTypes.DATA,
op_sub_type: AuditOperationSubTypes.BULK_DELETE, op_sub_type: AuditOperationSubTypes.BULK_DELETE,
description: DOMPurify.sanitize( description: DOMPurify.sanitize(
`${noOfDeletedRecords} records bulk deleted in ${this.model.title}`, `${noOfDeletedRecords} ${
noOfDeletedRecords > 1 ? 'records have' : 'record has'
} been bulk deleted in ${this.model.title}`,
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
@ -2390,7 +2395,9 @@ class BaseModelSqlv2 {
op_type: AuditOperationTypes.DATA, op_type: AuditOperationTypes.DATA,
op_sub_type: AuditOperationSubTypes.BULK_INSERT, op_sub_type: AuditOperationSubTypes.BULK_INSERT,
description: DOMPurify.sanitize( description: DOMPurify.sanitize(
`${data.length} records bulk inserted into ${this.model.title}`, `${data.length} ${
data.length > 1 ? 'records have' : 'record has'
} been bulk inserted in ${this.model.title}`,
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
@ -2415,15 +2422,31 @@ class BaseModelSqlv2 {
newData: any, newData: any,
_trx: any, _trx: any,
req, req,
updateObj?: Record<string, any>,
): Promise<void> { ): Promise<void> {
const id = this._extractPksValues(newData); const id = this._extractPksValues(newData);
let desc = `Record with ID ${id} has been updated in Table ${this.model.title}.`;
if (updateObj) {
updateObj = await this.model.mapColumnToAlias(updateObj);
for (const k of Object.keys(updateObj)) {
const prevValue =
typeof prevData[k] === 'object'
? JSON.stringify(prevData[k])
: prevData[k];
const newValue =
typeof newData[k] === 'object'
? JSON.stringify(newData[k])
: newData[k];
desc += `\n`;
desc += `Column "${k}" got changed from "${prevValue}" to "${newValue}"`;
}
}
await Audit.insert({ await Audit.insert({
fk_model_id: this.model.id, fk_model_id: this.model.id,
row_id: id, row_id: id,
op_type: AuditOperationTypes.DATA, op_type: AuditOperationTypes.DATA,
op_sub_type: AuditOperationSubTypes.UPDATE, op_sub_type: AuditOperationSubTypes.UPDATE,
description: DOMPurify.sanitize(`${id} updated in ${this.model.title}`), description: DOMPurify.sanitize(desc),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
user: req?.user?.email, user: req?.user?.email,
@ -2451,7 +2474,9 @@ class BaseModelSqlv2 {
row_id: id, row_id: id,
op_type: AuditOperationTypes.DATA, op_type: AuditOperationTypes.DATA,
op_sub_type: AuditOperationSubTypes.DELETE, op_sub_type: AuditOperationSubTypes.DELETE,
description: DOMPurify.sanitize(`${id} deleted from ${this.model.title}`), description: DOMPurify.sanitize(
`Record with ID ${id} has been deleted in Table ${this.model.title}`,
),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
user: req?.user?.email, user: req?.user?.email,
@ -2722,7 +2747,7 @@ class BaseModelSqlv2 {
op_sub_type: AuditOperationSubTypes.LINK_RECORD, op_sub_type: AuditOperationSubTypes.LINK_RECORD,
row_id: rowId, row_id: rowId,
description: DOMPurify.sanitize( description: DOMPurify.sanitize(
`Record [id:${childId}] record linked with record [id:${rowId}] record in ${this.model.title}`, `Record [id:${childId}] has been linked with record [id:${rowId}] in ${this.model.title}`,
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
@ -2824,7 +2849,7 @@ class BaseModelSqlv2 {
op_sub_type: AuditOperationSubTypes.UNLINK_RECORD, op_sub_type: AuditOperationSubTypes.UNLINK_RECORD,
row_id: rowId, row_id: rowId,
description: DOMPurify.sanitize( description: DOMPurify.sanitize(
`Record [id:${childId}] record unlinked with record [id:${rowId}] record in ${this.model.title}`, `Record [id:${childId}] has been unlinked with record [id:${rowId}] in ${this.model.title}`,
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,

110
packages/nocodb-nest/src/models/Model.ts

@ -57,7 +57,7 @@ export default class Model implements TableType {
{ {
fk_model_id: this.id, fk_model_id: this.id,
}, },
ncMeta, ncMeta
); );
return this.columns; return this.columns;
} }
@ -94,7 +94,7 @@ export default class Model implements TableType {
mm?: BoolType; mm?: BoolType;
type?: ModelTypes; type?: ModelTypes;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
const insertObj = extractProps(model, [ const insertObj = extractProps(model, [
'table_name', 'table_name',
@ -113,7 +113,7 @@ export default class Model implements TableType {
{ {
project_id: projectId, project_id: projectId,
base_id: baseId, base_id: baseId,
}, }
); );
} }
@ -125,13 +125,13 @@ export default class Model implements TableType {
projectId, projectId,
baseId, baseId,
MetaTable.MODELS, MetaTable.MODELS,
insertObj, insertObj
); );
await NocoCache.appendToList( await NocoCache.appendToList(
CacheScope.MODEL, CacheScope.MODEL,
[projectId], [projectId],
`${CacheScope.MODEL}:${id}`, `${CacheScope.MODEL}:${id}`
); );
const view = await View.insert( const view = await View.insert(
@ -141,7 +141,7 @@ export default class Model implements TableType {
is_default: true, is_default: true,
type: ViewTypes.GRID, type: ViewTypes.GRID,
}, },
ncMeta, ncMeta
); );
for (const column of model?.columns || []) { for (const column of model?.columns || []) {
@ -159,7 +159,7 @@ export default class Model implements TableType {
project_id: string; project_id: string;
base_id: string; base_id: string;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
): Promise<Model[]> { ): Promise<Model[]> {
let modelList = []; let modelList = [];
if (base_id) { if (base_id) {
@ -176,7 +176,7 @@ export default class Model implements TableType {
orderBy: { orderBy: {
order: 'asc', order: 'asc',
}, },
}, }
); );
// parse meta of each model // parse meta of each model
@ -188,7 +188,7 @@ export default class Model implements TableType {
await NocoCache.setList( await NocoCache.setList(
CacheScope.MODEL, CacheScope.MODEL,
[project_id, base_id], [project_id, base_id],
modelList, modelList
); );
} else { } else {
await NocoCache.setList(CacheScope.MODEL, [project_id], modelList); await NocoCache.setList(CacheScope.MODEL, [project_id], modelList);
@ -197,7 +197,7 @@ export default class Model implements TableType {
modelList.sort( modelList.sort(
(a, b) => (a, b) =>
(a.order != null ? a.order : Infinity) - (a.order != null ? a.order : Infinity) -
(b.order != null ? b.order : Infinity), (b.order != null ? b.order : Infinity)
); );
return modelList.map((m) => new Model(m)); return modelList.map((m) => new Model(m));
} }
@ -210,7 +210,7 @@ export default class Model implements TableType {
project_id: string; project_id: string;
db_alias: string; db_alias: string;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
): Promise<Model[]> { ): Promise<Model[]> {
let modelList = await NocoCache.getList(CacheScope.MODEL, [ let modelList = await NocoCache.getList(CacheScope.MODEL, [
project_id, project_id,
@ -220,7 +220,7 @@ export default class Model implements TableType {
modelList = await ncMeta.metaList2( modelList = await ncMeta.metaList2(
project_id, project_id,
db_alias, db_alias,
MetaTable.MODELS, MetaTable.MODELS
); );
// parse meta of each model // parse meta of each model
@ -244,7 +244,7 @@ export default class Model implements TableType {
id && id &&
(await NocoCache.get( (await NocoCache.get(
`${CacheScope.MODEL}:${id}`, `${CacheScope.MODEL}:${id}`,
CacheGetType.TYPE_OBJECT, CacheGetType.TYPE_OBJECT
)); ));
if (!modelData) { if (!modelData) {
modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, id); modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, id);
@ -267,14 +267,14 @@ export default class Model implements TableType {
| { | {
id?: string; id?: string;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
): Promise<Model> { ): Promise<Model> {
const k = 'id' in args ? args?.id : args; const k = 'id' in args ? args?.id : args;
let modelData = let modelData =
k && k &&
(await NocoCache.get( (await NocoCache.get(
`${CacheScope.MODEL}:${k}`, `${CacheScope.MODEL}:${k}`,
CacheGetType.TYPE_OBJECT, CacheGetType.TYPE_OBJECT
)); ));
if (!modelData) { if (!modelData) {
modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, k); modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, k);
@ -295,13 +295,13 @@ export default class Model implements TableType {
table_name?: string; table_name?: string;
id?: string; id?: string;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
): Promise<Model> { ): Promise<Model> {
let modelData = let modelData =
id && id &&
(await NocoCache.get( (await NocoCache.get(
`${CacheScope.MODEL}:${id}`, `${CacheScope.MODEL}:${id}`,
CacheGetType.TYPE_OBJECT, CacheGetType.TYPE_OBJECT
)); ));
if (!modelData) { if (!modelData) {
modelData = await ncMeta.metaGet2( modelData = await ncMeta.metaGet2(
@ -310,7 +310,7 @@ export default class Model implements TableType {
MetaTable.MODELS, MetaTable.MODELS,
id || { id || {
table_name, table_name,
}, }
); );
modelData.meta = parseMetaProp(modelData); modelData.meta = parseMetaProp(modelData);
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData); await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
@ -336,7 +336,7 @@ export default class Model implements TableType {
dbDriver: XKnex; dbDriver: XKnex;
model?: Model; model?: Model;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
): Promise<BaseModelSqlv2> { ): Promise<BaseModelSqlv2> {
const model = args?.model || (await this.get(args.id, ncMeta)); const model = args?.model || (await this.get(args.id, ncMeta));
@ -388,7 +388,7 @@ export default class Model implements TableType {
await NocoCache.deepDel( await NocoCache.deepDel(
cacheScopeName, cacheScopeName,
`${cacheScopeName}:${col.id}`, `${cacheScopeName}:${col.id}`,
CacheDelDirection.CHILD_TO_PARENT, CacheDelDirection.CHILD_TO_PARENT
); );
} }
} }
@ -402,14 +402,14 @@ export default class Model implements TableType {
condition: { condition: {
fk_related_model_id: this.id, fk_related_model_id: this.id,
}, },
}, }
); );
for (const col of leftOverColumns) { for (const col of leftOverColumns) {
await NocoCache.deepDel( await NocoCache.deepDel(
CacheScope.COL_RELATION, CacheScope.COL_RELATION,
`${CacheScope.COL_RELATION}:${col.fk_column_id}`, `${CacheScope.COL_RELATION}:${col.fk_column_id}`,
CacheDelDirection.CHILD_TO_PARENT, CacheDelDirection.CHILD_TO_PARENT
); );
} }
@ -421,7 +421,7 @@ export default class Model implements TableType {
await NocoCache.deepDel( await NocoCache.deepDel(
CacheScope.COLUMN, CacheScope.COLUMN,
`${CacheScope.COLUMN}:${this.id}`, `${CacheScope.COLUMN}:${this.id}`,
CacheDelDirection.CHILD_TO_PARENT, CacheDelDirection.CHILD_TO_PARENT
); );
await ncMeta.metaDelete(null, null, MetaTable.COLUMNS, { await ncMeta.metaDelete(null, null, MetaTable.COLUMNS, {
fk_model_id: this.id, fk_model_id: this.id,
@ -430,7 +430,7 @@ export default class Model implements TableType {
await NocoCache.deepDel( await NocoCache.deepDel(
CacheScope.MODEL, CacheScope.MODEL,
`${CacheScope.MODEL}:${this.id}`, `${CacheScope.MODEL}:${this.id}`,
CacheDelDirection.CHILD_TO_PARENT, CacheDelDirection.CHILD_TO_PARENT
); );
await ncMeta.metaDelete(null, null, MetaTable.MODELS, this.id); await ncMeta.metaDelete(null, null, MetaTable.MODELS, this.id);
@ -457,11 +457,29 @@ export default class Model implements TableType {
return insertObj; return insertObj;
} }
async mapColumnToAlias(data) {
const res = {};
for (const col of await this.getColumns()) {
if (isVirtualCol(col)) continue;
let val =
data?.[col.title] !== undefined
? data?.[col.title]
: data?.[col.column_name];
if (val !== undefined) {
if (col.uidt === UITypes.Attachment && typeof val !== 'string') {
val = JSON.stringify(val);
}
res[sanitize(col.title)] = val;
}
}
return res;
}
static async updateAliasAndTableName( static async updateAliasAndTableName(
tableId, tableId,
title: string, title: string,
table_name: string, table_name: string,
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
if (!title) { if (!title) {
NcError.badRequest("Missing 'title' property in body"); NcError.badRequest("Missing 'title' property in body");
@ -488,7 +506,7 @@ export default class Model implements TableType {
title, title,
table_name, table_name,
}, },
tableId, tableId
); );
} }
@ -510,7 +528,7 @@ export default class Model implements TableType {
{ {
mm: isMm, mm: isMm,
}, },
tableId, tableId
); );
} }
@ -535,7 +553,7 @@ export default class Model implements TableType {
static async updateOrder( static async updateOrder(
tableId: string, tableId: string,
order: number, order: number,
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
// get existing cache // get existing cache
const key = `${CacheScope.MODEL}:${tableId}`; const key = `${CacheScope.MODEL}:${tableId}`;
@ -553,14 +571,14 @@ export default class Model implements TableType {
{ {
order, order,
}, },
tableId, tableId
); );
} }
static async updatePrimaryColumn( static async updatePrimaryColumn(
tableId: string, tableId: string,
columnId: string, columnId: string,
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
const model = await this.getWithInfo({ id: tableId }); const model = await this.getWithInfo({ id: tableId });
const newPvCol = model.columns.find((c) => c.id === columnId); const newPvCol = model.columns.find((c) => c.id === columnId);
@ -585,7 +603,7 @@ export default class Model implements TableType {
{ {
pv: false, pv: false,
}, },
col.id, col.id
); );
} }
@ -605,7 +623,7 @@ export default class Model implements TableType {
{ {
pv: true, pv: true,
}, },
newPvCol.id, newPvCol.id
); );
const grid_views_with_column = await ncMeta.metaList2( const grid_views_with_column = await ncMeta.metaList2(
@ -616,7 +634,7 @@ export default class Model implements TableType {
condition: { condition: {
fk_column_id: newPvCol.id, fk_column_id: newPvCol.id,
}, },
}, }
); );
if (grid_views_with_column.length) { if (grid_views_with_column.length) {
@ -645,7 +663,7 @@ export default class Model implements TableType {
{ {
mm: true, mm: true,
}, },
id, id
); );
} }
@ -659,14 +677,14 @@ export default class Model implements TableType {
base_id?: string; base_id?: string;
aliasOrId: string; aliasOrId: string;
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
const modelId = const modelId =
project_id && project_id &&
aliasOrId && aliasOrId &&
(await NocoCache.get( (await NocoCache.get(
`${CacheScope.MODEL}:${project_id}:${aliasOrId}`, `${CacheScope.MODEL}:${project_id}:${aliasOrId}`,
CacheGetType.TYPE_OBJECT, CacheGetType.TYPE_OBJECT
)); ));
if (!modelId) { if (!modelId) {
const model = base_id const model = base_id
@ -689,7 +707,7 @@ export default class Model implements TableType {
}, },
}, },
], ],
}, }
) )
: await ncMeta.metaGet2( : await ncMeta.metaGet2(
null, null,
@ -710,12 +728,12 @@ export default class Model implements TableType {
}, },
}, },
], ],
}, }
); );
if (model) { if (model) {
await NocoCache.set( await NocoCache.set(
`${CacheScope.MODEL}:${project_id}:${aliasOrId}`, `${CacheScope.MODEL}:${project_id}:${aliasOrId}`,
model.id, model.id
); );
await NocoCache.set(`${CacheScope.MODEL}:${model.id}`, model); await NocoCache.set(`${CacheScope.MODEL}:${model.id}`, model);
} }
@ -731,7 +749,7 @@ export default class Model implements TableType {
base_id, base_id,
exclude_id, exclude_id,
}: { table_name; project_id; base_id; exclude_id? }, }: { table_name; project_id; base_id; exclude_id? },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
return !(await ncMeta.metaGet2( return !(await ncMeta.metaGet2(
project_id, project_id,
@ -741,7 +759,7 @@ export default class Model implements TableType {
table_name, table_name,
}, },
null, null,
exclude_id && { id: { neq: exclude_id } }, exclude_id && { id: { neq: exclude_id } }
)); ));
} }
@ -752,7 +770,7 @@ export default class Model implements TableType {
base_id, base_id,
exclude_id, exclude_id,
}: { title; project_id; base_id; exclude_id? }, }: { title; project_id; base_id; exclude_id? },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
return !(await ncMeta.metaGet2( return !(await ncMeta.metaGet2(
project_id, project_id,
@ -762,14 +780,14 @@ export default class Model implements TableType {
title, title,
}, },
null, null,
exclude_id && { id: { neq: exclude_id } }, exclude_id && { id: { neq: exclude_id } }
)); ));
} }
async getAliasColObjMap() { async getAliasColObjMap() {
return (await this.getColumns()).reduce( return (await this.getColumns()).reduce(
(sortAgg, c) => ({ ...sortAgg, [c.title]: c }), (sortAgg, c) => ({ ...sortAgg, [c.title]: c }),
{}, {}
); );
} }
@ -777,7 +795,7 @@ export default class Model implements TableType {
static async updateMeta( static async updateMeta(
tableId: string, tableId: string,
meta: string | Record<string, any>, meta: string | Record<string, any>,
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta
) { ) {
// get existing cache // get existing cache
const key = `${CacheScope.MODEL}:${tableId}`; const key = `${CacheScope.MODEL}:${tableId}`;
@ -797,7 +815,7 @@ export default class Model implements TableType {
{ {
meta: typeof meta === 'object' ? JSON.stringify(meta) : meta, meta: typeof meta === 'object' ? JSON.stringify(meta) : meta,
}, },
tableId, tableId
); );
} }
} }

8
packages/nocodb-nest/src/modules/auth/auth.module.ts

@ -5,18 +5,12 @@ import { LocalStrategy } from '../../strategies/local.strategy';
import { UsersModule } from '../users/users.module'; import { UsersModule } from '../users/users.module';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { AuthController } from './auth.controller'; import { AuthController } from './auth.controller';
import { jwtConstants } from './constants';
@Module({ @Module({
controllers: [AuthController], controllers: [AuthController],
imports: [ imports: [
UsersModule, UsersModule,
PassportModule, PassportModule
JwtModule.register({
secret: jwtConstants.secret,
signOptions: { expiresIn: '10h' },
}),
], ],
providers: [AuthService, LocalStrategy], providers: [AuthService, LocalStrategy],
}) })

15
packages/nocodb-nest/src/modules/global/global.module.ts

@ -1,16 +1,23 @@
import { Global, Module } from '@nestjs/common' import { Global, Module } from '@nestjs/common'
import { JwtModule, JwtService } from '@nestjs/jwt'
import { Connection } from '../../connection/connection' import { Connection } from '../../connection/connection'
import { MetaService } from '../../meta/meta.service' import { MetaService } from '../../meta/meta.service'
import { jwtConstants } from '../auth/constants'
@Global() @Global()
@Module({ @Module({
imports: [
],
providers: [ providers: [
Connection, Connection,
MetaService MetaService,
], ],
exports: [ exports: [
Connection, Connection,
MetaService MetaService,
] // JwtService,
],
}) })
export class GlobalModule {} export class GlobalModule {
}

8
packages/nocodb-nest/src/modules/users/users.controller.ts

@ -31,12 +31,14 @@ export class UsersController {
'/api/v1/db/auth/user/signup', '/api/v1/db/auth/user/signup',
'/api/v1/auth/user/signup', '/api/v1/auth/user/signup',
]) ])
async signup(@Request() req: any, @Request() res: any): Promise<any> { async signup(@Request() req: any, @Response() res: any): Promise<any> {
return await this.usersService.signup({ res.json(
await this.usersService.signup({
body: req.body, body: req.body,
req, req,
res, res,
}); }),
);
} }
@Post([ @Post([

13
packages/nocodb-nest/src/modules/users/users.module.ts

@ -1,13 +1,20 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt'; import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from '../auth/constants';
import { GlobalModule } from '../global/global.module'; import { GlobalModule } from '../global/global.module';
import { UsersService } from './users.service'; import { UsersService } from './users.service';
import { UsersController } from './users.controller'; import { UsersController } from './users.controller';
@Module({ @Module({
imports: [GlobalModule], imports: [
GlobalModule,
JwtModule.register({
secret: jwtConstants.secret,
signOptions: { expiresIn: '10h' },
}),
],
controllers: [UsersController], controllers: [UsersController],
providers: [UsersService, JwtService], providers: [UsersService],
exports: [UsersService], exports: [UsersService],
}) })
export class UsersModule {} export class UsersModule {}

8
packages/nocodb-nest/src/modules/users/users.service.ts

@ -459,7 +459,6 @@ export class UsersService {
'Warning : `mailSend` failed, Please configure emailClient configuration.', 'Warning : `mailSend` failed, Please configure emailClient configuration.',
); );
} }
await promisify((param.req as any).login.bind(param.req))(user);
const refreshToken = randomTokenString(); const refreshToken = randomTokenString();
@ -468,9 +467,8 @@ export class UsersService {
email: user.email, email: user.email,
}); });
setTokenCookie(param.res, refreshToken);
user = (param.req as any).user; setTokenCookie(param.res, refreshToken);
await Audit.insert({ await Audit.insert({
op_type: 'AUTHENTICATION', op_type: 'AUTHENTICATION',
@ -481,7 +479,7 @@ export class UsersService {
}); });
return { return {
token: genJwt(user, Noco.getConfig()), token: this.login(user),
} as any; } as any;
} }
@ -490,7 +488,7 @@ export class UsersService {
delete user.salt; delete user.salt;
const payload = user; const payload = user;
return { return {
token: this.jwtService.sign(payload), token: genJwt(user, Noco.getConfig()), //this.jwtService.sign(payload),
}; };
} }
} }

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

@ -4,22 +4,28 @@ import { ExtractJwt, Strategy } from 'passport-jwt';
import { OrgUserRoles } from '../../../nocodb-sdk'; import { OrgUserRoles } from '../../../nocodb-sdk';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { ProjectUser, User } from '../models'; import { ProjectUser, User } from '../models';
import { genJwt } from '../modules/users/helpers'
import Noco from '../Noco'
import extractRolesObj from '../utils/extractRolesObj'; import extractRolesObj from '../utils/extractRolesObj';
import { CacheGetType, CacheScope } from '../utils/globals'; import { CacheGetType, CacheScope } from '../utils/globals';
import { jwtConstants } from '../modules/auth/constants'; import { jwtConstants } from '../modules/auth/constants';
import { UsersService } from '../modules/users/users.service'; import { UsersService } from '../modules/users/users.service';
import NcConfigFactory from '../utils/NcConfigFactory'
@Injectable() @Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) { export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private userService: UsersService) { constructor(options, private userService: UsersService, ) {
super({ super(
// ignoreExpiration: false, options
jwtFromRequest: ExtractJwt.fromHeader('xc-auth'), )
secretOrKey: jwtConstants.secret, // {
expiresIn: '10h', // // ignoreExpiration: false,
// jwtFromRequest: ExtractJwt.fromHeader('xc-auth'),
passReqToCallback: true, // expiresIn: '10h',
}); // passReqToCallback: true,
// secretOrKey: process.env.NC_AUTH_JWT_SECRET ?? 'temporary-key'
//
// });
} }
async validate(req: any, jwtPayload: any) { async validate(req: any, jwtPayload: any) {

Loading…
Cancel
Save