2024-10-01 13:49:18 +02:00
|
|
|
import * as plugins from '../plugins.js';
|
2024-09-29 13:56:38 +02:00
|
|
|
import { JwtManager } from './classes.jwtmanager.js';
|
2026-04-20 08:12:07 +00:00
|
|
|
import type { LoginSession } from './classes.loginsession.js';
|
2024-09-29 13:56:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* a User is identified by its username or email.
|
|
|
|
|
* Both need to be unique and both can be changed.
|
|
|
|
|
*/
|
|
|
|
|
@plugins.smartdata.Manager()
|
2024-10-07 10:26:21 +02:00
|
|
|
export class Jwt extends plugins.smartdata.SmartDataDbDoc<Jwt, plugins.idpInterfaces.data.IJwt, JwtManager> {
|
2024-09-29 13:56:38 +02:00
|
|
|
// STATIC
|
|
|
|
|
public static async createJwtForRefreshToken(
|
|
|
|
|
jwtManagerInstance: JwtManager,
|
|
|
|
|
refreshTokenArg: string
|
2026-04-20 08:12:07 +00:00
|
|
|
): Promise<string | null> {
|
|
|
|
|
const sessionLookup =
|
|
|
|
|
await jwtManagerInstance.receptionRef.loginSessionManager.findLoginSessionByRefreshToken(
|
2024-09-29 13:56:38 +02:00
|
|
|
refreshTokenArg
|
|
|
|
|
);
|
2026-04-20 08:12:07 +00:00
|
|
|
if (!sessionLookup || sessionLookup.validationStatus !== 'current') {
|
2024-09-29 13:56:38 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2026-04-20 08:12:07 +00:00
|
|
|
return this.createJwtForLoginSession(jwtManagerInstance, sessionLookup.loginSession);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async createJwtForLoginSession(
|
|
|
|
|
jwtManagerInstance: JwtManager,
|
|
|
|
|
loginSession: LoginSession
|
|
|
|
|
): Promise<string | null> {
|
2024-09-29 13:56:38 +02:00
|
|
|
const user = await jwtManagerInstance.receptionRef.userManager.CUser.getInstance({
|
|
|
|
|
id: loginSession.data.userId,
|
|
|
|
|
});
|
2026-04-20 08:12:07 +00:00
|
|
|
if (!user) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-09-29 13:56:38 +02:00
|
|
|
const validUntil = plugins.smarttime.ExtendedDate.fromMillis(
|
|
|
|
|
Date.now() + plugins.smarttime.getMilliSecondsFromUnits({ days: 1 })
|
|
|
|
|
);
|
|
|
|
|
const jwt = new Jwt();
|
|
|
|
|
jwt.id = plugins.smartunique.shortId();
|
|
|
|
|
jwt.data = {
|
|
|
|
|
userId: user.id,
|
2026-04-20 08:12:07 +00:00
|
|
|
sessionId: loginSession.id,
|
2024-09-29 13:56:38 +02:00
|
|
|
validUntil: validUntil.getTime(),
|
|
|
|
|
refreshEvery: 1000000,
|
|
|
|
|
refreshFrom: Date.now() + plugins.smarttime.getMilliSecondsFromUnits({ days: 0.5 }),
|
|
|
|
|
justForLooks: {
|
|
|
|
|
validUntilIsoString: validUntil.toISOString(),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await jwt.save();
|
|
|
|
|
|
|
|
|
|
const jwtString = await jwtManagerInstance.smartjwtInstance.createJWT({
|
|
|
|
|
id: jwt.id,
|
2026-04-20 08:12:07 +00:00
|
|
|
blocked: false,
|
2024-09-29 13:56:38 +02:00
|
|
|
data: jwt.data,
|
2024-10-07 10:26:21 +02:00
|
|
|
} as plugins.idpInterfaces.data.IJwt);
|
2024-09-29 13:56:38 +02:00
|
|
|
return jwtString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
|
@plugins.smartdata.unI()
|
|
|
|
|
public id: string;
|
|
|
|
|
|
|
|
|
|
@plugins.smartdata.svDb()
|
|
|
|
|
public blocked: boolean = false;
|
|
|
|
|
|
|
|
|
|
@plugins.smartdata.svDb()
|
2024-10-07 10:26:21 +02:00
|
|
|
public data: plugins.idpInterfaces.data.IJwt['data'];
|
2024-09-29 13:56:38 +02:00
|
|
|
|
|
|
|
|
public async block() {
|
|
|
|
|
this.blocked = true;
|
|
|
|
|
await this.save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async getLoginSession() {
|
2026-04-20 08:12:07 +00:00
|
|
|
if (this.data.sessionId) {
|
|
|
|
|
return this.manager.receptionRef.loginSessionManager.CLoginSession.getInstance({
|
|
|
|
|
id: this.data.sessionId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.data.refreshToken) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sessionLookup =
|
|
|
|
|
await this.manager.receptionRef.loginSessionManager.findLoginSessionByRefreshToken(
|
|
|
|
|
this.data.refreshToken
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!sessionLookup) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sessionLookup.loginSession;
|
|
|
|
|
|
2024-09-29 13:56:38 +02:00
|
|
|
}
|
|
|
|
|
}
|