@ -1,21 +1,83 @@
|
||||
import { objectOmit } from '@dzeio/object-util'
|
||||
import mongoose from 'mongoose'
|
||||
import type Config from '.'
|
||||
import Client from '../Client'
|
||||
import Dao from '../Dao'
|
||||
|
||||
export default class ConfigDao extends Dao<Config> {
|
||||
private idx = 0
|
||||
public async create(obj: Omit<Config, 'id'>): Promise<Config | null> {
|
||||
console.log('pouet', this.idx++)
|
||||
return null
|
||||
// throw new Error('Method not implemented.')
|
||||
|
||||
// @ts-expect-error typing fix
|
||||
private model = mongoose.models['Config'] as null ?? mongoose.model('Config', new mongoose.Schema({
|
||||
user: { type: String, required: true },
|
||||
type: { type: String, required: true},
|
||||
files: [{
|
||||
name: { type: String, unique: true, required: true},
|
||||
data: { type: Buffer, required: true }
|
||||
}]
|
||||
}, {
|
||||
timestamps: true
|
||||
}))
|
||||
|
||||
|
||||
public async create(obj: Omit<Config, 'id' | 'created' | 'updated'>): Promise<Config | null> {
|
||||
await Client.get()
|
||||
return this.fromSource(await this.model.create(obj))
|
||||
}
|
||||
|
||||
public async findAll(query?: Partial<Config> | undefined): Promise<Config[]> {
|
||||
throw new Error('Method not implemented.')
|
||||
await Client.get()
|
||||
try {
|
||||
if (query?.id) {
|
||||
const item = await this.model.findById(new mongoose.Types.ObjectId(query.id))
|
||||
if (!item) {
|
||||
return []
|
||||
}
|
||||
return [this.fromSource(item)]
|
||||
}
|
||||
const resp = await this.model.find(query ? this.toSource(query as Config) : {})
|
||||
return resp.map(this.fromSource)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return []
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async update(obj: Config): Promise<Config | null> {
|
||||
throw new Error('Method not implemented.')
|
||||
await Client.get()
|
||||
|
||||
const query = await this.model.updateOne({
|
||||
_id: new mongoose.Types.ObjectId(obj.id)
|
||||
}, this.toSource(obj))
|
||||
if (query.matchedCount >= 1) {
|
||||
obj.updated = new Date()
|
||||
return obj
|
||||
}
|
||||
return null
|
||||
// return this.fromSource()
|
||||
}
|
||||
|
||||
public async delete(obj: Config): Promise<boolean> {
|
||||
throw new Error('Method not implemented.')
|
||||
await Client.get()
|
||||
const res = await this.model.deleteOne({
|
||||
_id: new mongoose.Types.ObjectId(obj.id)
|
||||
})
|
||||
return res.deletedCount > 0
|
||||
}
|
||||
|
||||
private toSource(obj: Config): Omit<Config, 'id'> {
|
||||
return objectOmit(obj, 'id', 'updated', 'created')
|
||||
}
|
||||
|
||||
private fromSource(doc: mongoose.Document<any, any, Config>): Config {
|
||||
return {
|
||||
id: doc._id.toString(),
|
||||
user: doc.get('user'),
|
||||
type: doc.get('type'),
|
||||
files: doc.get('files') ?? [],
|
||||
updated: doc.get('updatedAt'),
|
||||
created: doc.get('createdAt')
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
import type User from '../User'
|
||||
|
||||
export default interface Config {
|
||||
id: string
|
||||
user: string
|
||||
type: 'prusa'
|
||||
files: Array<{
|
||||
name: string
|
||||
data: Buffer
|
||||
}>
|
||||
created: Date
|
||||
updated: Date
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import APIKeyDao from './APIKey/APIKeyDao'
|
||||
import ConfigDao from './Config/ConfigDao'
|
||||
import Dao from './Dao'
|
||||
import SessionDao from './Session/SessionDao'
|
||||
import UserDao from './User/UserDao'
|
||||
|
||||
/**
|
||||
@ -18,6 +18,7 @@ interface DaoItem {
|
||||
config: ConfigDao
|
||||
user: UserDao
|
||||
apiKey: APIKeyDao
|
||||
session: SessionDao
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,11 +55,12 @@ export default class DaoFactory {
|
||||
* @param item the element to init
|
||||
* @returns a new initialized dao or undefined if no dao is linked
|
||||
*/
|
||||
private static initDao(item: keyof DaoItem): Dao<any> | undefined {
|
||||
private static initDao(item: keyof DaoItem): any | undefined {
|
||||
switch (item) {
|
||||
case 'config': return new ConfigDao()
|
||||
case 'user': return new UserDao()
|
||||
case 'apiKey': return new APIKeyDao()
|
||||
case 'session': return new SessionDao()
|
||||
default: return undefined
|
||||
}
|
||||
}
|
||||
|
53
src/models/Session/SessionDao.ts
Normal file
53
src/models/Session/SessionDao.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import jwt, { SignOptions } from 'jsonwebtoken'
|
||||
import type Session from '.'
|
||||
import CookieManager from '../../libs/CookieManager'
|
||||
|
||||
export interface SessionOptions {
|
||||
cookieName: string
|
||||
security: SignOptions
|
||||
key?: string
|
||||
privateKey?: string
|
||||
publicKey?: string
|
||||
}
|
||||
|
||||
|
||||
export default class SessionDao {
|
||||
|
||||
private options: SessionOptions = {
|
||||
cookieName: 'session',
|
||||
security: {
|
||||
algorithm: 'ES512'
|
||||
},
|
||||
privateKey: import.meta.env.PRIVATE_KEY ?? '',
|
||||
publicKey: import.meta.env.PUBLIC_KEY ?? ''
|
||||
}
|
||||
|
||||
public getSession(req: Request): Session | null {
|
||||
const cookie = new CookieManager(req.headers.get('Cookie') ?? '').get(this.options.cookieName)
|
||||
if (!cookie) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return jwt.verify(cookie, (this.options.publicKey || this.options.key) as string) as Session
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
public setSession(session: Session, res: ResponseInit & { readonly headers: Headers; }) {
|
||||
const token = jwt.sign(session, (this.options.privateKey || this.options.key) as string, this.options.security)
|
||||
CookieManager.addCookie(res, {
|
||||
key: this.options.cookieName,
|
||||
value: token,
|
||||
httpOnly: true,
|
||||
path: '/',
|
||||
secure: true,
|
||||
sameSite: 'Strict',
|
||||
maxAge: 365000
|
||||
})
|
||||
}
|
||||
|
||||
public removeSession(res: ResponseInit & { readonly headers: Headers; }) {
|
||||
|
||||
}
|
||||
}
|
3
src/models/Session/index.ts
Normal file
3
src/models/Session/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export default interface Session {
|
||||
userId: string
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { objectOmit } from '@dzeio/object-util'
|
||||
import mongoose, { ObjectId } from 'mongoose'
|
||||
import User from '.'
|
||||
import mongoose from 'mongoose'
|
||||
import type User from '.'
|
||||
import Client from '../Client'
|
||||
import Dao from '../Dao'
|
||||
|
||||
@ -8,7 +8,8 @@ export default class UserDao extends Dao<User> {
|
||||
|
||||
// @ts-expect-error typing fix
|
||||
private model = mongoose.models['User'] as null ?? mongoose.model('User', new mongoose.Schema({
|
||||
email: { type: String, required: true }
|
||||
email: { type: String, required: true },
|
||||
password: { type: String, required: true }
|
||||
}, {
|
||||
timestamps: true
|
||||
}))
|
||||
@ -62,6 +63,7 @@ export default class UserDao extends Dao<User> {
|
||||
return {
|
||||
id: doc._id.toString(),
|
||||
email: doc.get('email'),
|
||||
password: doc.get('password'),
|
||||
updated: doc.get('updatedAt'),
|
||||
created: doc.get('createdAt')
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
export default interface User {
|
||||
id: string
|
||||
email: string
|
||||
password: string
|
||||
created: Date
|
||||
updated: Date
|
||||
}
|
||||
|
Reference in New Issue
Block a user