Skip to content

Commit

Permalink
add approval expiration logic to multisig endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Oct 4, 2023
1 parent ddd35fe commit e3e2ffa
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export class AddExpirationDateResetToMultisigSession1696397559095
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumn(
'multisig_session',
new TableColumn({
name: 'approvalExpirationDate',
type: 'date',
isNullable: true,
}),
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumn('multisig_session', 'approvalExpirationDate');
}
}
21 changes: 20 additions & 1 deletion src/controllers/v1/multisigAuthenticationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class MultisigAuthenticationController {
body.network,
);

if (!multisigSession && !body.approvalExpirationDays) {
throw new StandardError(errorMessagesEnum.MULTISIG_INVALID_REQUEST);
}

if (!multisigSession && body.safeMessageTimestamp) {
safeMessage = await fetchSafeMessageByTimestamp(
body.safeAddress,
Expand Down Expand Up @@ -73,6 +77,9 @@ export class MultisigAuthenticationController {

if (!multisigSession) {
multisigSession = MultisigSession.create({
approvalExpirationDate: moment()
.add(body.approvalExpirationDays, 'days')
.toDate(),
expirationDate: moment().add(1, 'week').toDate(),
safeMessageHash: safeMessage.messageHash,
multisigAddress: body.safeAddress,
Expand All @@ -85,7 +92,19 @@ export class MultisigAuthenticationController {
(await multisigSession.multisigStatus(safeMessage)) ===
MultisigStatuses.Successful
) {
token = await generateAccessToken({ address: body.safeAddress });
if (
multisigSession.expirationDate !==
multisigSession.approvalExpirationDAte
) {
multisigSession.expirationDate =
multisigSession.approvalExpirationDate;
await multisigSession.save();
}

token = await generateAccessToken({
address: body.safeAddress,
expirationDate: multisigSession.expirationDate,
});
logger.info(`Multisig with address ${body.safeAddress} logged in`);
}

Expand Down
3 changes: 3 additions & 0 deletions src/entities/multisigSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export class MultisigSession extends BaseEntity {
@Column({ nullable: false })
expirationDate: Date;

@Column({ nullable: true })
approvalExpirationDate: Date;

didExpire(): boolean {
return this.expirationDate.valueOf() < moment().valueOf();
}
Expand Down
2 changes: 2 additions & 0 deletions src/routes/v1/multisigAuthenticationRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ multisigAuthenticationRouter.post(
try {
const { safeAddress, network, jwt } = req.body;
const safeMessageTimestamp = req.body?.safeMessageTimestamp;
const approvalExpirationDays = req.body?.approvalExpirationDays;
if (!safeAddress || !jwt || !network) {
res.status(422).json({ message: errorMessagesEnum.MISSING_LOGIN_DATA });
return;
}

const result = await multisigAuthenticationController.authenticate({
approvalExpirationDays,
safeMessageTimestamp,
safeAddress,
network,
Expand Down
10 changes: 8 additions & 2 deletions src/services/authenticationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ import { generateJwt, JwtPayload } from './jwtService';

interface AccessTokenFields {
address: string;
expirationDate?: Date;
}

export const generateAccessToken = async (
fields: AccessTokenFields,
isPassport = false,
): Promise<AccessToken> => {
const jti = `${new Date().getTime()}-${generateRandomString(5)}`;
let expirationDate: Date;

const expirationDate = moment().add(30, 'days');
if (fields.expirationDate) {
expirationDate = fields.expirationDate;
} else {
expirationDate = moment().add(30, 'days').toDate();
}

const jwtPayload: JwtPayload = {
publicAddress: fields.address,
expirationDate: expirationDate.toDate(),
expirationDate: expirationDate,
jti: jti,
};
if (isPassport) {
Expand Down
1 change: 1 addition & 0 deletions src/types/requestResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type AuthenticationRequest = {

export type MultisigAuthenticationRequest = {
safeMessageTimestamp?: number;
approvalExpirationDays?: number;
safeAddress: string;
network: number;
jwt: string;
Expand Down

0 comments on commit e3e2ffa

Please sign in to comment.