-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow otpUrl to generate TOTP/HOTP tokens
- Loading branch information
Corentin Mors
committed
Nov 19, 2024
1 parent
bcfde0b
commit 5b1db81
Showing
6 changed files
with
125 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { decryptTransaction, decryptTransactions } from './decrypt.js'; | ||
export { getLocalConfiguration } from './keychainManager.js'; | ||
export * from './otpauth.js'; | ||
export * from './xor.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { authenticator, hotp } from 'otplib'; | ||
|
||
enum HashAlgorithms { | ||
'SHA1' = 'sha1', | ||
'SHA256' = 'sha256', | ||
'SHA512' = 'sha512', | ||
} | ||
|
||
interface Otpauth { | ||
protocol: string; | ||
type: string; | ||
issuer: string; | ||
secret: string; | ||
algorithm: HashAlgorithms; | ||
digits: number; | ||
period: number; | ||
counter: number; | ||
} | ||
|
||
const matchAlgorithm = (algorithm: string): HashAlgorithms => { | ||
switch (algorithm) { | ||
case 'SHA1': | ||
return HashAlgorithms.SHA1; | ||
case 'SHA256': | ||
return HashAlgorithms.SHA256; | ||
case 'SHA512': | ||
return HashAlgorithms.SHA512; | ||
default: | ||
throw new Error('Invalid algorithm'); | ||
} | ||
}; | ||
|
||
const parseOtpauth = (uri: string): Otpauth => { | ||
const url = new URL(uri); | ||
const searchParams = url.searchParams; | ||
|
||
return { | ||
protocol: url.protocol.slice(0, -1), | ||
type: url.hostname, | ||
issuer: searchParams.get('issuer') ?? '', | ||
secret: searchParams.get('secret') ?? '', | ||
algorithm: matchAlgorithm(searchParams.get('algorithm') ?? 'sha1'), | ||
digits: Number(searchParams.get('digits') ?? 0), | ||
period: Number(searchParams.get('period') ?? 0), | ||
counter: Number(searchParams.get('counter') ?? 0), | ||
}; | ||
}; | ||
|
||
export const generateOtpFromUri = (uri: string): { token: string; remainingTime: number | null } => { | ||
const otpauth = parseOtpauth(uri); | ||
|
||
authenticator.resetOptions(); | ||
hotp.resetOptions(); | ||
|
||
switch (otpauth.type) { | ||
case 'totp': | ||
authenticator.options = { | ||
algorithm: otpauth.algorithm, | ||
digits: otpauth.digits, | ||
period: otpauth.period, | ||
}; | ||
return { token: authenticator.generate(otpauth.secret), remainingTime: authenticator.timeRemaining() }; | ||
case 'hotp': | ||
hotp.options = { algorithm: otpauth.algorithm, digits: otpauth.digits }; | ||
return { token: hotp.generate(otpauth.secret, otpauth.counter), remainingTime: null }; | ||
default: | ||
throw new Error('Invalid OTP type'); | ||
} | ||
}; | ||
|
||
export const generateOtpFromSecret = (secret: string): { token: string; remainingTime: number | null } => { | ||
authenticator.resetOptions(); | ||
return { token: authenticator.generate(secret), remainingTime: authenticator.timeRemaining() }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters