Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/onefx-auth/src/model/jwt-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import jwt from "jsonwebtoken";
import jwt, { Secret, VerifyOptions } from "jsonwebtoken";
import mongoose from "mongoose";
import { promisify } from "util";

const { Schema } = mongoose;

const sign = promisify(jwt.sign);
const verify = promisify(jwt.verify);
const verify = promisify<string, Secret, VerifyOptions | undefined, AuthJwt>(
jwt.verify
);

type Opts = {
secret: string;
Expand Down Expand Up @@ -80,10 +82,21 @@ export class JwtModel {
);
}

async revokeOtherSessions(userId: string, token: string): Promise<void> {
let decoded;
try {
decoded = await verify(token, this.secret, undefined);
} catch (e) {
return undefined;
}
await this.Model.deleteMany({ userId, _id: { $ne: decoded.jti } });
return undefined;
}

public async revoke(token: string): Promise<void> {
let decoded;
try {
decoded = (await verify(token, this.secret)) as AuthJwt;
decoded = (await verify(token, this.secret, undefined)) as AuthJwt;
} catch (e) {
return;
}
Expand All @@ -93,7 +106,7 @@ export class JwtModel {
public async verify(token: string): Promise<UserId> {
let decoded: AuthJwt;
try {
decoded = (await verify(token, this.secret)) as AuthJwt;
decoded = (await verify(token, this.secret, undefined)) as AuthJwt;
} catch (e) {
return "";
}
Expand Down
6 changes: 6 additions & 0 deletions packages/onefx-auth/src/onefx-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ export class OnefxAuth {
ctx.redirect(allowedLogoutNext(this.config, ctx.query.next));
};

logoutOtherSessions = async (ctx: Context): Promise<void> => {
const token = this.tokenFromCtx(ctx);
const { userId } = ctx.state;
await this.jwt.revokeOtherSessions(userId, token);
};

public postAuthentication = async (ctx: Context): Promise<void> => {
if (!ctx.state.userId) {
return;
Expand Down