-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added hexDigest bypass feat: remove guard in favour of js thrown err chore: standardise export name and allOptions chore: some code style changes feat: add async versions of core lib feat: added async tests and async plugin chore: mv code to utils and ignore internal export fix: interface inheritance chore: tests should all run as async
- Loading branch information
Showing
52 changed files
with
2,241 additions
and
1,338 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-typescript" | ||
] | ||
"presets": ["@babel/preset-env", "@babel/preset-typescript"], | ||
"plugins": ["@babel/plugin-transform-runtime"] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Authenticator } from 'otplib-authenticator'; | ||
import { testSuiteAuthenticator } from 'tests-suites/core-authenticator'; | ||
import { testClassPropertiesEqual } from 'tests-suites/helpers'; | ||
import { AuthenticatorAsync } from './authenticator'; | ||
|
||
testClassPropertiesEqual<Authenticator, AuthenticatorAsync>( | ||
Authenticator.name, | ||
new Authenticator(), | ||
AuthenticatorAsync.name, | ||
new AuthenticatorAsync() | ||
); | ||
|
||
testSuiteAuthenticator<AuthenticatorAsync>( | ||
'authenticator-async', | ||
AuthenticatorAsync | ||
); |
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,107 @@ | ||
import { HexString, SecretKey } from 'otplib-hotp'; | ||
import { | ||
AuthenticatorOptions, | ||
Base32SecretKey, | ||
authenticatorDecoder, | ||
authenticatorEncoder | ||
} from 'otplib-authenticator'; | ||
import { | ||
TOTPAsync, | ||
totpCheckWithWindowAsync, | ||
totpDigestAsync | ||
} from 'otplib-totp-async'; | ||
import { authenticatorOptions, totpToken } from 'otplib-core'; | ||
|
||
/** | ||
* Allow AuthenticatorOptions to accept async method options. | ||
*/ | ||
export type AuthenticatorAsyncOptions = AuthenticatorOptions<Promise<string>>; | ||
|
||
/** | ||
* Generates the digest for Authenticator based tokens. | ||
* | ||
* Uses [[totpDigestAsync]]. | ||
*/ | ||
export async function authenticatorDigestAsync< | ||
T extends AuthenticatorAsyncOptions = AuthenticatorAsyncOptions | ||
>(secret: Base32SecretKey, options: Readonly<T>): Promise<HexString> { | ||
const decodedSecret = await authenticatorDecoder<T>(secret, options); | ||
return totpDigestAsync<T>(decodedSecret, options); | ||
} | ||
|
||
/** | ||
* Async version of [[authenticatorToken]]. | ||
*/ | ||
export async function authenticatorTokenAsync< | ||
T extends AuthenticatorAsyncOptions = AuthenticatorAsyncOptions | ||
>(secret: Base32SecretKey, options: Readonly<T>): Promise<string> { | ||
const digest = await authenticatorDigestAsync<T>(secret, options); | ||
return totpToken<T>(secret, { ...options, digest }); | ||
} | ||
|
||
/** | ||
* Async version of [[authenticatorCheckWithWindow]]. | ||
*/ | ||
export async function authenticatorCheckWithWindowAsync< | ||
T extends AuthenticatorAsyncOptions = AuthenticatorAsyncOptions | ||
>( | ||
token: string, | ||
secret: Base32SecretKey, | ||
options: Readonly<T> | ||
): Promise<number | null> { | ||
const decodedSecret = await authenticatorDecoder<T>(secret, options); | ||
return totpCheckWithWindowAsync<T>(token, decodedSecret, options); | ||
} | ||
|
||
export async function authenticatorGenerateSecretAsync< | ||
T extends AuthenticatorAsyncOptions = AuthenticatorAsyncOptions | ||
>( | ||
numberOfBytes: number, | ||
options: Pick<T, 'keyEncoder' | 'encoding' | 'createRandomBytes'> | ||
): Promise<Base32SecretKey> { | ||
const key = await options.createRandomBytes(numberOfBytes, options.encoding); | ||
return authenticatorEncoder<T>(key, options); | ||
} | ||
|
||
/** | ||
* Async version of [[Authenticator]]. | ||
*/ | ||
export class AuthenticatorAsync< | ||
T extends AuthenticatorAsyncOptions = AuthenticatorAsyncOptions | ||
> extends TOTPAsync<T> { | ||
public create(defaultOptions: Partial<T> = {}): AuthenticatorAsync<T> { | ||
return new AuthenticatorAsync<T>(defaultOptions); | ||
} | ||
|
||
public allOptions(): Readonly<T> { | ||
return authenticatorOptions<T>(this.options); | ||
} | ||
|
||
public async generate(secret: SecretKey): Promise<string> { | ||
return authenticatorTokenAsync<T>(secret, this.allOptions()); | ||
} | ||
|
||
public async checkDelta( | ||
token: string, | ||
secret: SecretKey | ||
): Promise<number | null> { | ||
return authenticatorCheckWithWindowAsync(token, secret, this.allOptions()); | ||
} | ||
|
||
public async encode(secret: SecretKey): Promise<Base32SecretKey> { | ||
return authenticatorEncoder<T>(secret, this.allOptions()); | ||
} | ||
|
||
public async decode(secret: Base32SecretKey): Promise<SecretKey> { | ||
return authenticatorDecoder<T>(secret, this.allOptions()); | ||
} | ||
|
||
public async generateSecret( | ||
numberOfBytes: number = 10 | ||
): Promise<Base32SecretKey> { | ||
return authenticatorGenerateSecretAsync<T>( | ||
numberOfBytes, | ||
this.allOptions() | ||
); | ||
} | ||
} |
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 @@ | ||
export * from './authenticator'; |
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
Oops, something went wrong.