Skip to content

Commit

Permalink
Add otp expiration transformation (#172)
Browse files Browse the repository at this point in the history
Fix #167
  • Loading branch information
Mikescops committed Aug 17, 2023
1 parent 3209147 commit 1cdbfef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions documentation/pages/personal/secrets/read.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The transformer is a function that will be applied to the secret field before lo
Available transformers:

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

Expand Down
5 changes: 4 additions & 1 deletion src/utils/secretPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isUuid } from './strings';
import { transformJsonPath, transformOtp } from './secretTransformation';
import { transformJsonPath, transformOtp, transformOtpAndExpiry } from './secretTransformation';
import { ParsedPath } from '../types';
import { InvalidDashlanePathError } from '../errors';

Expand Down Expand Up @@ -52,6 +52,9 @@ export const parsePath = (path: string): ParsedPath => {
case 'otp':
transformation = transformOtp;
break;
case 'otp+expiry':
transformation = transformOtpAndExpiry;
break;
case 'json':
transformation = (json: string) => transformJsonPath(json, queryParamValue);
break;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/secretTransformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export const transformOtp = (secret: string) => {
return authenticator.generate(secret);
};

export const transformOtpAndExpiry = (secret: string) => {
const otp = authenticator.generate(secret);
const expiry = authenticator.timeRemaining();
return `${otp} ${expiry}`;
};

export const transformJsonPath = (json: string, path: string) => {
const result = JSONPath<unknown>({ path, json: JSON.parse(json) as object, wrap: false });
if (result === undefined) {
Expand Down

0 comments on commit 1cdbfef

Please sign in to comment.