Skip to content

Commit

Permalink
feat: enhance typescript def
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Jun 12, 2018
1 parent ef8bd9e commit 9e02105
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ Before submitting a pull request, please make sure the following is done:

Thank you for contributing!

[pr-welcome-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[pr-welcome-badge]: https://img.shields.io/badge/welcome-PRs-brightgreen.svg?style=flat-square&longCache=true
[pr-welcome-link]: https://github.com/yeojz/otplib/blob/master/CONTRIBUTING.md
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# otplib

> Time-based (TOTP) and HMAC-based (HOTP) One-Time Password library
Expand All @@ -6,9 +7,8 @@
[![Build Status][circle-badge]][circle-link]
[![Coverage Status][coveralls-badge]][coveralls-link]
[![npm downloads][npm-downloads-badge]][npm-link]
[![PRs Welcome][pr-welcome-badge]][pr-welcome-link]
[![TypeScript Support][type-ts-badge]][type-ts-link]

<img width="150" src="https://yeojz.github.io/otplib/otplib.png" />

- [About](#about)
- [Demo and Documentation](#demo-and-documentation)
Expand Down Expand Up @@ -335,11 +335,14 @@ qrcode.toDataURL(otpauth, (err, imageUrl) => {
Check out: [CONTRIBUTING.md][pr-welcome-link]

[![Support Project][coffee-badge]][coffee-link]
[![PRs Welcome][pr-welcome-badge]][pr-welcome-link]

## License

`otplib` is [MIT licensed](./LICENSE)

<img width="150" src="https://yeojz.github.io/otplib/otplib.png" />


[npm-badge]: https://img.shields.io/npm/v/otplib.svg?style=flat-square
[npm-link]: https://www.npmjs.com/package/otplib
Expand All @@ -349,7 +352,7 @@ Check out: [CONTRIBUTING.md][pr-welcome-link]
[circle-link]: https://circleci.com/gh/yeojz/otplib
[coveralls-badge]: https://img.shields.io/coveralls/yeojz/otplib/master.svg?style=flat-square
[coveralls-link]: https://coveralls.io/github/yeojz/otplib
[pr-welcome-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[pr-welcome-badge]: https://img.shields.io/badge/welcome-PRs-brightgreen.svg?style=flat-square&longCache=true
[pr-welcome-link]: https://github.com/yeojz/otplib/blob/master/CONTRIBUTING.md
[mdn-uint8array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
[mdn-crypto]: https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
Expand All @@ -363,5 +366,7 @@ Check out: [CONTRIBUTING.md][pr-welcome-link]
[rfc-6238-wiki]: http://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
[donate-badge]: https://img.shields.io/badge/donate-%3C3-red.svg?longCache=true&style=flat-square
[donate-link]: https://www.paypal.me/yeojz
[coffee-badge]: https://img.shields.io/badge/%E2%98%95%EF%B8%8F%20%20-buy%20me%20a%20coffee-orange.svg?longCache=true&style=flat-square
[coffee-badge]: https://img.shields.io/badge/%E2%98%95%EF%B8%8F%20-buy%20me%20a%20coffee-orange.svg?longCache=true&style=flat-square
[coffee-link]: https://ko-fi.com/geraldyeo
[type-ts-badge]: https://img.shields.io/badge/included-.d.ts-blue.svg?style=flat-square&longCache=true
[type-ts-link]: https://github.com/yeojz/otplib/tree/master/packages/types-ts
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,17 @@
},
"jest": {
"coverageDirectory": "./coverage/",
"coveragePathIgnorePatterns": [
"<rootDir>/scripts/*",
"<rootDir>/dist/*"
],
"coveragePathIgnorePatterns": [],
"modulePaths": [
"<rootDir>/packages/"
],
"roots": [
"<rootDir>/packages/"
],
"resetMocks": true,
"setupFiles": [],
"testPathIgnorePatterns": [
"/node_modules/",
"<rootDir>/dist/*",
"<rootDir>/scripts/*",
"<rootDir>/tests/"
"/node_modules/"
]
},
"repl": [
Expand Down
5 changes: 4 additions & 1 deletion packages/otplib-core/hotpDigest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ function hotpDigest(secret, counter, options) {
}

// Convert secret to encoding for hmacSecret
const hmacSecret = options.createHmacSecret(secret, options);
const hmacSecret = options.createHmacSecret(secret, {
algorithm: options.algorithm,
encoding: options.encoding
});

// Ensure counter is a buffer or string (for HMAC creation)
const hexCounter = hotpCounter(counter);
Expand Down
2 changes: 1 addition & 1 deletion packages/otplib-core/totpCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Generates a counter for TOTP
*
* @module otplib-core/totpCounter
* @param {string} epoch - starting time since the UNIX epoch (seconds)
* @param {number} epoch - starting time since the UNIX epoch (seconds)
* @param {number} step - time step (seconds)
* @return {float}
*/
Expand Down
98 changes: 84 additions & 14 deletions packages/types-ts/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,103 @@
type CreateHmacSecretFunction = (secret: string, options: any) => Buffer;

interface HOTPOptions {
interface hmacOptions {
algorithm?: string;
createHmacSecret?: CreateHmacSecretFunction;
encoding?: string;
}

type createHmacSecret = (
secret: string,
options: hmacOptions
) => Buffer;

interface hotpOptionsInterface extends hmacOptions {
createHmacSecret?: createHmacSecret;
crypto?: any;
digits?: number;
encoding?: string;
}

interface HOTPVerifyOptions {
interface hotpVerifyOptionsInterface {
token?: string;
secret?: string;
counter?: number;
}

interface TOTPOptions extends HOTPOptions {
type hotpCheck = (
token: string,
secret: string,
counter: number,
options: hotpOptionsInterface
) => boolean;

type hotpCounter = (counter: number) => string;

type hotpDigest = (
secret: string,
counter: number,
options: hotpOptionsInterface
) => string;

type hotpOptions = (options: any) => hotpOptionsInterface;

type hotpSecret = createHmacSecret;

type hotpToken = (
secret: string,
counter: number,
options: hotpOptionsInterface
) => string;

interface totpOptionsInterface extends hotpOptionsInterface {
epoch?: any;
step?: number;
window?: number | number[];
}

interface TOTPVerifyOptions {
interface totpVerifyOptionsInterface {
token?: string;
secret?: string;
}

type totpCheck = (
token: string,
secret: string,
options: totpOptionsInterface
) => boolean;

type totpCheckWithWindow = (
token: string,
secret: string,
options: totpOptionsInterface
) => number | null;

type totpCounter = (epoch: number, step: number) => number;

type totpOptions = (options: any) => totpOptionsInterface;

type totpSecret = createHmacSecret;

type totpToken = (secret: string, options: totpOptionsInterface) => string;

declare class HOTP {
HOTP: typeof HOTP;
getClass(): typeof HOTP;

options: HOTPOptions;
optionsAll: HOTPOptions;
options: totpOptionsInterface;
optionsAll: totpOptionsInterface;
resetOptions(): this;
generate(secret: string, counter: number): string;
check(token: string, secret: string, counter: number): boolean;
verify(opts: HOTPVerifyOptions): boolean;
verify(opts: hotpVerifyOptionsInterface): boolean;
}

declare class TOTP extends HOTP {
TOTP: typeof TOTP;
getClass(): typeof TOTP;

options: TOTPOptions;
optionsAll: TOTPOptions;
options: totpOptionsInterface;
optionsAll: totpOptionsInterface;
generate(secret: string): string;
check(token: string, secret: string): boolean;
checkDelta(token: string, secret: string): number | null;
verify(opts: TOTPVerifyOptions): boolean;
verify(opts: totpVerifyOptionsInterface): boolean;
}

declare class Authenticator extends TOTP {
Expand Down Expand Up @@ -82,3 +133,22 @@ declare module 'otplib/hotp' {
const hotp: HOTP;
export = hotp;
}

declare module 'otplib/core' {
interface core {
hotpCheck: hotpCheck;
hotpCounter: hotpCounter;
hotpDigest: hotpDigest;
hotpOptions: hotpOptions;
hotpSecret: hotpSecret;
hotpToken: hotpToken;
totpCheck: totpCheck;
totpCheckWithWindow: totpCheckWithWindow;
totpCounter: totpCounter;
totpOptions: totpOptions;
totpSecret: totpSecret;
totpToken: totpToken;
}
const lib: core;
export = lib;
}
23 changes: 19 additions & 4 deletions packages/types-ts/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as otplib from 'otplib';
import authenticator = require('otplib/authenticator');
import core = require('otplib/core');
import hotp = require('otplib/hotp');
import totp = require('otplib/totp');

const SECRET = '1234567890';
let secret = '';
let token = '';

secret = otplib.authenticator.generateSecret(20);
token = otplib.authenticator.generate(secret);
secret = otplib.authenticator.generateSecret(20); // $ExpectType string
token = otplib.authenticator.generate(secret); // $ExpectType string

otplib.authenticator.check(token, secret); // $ExpectType boolean
otplib.authenticator.checkDelta(token, secret); // $ExpectType number | null
Expand All @@ -27,7 +28,7 @@ authenticator.keyuri('me', 'otplib-test', secret); // $ExpectType string
authenticator.Authenticator;
authenticator.getClass();

token = otplib.totp.generate(SECRET);
token = otplib.totp.generate(SECRET); // $ExpectType string
otplib.totp.check(token, SECRET); // $ExpectType boolean
otplib.totp.checkDelta(token, SECRET); // $ExpectType number | null
otplib.totp.verify({ secret: SECRET, token }); // $ExpectType boolean
Expand All @@ -39,7 +40,7 @@ totp.verify({ secret: SECRET, token }); // $ExpectType boolean
totp.TOTP;
totp.getClass();

token = otplib.hotp.generate(SECRET, 1);
token = otplib.hotp.generate(SECRET, 1); // $ExpectType string
otplib.hotp.check(token, SECRET, 1); // $ExpectType boolean
otplib.hotp.verify({ secret: SECRET, token, counter: 1 }); // $ExpectType boolean
otplib.hotp.HOTP;
Expand All @@ -48,3 +49,17 @@ hotp.check(token, SECRET, 1); // $ExpectType boolean
hotp.verify({ secret: SECRET, token, counter: 1 }); // $ExpectType boolean
hotp.HOTP;
hotp.getClass();

const hotpOpt = core.hotpOptions({});
core.hotpCheck('123', SECRET, 0, hotpOpt); // $ExpectType boolean
core.hotpCounter(0); // $ExpectType string
core.hotpDigest(SECRET, 0, hotpOpt); // $ExpectType string
core.hotpSecret(SECRET, { algorithm: hotpOpt.algorithm, encoding: hotpOpt.encoding }); // $ExpectType Buffer
core.hotpToken(SECRET, 0, hotpOpt); // $ExpectType string

const totpOpt = core.totpOptions({});
core.totpCheck('123', SECRET, totpOpt); // $ExpectType boolean
core.totpCheckWithWindow('123', SECRET, totpOpt); // $ExpectType number | null
core.totpCounter(new Date().valueOf(), 0); // $ExpectType number
core.totpSecret(SECRET, { algorithm: totpOpt.algorithm, encoding: totpOpt.encoding }); // $ExpectType Buffer
core.totpToken(SECRET, totpOpt); // $ExpectType string
3 changes: 3 additions & 0 deletions packages/types-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"otplib": [
"index"
],
"otplib/core": [
"core"
],
"otplib/hotp": [
"hotp"
],
Expand Down

0 comments on commit 9e02105

Please sign in to comment.