Skip to content

Commit 1cdbfef

Browse files
author
Corentin Mors
authored
Add otp expiration transformation (#172)
Fix #167
1 parent 3209147 commit 1cdbfef

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

documentation/pages/personal/secrets/read.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The transformer is a function that will be applied to the secret field before lo
5151
Available transformers:
5252

5353
- `?otp` to generate a one-time password from a secret key
54+
- `?otp+expiry` to generate a one-time password from a secret key and display its expiry date (in seconds)
5455
- `?json=<JSONPathQuery>` to extract a value from a JSON object
5556
(please refer to [JSONPath documentation](https://github.com/JSONPath-Plus/JSONPath#syntax-through-examples) for more information)
5657

src/utils/secretPath.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isUuid } from './strings';
2-
import { transformJsonPath, transformOtp } from './secretTransformation';
2+
import { transformJsonPath, transformOtp, transformOtpAndExpiry } from './secretTransformation';
33
import { ParsedPath } from '../types';
44
import { InvalidDashlanePathError } from '../errors';
55

@@ -52,6 +52,9 @@ export const parsePath = (path: string): ParsedPath => {
5252
case 'otp':
5353
transformation = transformOtp;
5454
break;
55+
case 'otp+expiry':
56+
transformation = transformOtpAndExpiry;
57+
break;
5558
case 'json':
5659
transformation = (json: string) => transformJsonPath(json, queryParamValue);
5760
break;

src/utils/secretTransformation.ts

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ export const transformOtp = (secret: string) => {
55
return authenticator.generate(secret);
66
};
77

8+
export const transformOtpAndExpiry = (secret: string) => {
9+
const otp = authenticator.generate(secret);
10+
const expiry = authenticator.timeRemaining();
11+
return `${otp} ${expiry}`;
12+
};
13+
814
export const transformJsonPath = (json: string, path: string) => {
915
const result = JSONPath<unknown>({ path, json: JSON.parse(json) as object, wrap: false });
1016
if (result === undefined) {

0 commit comments

Comments
 (0)