Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 'generate-token' to create identity-specific tokens. #505

Merged
merged 1 commit into from
Jul 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions schemas/verification-token-generate.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"expiresIn": {
"description": "Specify how long the token is valid for, in seconds.",
"type": "number"
},
"identity": {
"description": "If set, the token will be associated with a specific email address or phone number. When this token is validated later, the email address or phone number will be marked as 'verified' for the user.",
"type": "string",
"format": "uri"
}
}
}
4 changes: 4 additions & 0 deletions src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,8 @@ export interface VerificationTokenGenerateRequest {
* Specify how long the token is valid for, in seconds.
*/
expiresIn?: number;
/**
* If set, the token will be associated with a specific email address or phone number. When this token is validated later, the email address or phone number will be marked as 'verified' for the user.
*/
identity?: string;
}
2 changes: 2 additions & 0 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * as privilege from './privilege/service.js';
export * as resetPassword from './reset-password/service.js';
export * as log from './log/service.js';
export * as appClient from './app-client/service.js';
export * as oauth2 from './oauth2/service.js';
export * as verificationToken from './verification-token/service.js';
15 changes: 10 additions & 5 deletions src/verification-token/controller/generate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Controller from '@curveball/controller';
import { Context } from '@curveball/core';
import { PrincipalService } from '../../principal/service.js';
import { createToken } from '../service.js';
import * as hal from '../formats/hal.js';
import { resolve } from 'url';
import { VerificationTokenGenerateRequest } from '../../api-types.js';
import * as services from '../../services.js';

class OneTimeTokenController extends Controller {

Expand All @@ -13,13 +12,19 @@ class OneTimeTokenController extends Controller {
ctx.request.validate<VerificationTokenGenerateRequest>('https://curveballjs.org/schemas/a12nserver/verification-token-generate.json');
ctx.privileges.require('a12n:one-time-token:generate');

const principalService = new PrincipalService(ctx.privileges);
const principalService = new services.principal.PrincipalService(ctx.privileges);
const user = await principalService.findByExternalId(ctx.params.id, 'user');

const token = await createToken(
let identity = null;

if (ctx.request.body.identity) {
identity = await services.principalIdentity.findByUri(user, ctx.request.body.identity);
}

const token = await services.verificationToken.createToken(
user,
ctx.request.body.expiresIn ?? null,
null,
identity,
);
const url = resolve(ctx.request.origin, 'reset-password/token/' + token.token);

Expand Down
Loading