Skip to content

Commit

Permalink
feat: add token reset as join:delete (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 17, 2018
1 parent 57302ff commit 8da0341
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/controller/SessionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class SessionController extends BaseController<SessionControllerData> imp
switch (cmd.verb) {
case CommandVerb.Create:
return this.createJoin(cmd);
case CommandVerb.Delete:
return this.deleteJoin(cmd);
default:
return this.reply(cmd.context, `unsupported verb: ${cmd.verb}`);
}
Expand Down Expand Up @@ -130,23 +132,24 @@ export class SessionController extends BaseController<SessionControllerData> imp
name,
roles,
}));
const now = this.clock.getSeconds();
const token = await this.tokenRepository.save(new Token({
audience: this.data.token.audience,
createdAt: now,
data: {},
expiresAt: now + this.data.token.duration,
grants: this.data.join.grants,
issuer: this.data.token.issuer,
labels: {},
subject: user.id,
user,
}));
const jwt = token.sign(this.data.token.secret);

const jwt = await this.createToken(user);
return this.reply(cmd.context, `user ${name} joined, signin token: ${jwt}`);
}

public async deleteJoin(cmd: Command): Promise<void> {
if (!cmd.context.user) {
return this.reply(cmd.context, 'must be logged in');
}

await this.tokenRepository.delete({
subject: cmd.context.user.id,
});

const jwt = await this.createToken(cmd.context.user);
return this.reply(cmd.context, `revoked tokens for ${cmd.context.user.name}, new signin token: ${jwt}`);
}

public async createSession(cmd: Command): Promise<void> {
const jwt = cmd.getHead('token');
const token = Token.verify(jwt, this.data.token.secret, {
Expand Down Expand Up @@ -174,4 +177,20 @@ export class SessionController extends BaseController<SessionControllerData> imp

return this.reply(cmd.context, session.toString());
}

protected async createToken(user: User): Promise<string> {
const now = this.clock.getSeconds();
const token = await this.tokenRepository.save(new Token({
audience: this.data.token.audience,
createdAt: now,
data: {},
expiresAt: now + this.data.token.duration,
grants: this.data.join.grants,
issuer: this.data.token.issuer,
labels: {},
subject: user.id,
user,
}));
return token.sign(this.data.token.secret);
}
}

0 comments on commit 8da0341

Please sign in to comment.