Restructuring the Project
Updating dependencies
This commit is contained in:
40
Backend/src/models/client.ts
Normal file
40
Backend/src/models/client.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
import { v4 } from "uuid";
|
||||
|
||||
export interface IClient extends ModelDataBase {
|
||||
maintainer: ObjectID;
|
||||
internal: boolean;
|
||||
name: string;
|
||||
redirect_url: string;
|
||||
website: string;
|
||||
logo?: string;
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
featured?: boolean;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
const Client = DB.addModel<IClient>({
|
||||
name: "client",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
maintainer: { type: ObjectID },
|
||||
internal: { type: Boolean, default: false },
|
||||
name: { type: String },
|
||||
redirect_url: { type: String },
|
||||
website: { type: String },
|
||||
logo: { type: String, optional: true },
|
||||
client_id: { type: String, default: () => v4() },
|
||||
client_secret: { type: String },
|
||||
featured: { type: Boolean, optional: true },
|
||||
description: { type: String, optional: true },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default Client;
|
29
Backend/src/models/client_code.ts
Normal file
29
Backend/src/models/client_code.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
import { v4 } from "uuid";
|
||||
|
||||
export interface IClientCode extends ModelDataBase {
|
||||
user: ObjectID;
|
||||
code: string;
|
||||
client: ObjectID;
|
||||
permissions: ObjectID[];
|
||||
validTill: Date;
|
||||
}
|
||||
|
||||
const ClientCode = DB.addModel<IClientCode>({
|
||||
name: "client_code",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
user: { type: ObjectID },
|
||||
code: { type: String },
|
||||
client: { type: ObjectID },
|
||||
permissions: { type: Array },
|
||||
validTill: { type: Date },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
export default ClientCode;
|
25
Backend/src/models/grants.ts
Normal file
25
Backend/src/models/grants.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
|
||||
export interface IGrant extends ModelDataBase {
|
||||
user: ObjectID;
|
||||
client: ObjectID;
|
||||
permissions: ObjectID[];
|
||||
}
|
||||
|
||||
const Grant = DB.addModel<IGrant>({
|
||||
name: "grant",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
user: { type: ObjectID },
|
||||
client: { type: ObjectID },
|
||||
permissions: { type: ObjectID, array: true },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default Grant;
|
76
Backend/src/models/login_token.ts
Normal file
76
Backend/src/models/login_token.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
import moment = require("moment");
|
||||
|
||||
export interface ILoginToken extends ModelDataBase {
|
||||
token: string;
|
||||
special: boolean;
|
||||
user: ObjectID;
|
||||
validTill: Date;
|
||||
valid: boolean;
|
||||
validated: boolean;
|
||||
data: any;
|
||||
ip: string;
|
||||
browser: string;
|
||||
}
|
||||
const LoginToken = DB.addModel<ILoginToken>({
|
||||
name: "login_token",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
token: { type: String },
|
||||
special: { type: Boolean, default: () => false },
|
||||
user: { type: ObjectID },
|
||||
validTill: { type: Date },
|
||||
valid: { type: Boolean },
|
||||
},
|
||||
},
|
||||
{
|
||||
migration: (doc: ILoginToken) => {
|
||||
doc.validated = true;
|
||||
},
|
||||
schema: {
|
||||
token: { type: String },
|
||||
special: { type: Boolean, default: () => false },
|
||||
user: { type: ObjectID },
|
||||
validTill: { type: Date },
|
||||
valid: { type: Boolean },
|
||||
validated: { type: Boolean, default: false },
|
||||
},
|
||||
},
|
||||
{
|
||||
migration: (doc: ILoginToken) => {
|
||||
doc.validated = true;
|
||||
},
|
||||
schema: {
|
||||
token: { type: String },
|
||||
special: { type: Boolean, default: () => false },
|
||||
user: { type: ObjectID },
|
||||
validTill: { type: Date },
|
||||
valid: { type: Boolean },
|
||||
validated: { type: Boolean, default: false },
|
||||
data: { type: "any", optional: true },
|
||||
ip: { type: String, optional: true },
|
||||
browser: { type: String, optional: true },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export async function CheckToken(
|
||||
token: ILoginToken,
|
||||
validated: boolean = true
|
||||
): Promise<boolean> {
|
||||
if (!token || !token.valid) return false;
|
||||
if (validated && !token.validated) return false;
|
||||
if (moment().isAfter(token.validTill)) {
|
||||
token.valid = false;
|
||||
await LoginToken.save(token);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export default LoginToken;
|
24
Backend/src/models/mail.ts
Normal file
24
Backend/src/models/mail.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo";
|
||||
|
||||
export interface IMail extends ModelDataBase {
|
||||
mail: string;
|
||||
verified: boolean;
|
||||
primary: boolean;
|
||||
}
|
||||
|
||||
const Mail = DB.addModel<IMail>({
|
||||
name: "mail",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
mail: { type: String },
|
||||
verified: { type: Boolean, default: false },
|
||||
primary: { type: Boolean },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default Mail;
|
37
Backend/src/models/permissions.ts
Normal file
37
Backend/src/models/permissions.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
|
||||
export interface IPermission extends ModelDataBase {
|
||||
name: string;
|
||||
description: string;
|
||||
client: ObjectID;
|
||||
grant_type: "user" | "client";
|
||||
}
|
||||
|
||||
const Permission = DB.addModel<IPermission>({
|
||||
name: "permission",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
name: { type: String },
|
||||
description: { type: String },
|
||||
client: { type: ObjectID },
|
||||
},
|
||||
},
|
||||
{
|
||||
migration: (old) => {
|
||||
old.grant_type = "user";
|
||||
},
|
||||
schema: {
|
||||
name: { type: String },
|
||||
description: { type: String },
|
||||
client: { type: ObjectID },
|
||||
grant_type: { type: String, default: "user" },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default Permission;
|
32
Backend/src/models/refresh_token.ts
Normal file
32
Backend/src/models/refresh_token.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
import { v4 } from "uuid";
|
||||
|
||||
export interface IRefreshToken extends ModelDataBase {
|
||||
token: string;
|
||||
user: ObjectID;
|
||||
client: ObjectID;
|
||||
permissions: ObjectID[];
|
||||
validTill: Date;
|
||||
valid: boolean;
|
||||
}
|
||||
|
||||
const RefreshToken = DB.addModel<IRefreshToken>({
|
||||
name: "refresh_token",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
token: { type: String },
|
||||
user: { type: ObjectID },
|
||||
client: { type: ObjectID },
|
||||
permissions: { type: Array },
|
||||
validTill: { type: Date },
|
||||
valid: { type: Boolean },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default RefreshToken;
|
57
Backend/src/models/regcodes.ts
Normal file
57
Backend/src/models/regcodes.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
import { v4 } from "uuid";
|
||||
|
||||
export interface IRegCode extends ModelDataBase {
|
||||
token: string;
|
||||
valid: boolean;
|
||||
validTill: Date;
|
||||
}
|
||||
|
||||
const RegCode = DB.addModel<IRegCode>({
|
||||
name: "reg_code",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
token: { type: String },
|
||||
valid: { type: Boolean },
|
||||
validTill: { type: Date },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default RegCode;
|
||||
|
||||
// import { Model, Table, Column, ForeignKey, BelongsTo, Unique, CreatedAt, UpdatedAt, DeletedAt, HasMany, BelongsToMany, Default, DataType } from "sequelize-typescript"
|
||||
// import User from "./user";
|
||||
// import Permission from "./permissions";
|
||||
// import RefreshPermission from "./refresh_permission";
|
||||
|
||||
// @Table
|
||||
// export default class RegCode extends Model<RegCode> {
|
||||
// @Unique
|
||||
// @Default(DataType.UUIDV4)
|
||||
// @Column(DataType.UUID)
|
||||
// token: string
|
||||
|
||||
// @Column
|
||||
// validTill: Date
|
||||
|
||||
// @Column
|
||||
// valid: boolean
|
||||
|
||||
// @Column
|
||||
// @CreatedAt
|
||||
// creationDate: Date;
|
||||
|
||||
// @Column
|
||||
// @UpdatedAt
|
||||
// updatedOn: Date;
|
||||
|
||||
// @Column
|
||||
// @DeletedAt
|
||||
// deletionDate: Date;
|
||||
// }
|
69
Backend/src/models/twofactor.ts
Normal file
69
Backend/src/models/twofactor.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "bson";
|
||||
|
||||
export enum TFATypes {
|
||||
OTC,
|
||||
BACKUP_CODE,
|
||||
U2F,
|
||||
APP_ALLOW,
|
||||
}
|
||||
|
||||
export const TFANames = new Map<TFATypes, string>();
|
||||
TFANames.set(TFATypes.OTC, "Authenticator");
|
||||
TFANames.set(TFATypes.BACKUP_CODE, "Backup Codes");
|
||||
TFANames.set(TFATypes.U2F, "Security Key (U2F)");
|
||||
TFANames.set(TFATypes.APP_ALLOW, "App Push");
|
||||
|
||||
export interface ITwoFactor extends ModelDataBase {
|
||||
user: ObjectID;
|
||||
valid: boolean;
|
||||
expires?: Date;
|
||||
name?: string;
|
||||
type: TFATypes;
|
||||
data: any;
|
||||
}
|
||||
|
||||
export interface IOTC extends ITwoFactor {
|
||||
data: string;
|
||||
}
|
||||
|
||||
export interface IYubiKey extends ITwoFactor {
|
||||
data: {
|
||||
registration?: any;
|
||||
publicKey: string;
|
||||
keyHandle: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface IU2F extends ITwoFactor {
|
||||
data: {
|
||||
challenge?: string;
|
||||
publicKey: string;
|
||||
keyHandle: string;
|
||||
registration?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface IBackupCode extends ITwoFactor {
|
||||
data: string[];
|
||||
}
|
||||
|
||||
const TwoFactor = DB.addModel<ITwoFactor>({
|
||||
name: "twofactor",
|
||||
versions: [
|
||||
{
|
||||
migration: (e) => {},
|
||||
schema: {
|
||||
user: { type: ObjectID },
|
||||
valid: { type: Boolean },
|
||||
expires: { type: Date, optional: true },
|
||||
name: { type: String, optional: true },
|
||||
type: { type: Number },
|
||||
data: { type: "any" },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default TwoFactor;
|
134
Backend/src/models/user.ts
Normal file
134
Backend/src/models/user.ts
Normal file
@ -0,0 +1,134 @@
|
||||
import DB from "../database";
|
||||
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
|
||||
import { ObjectID } from "mongodb";
|
||||
import { v4 } from "uuid";
|
||||
import { randomString } from "../helper/random";
|
||||
|
||||
export enum Gender {
|
||||
none,
|
||||
male,
|
||||
female,
|
||||
other,
|
||||
}
|
||||
|
||||
export interface IUser extends ModelDataBase {
|
||||
uid: string;
|
||||
username: string;
|
||||
|
||||
name: string;
|
||||
birthday?: Date;
|
||||
gender: Gender;
|
||||
admin: boolean;
|
||||
password: string;
|
||||
salt: string;
|
||||
mails: ObjectID[];
|
||||
phones: { phone: string; verified: boolean; primary: boolean }[];
|
||||
encryption_key: string;
|
||||
}
|
||||
|
||||
const User = DB.addModel<IUser>({
|
||||
name: "user",
|
||||
versions: [
|
||||
{
|
||||
migration: () => {},
|
||||
schema: {
|
||||
uid: { type: String, default: () => v4() },
|
||||
username: { type: String },
|
||||
name: { type: String },
|
||||
birthday: { type: Date, optional: true },
|
||||
gender: { type: Number },
|
||||
admin: { type: Boolean },
|
||||
password: { type: String },
|
||||
salt: { type: String },
|
||||
mails: { type: Array, default: () => [] },
|
||||
phones: {
|
||||
array: true,
|
||||
model: true,
|
||||
type: {
|
||||
phone: { type: String },
|
||||
verified: { type: Boolean },
|
||||
primary: { type: Boolean },
|
||||
},
|
||||
},
|
||||
twofactor: {
|
||||
array: true,
|
||||
model: true,
|
||||
type: {
|
||||
token: { type: String },
|
||||
valid: { type: Boolean },
|
||||
type: { type: Number },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
migration: (e: IUser) => {
|
||||
e.encryption_key = randomString(64);
|
||||
},
|
||||
schema: {
|
||||
uid: { type: String, default: () => v4() },
|
||||
username: { type: String },
|
||||
name: { type: String },
|
||||
birthday: { type: Date, optional: true },
|
||||
gender: { type: Number },
|
||||
admin: { type: Boolean },
|
||||
password: { type: String },
|
||||
salt: { type: String },
|
||||
mails: { type: Array, default: () => [] },
|
||||
phones: {
|
||||
array: true,
|
||||
model: true,
|
||||
type: {
|
||||
phone: { type: String },
|
||||
verified: { type: Boolean },
|
||||
primary: { type: Boolean },
|
||||
},
|
||||
},
|
||||
twofactor: {
|
||||
array: true,
|
||||
model: true,
|
||||
type: {
|
||||
token: { type: String },
|
||||
valid: { type: Boolean },
|
||||
type: { type: Number },
|
||||
},
|
||||
},
|
||||
encryption_key: {
|
||||
type: String,
|
||||
default: () => randomString(64),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
migration: (e: any) => {
|
||||
delete e.twofactor;
|
||||
},
|
||||
schema: {
|
||||
uid: { type: String, default: () => v4() },
|
||||
username: { type: String },
|
||||
name: { type: String },
|
||||
birthday: { type: Date, optional: true },
|
||||
gender: { type: Number },
|
||||
admin: { type: Boolean },
|
||||
password: { type: String },
|
||||
salt: { type: String },
|
||||
mails: { type: Array, default: () => [] },
|
||||
phones: {
|
||||
array: true,
|
||||
model: true,
|
||||
type: {
|
||||
phone: { type: String },
|
||||
verified: { type: Boolean },
|
||||
primary: { type: Boolean },
|
||||
},
|
||||
},
|
||||
encryption_key: {
|
||||
type: String,
|
||||
default: () => randomString(64),
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default User;
|
Reference in New Issue
Block a user