Skip to content

Commit d829d65

Browse files
committed
fix: correct overly short token expiration timestamps
1 parent 3eeb7be commit d829d65

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

src/controller/SessionController.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class SessionController extends BaseController<SessionControllerData> imp
134134
}));
135135

136136
const jwt = await this.createToken(user);
137-
return this.reply(cmd.context, `user ${name} joined, signin token: ${jwt}`);
137+
return this.reply(cmd.context, `user ${name} joined, sign in token: ${jwt}`);
138138
}
139139

140140
public async deleteJoin(cmd: Command): Promise<void> {
@@ -153,30 +153,35 @@ export class SessionController extends BaseController<SessionControllerData> imp
153153
});
154154

155155
const jwt = await this.createToken(cmd.context.user);
156-
return this.reply(cmd.context, `revoked tokens for ${cmd.context.user.name}, new signin token: ${jwt}`);
156+
return this.reply(cmd.context, `revoked tokens for ${cmd.context.user.name}, new sign in token: ${jwt}`);
157157
}
158158

159159
public async createSession(cmd: Command): Promise<void> {
160160
if (!cmd.context.source) {
161161
return this.reply(cmd.context, 'no source listener with which to create a session');
162162
}
163163

164-
const jwt = cmd.getHead('token');
165-
const token = Token.verify(jwt, this.data.token.secret, {
166-
audience: this.data.token.audience,
167-
issuer: this.data.token.issuer,
168-
});
169-
this.logger.debug({ token }, 'creating session from token');
170-
171-
const user = await this.userRepository.findOneOrFail({
172-
id: token.sub,
173-
});
174-
await this.userRepository.loadRoles(user);
175-
this.logger.debug({ user }, 'logging in user');
176-
177-
const session = await cmd.context.source.createSession(cmd.context.uid, user);
178-
this.logger.debug({ session, user }, 'created session');
179-
return this.reply(cmd.context, 'created session');
164+
try {
165+
const jwt = cmd.getHead('token');
166+
const token = Token.verify(jwt, this.data.token.secret, {
167+
audience: this.data.token.audience,
168+
issuer: this.data.token.issuer,
169+
});
170+
this.logger.debug({ token }, 'creating session from token');
171+
172+
const user = await this.userRepository.findOneOrFail({
173+
id: token.sub,
174+
});
175+
await this.userRepository.loadRoles(user);
176+
this.logger.debug({ user }, 'logging in user');
177+
178+
const session = await cmd.context.source.createSession(cmd.context.uid, user);
179+
this.logger.debug({ session, user }, 'created session');
180+
return this.reply(cmd.context, 'created session');
181+
} catch (err) {
182+
this.logger.error(err, 'error creating session');
183+
return this.reply(cmd.context, err.message);
184+
}
180185
}
181186

182187
public async getSession(cmd: Command): Promise<void> {
@@ -193,19 +198,24 @@ export class SessionController extends BaseController<SessionControllerData> imp
193198
}
194199

195200
protected async createToken(user: User): Promise<string> {
196-
const now = this.clock.getSeconds();
197-
const token = await this.tokenRepository.save(new Token({
201+
const issued = this.clock.getSeconds();
202+
const expires = issued + this.data.token.duration;
203+
this.logger.debug({ expires, issued }, 'creating token');
204+
205+
const tokenPre = new Token({
198206
audience: this.data.token.audience,
199-
createdAt: this.clock.getDate(now),
207+
createdAt: this.clock.getDate(issued),
200208
data: {},
201-
expiresAt: this.clock.getDate(now + this.data.token.duration),
209+
expiresAt: this.clock.getDate(expires),
202210
grants: this.data.join.grants,
203211
issuer: this.data.token.issuer,
204212
labels: {},
205213
subject: user.id,
206214
user,
207-
}));
208-
this.logger.debug({ token }, 'signing token');
215+
});
216+
217+
const token = await this.tokenRepository.save(tokenPre);
218+
this.logger.debug({ expires, issued, token }, 'signing token');
209219
return token.sign(this.data.token.secret);
210220
}
211221
}

src/entity/auth/Token.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export class Token extends DataEntity<Array<string>> implements TokenOptions {
100100

101101
if (options) {
102102
this.audience = options.audience;
103+
this.createdAt = options.createdAt;
103104
this.expiresAt = options.expiresAt;
104105
this.issuer = options.issuer;
105106
this.grants = Array.from(options.grants);

src/utils/Clock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class Clock {
2323
if (isNil(seconds)) {
2424
return new Date();
2525
} else {
26-
return new Date(seconds);
26+
return new Date(seconds * NOW_TO_SECONDS);
2727
}
2828
}
2929

0 commit comments

Comments
 (0)