Skip to content

Commit

Permalink
feat: rethink of packages
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Aug 25, 2019
1 parent b088efe commit a8a65ee
Show file tree
Hide file tree
Showing 49 changed files with 468 additions and 490 deletions.
61 changes: 36 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
- [Adding the scripts](#adding-the-scripts)
- [Browser bundle size](#browser-bundle-size)
- [Available Packages](#available-packages)
- [Base](#base)
- [Core](#core)
- [Plugins](#plugins)
- [Crypto](#crypto)
- [Base 32](#base-32)
- [Packages](#packages)
- [Base32](#base32)
- [Presets](#presets)
- [Available Options](#available-options)
- [Google Authenticator](#google-authenticator)
- [Difference between Authenticator and TOTP](#difference-between-authenticator-and-totp)
Expand Down Expand Up @@ -169,42 +169,53 @@ Paired with the gzipped browser `buffer.js` module, it would be about `7.65KB +

## Available Packages

This library has been split into 3 categories: `base`, `plugins` and `packages`.
This library has been split into 3 categories: `core`, `plugin` and `preset`.

### Base
### Core

These provides the main functionality of the library. However parts of the logic
has been separated out in order to provide flexibility to the library.

| file | description |
| -------------------- | ---------------------------------- |
| otplib/core | HOTP and TOTP functionality |
| otplib/authenticator | Google Authenticator functionality |
| file | description |
| -------------------- | ---------------------------------------------------- |
| otplib/hotp | HOTP functions + class |
| otplib/hotp | TOTP functions + class |
| otplib/authenticator | Google Authenticator functions + class |
| otplib/core | Aggregates hotp/totp/authenticator functions + class |

### Plugins

#### Crypto

| file | description |
| ---------------------- | ------------------------- |
| otplib/crypto/node | node crypto based methods |
| otplib/crypto/cryptojs | crypto-js based methods |
These plugins provide the core digest generation which is used in
token generation.

#### Base 32
| plugin | npm |
| ---------------------- | ----------------------- |
| otplib/plugin-crypto | crypto module from node |
| otplib/plugin-cryptojs | `npm install crypto-js` |

| file | description |
| -------------------------- | ----------------------------------------------------- |
| otplib/base32/thirty-two | Encoder/Decoder using thirty-two |
| otplib/base32/base32-endec | Encoder/Decoder using base32-encode and base32-decode |
#### Base32

### Packages
The Base32 functionalities are used when encoding and decoding keys
in the Google Authenticator implementation.

| file | description |
| --------------- | ----------------------------------------------------------- |
| otplib/node | Uses node crypto + thirty-two |
| otplib/cryptojs | Uses crypto-js + thirty-two |
| otplib/browser | Webpack bundle. Uses base32-endec + crypto-js |
| otplib/legacy | Wrapper to adapt the APIs to [email protected] compatible format |
| plugin | npm |
| ---------------------------- | ----------------------------------------- |
| otplib/plugin-thirty-two | `npm install thirty-two` |
| otplib/plugin-base32-enc-dec | `npm install base32-encode base32-decode` |

### Presets

Presets are preconfigured HOTP, TOTP, Authenticator instances to allow for a
faster implementations. They would need the corresponding npm module
to be installed (except `preset-browser`).

| file | description |
| --------------------- | ----------------------------------------------------------- |
| otplib/preset-default | Uses node crypto + thirty-two |
| otplib/preset-browser | Webpack bundle. Uses base32-endec + crypto-js |
| otplib/preset-legacy | Wrapper to adapt the APIs to [email protected] compatible format |

## Available Options

Expand Down
88 changes: 41 additions & 47 deletions configs/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,56 @@ const defaultPresetEnv = {
targets: 'node 8'
};

const standard = alias => ({
alias,
external: [],
bundler: 'rollup',
files: ['index.ts'],
format: 'cjs',
presetEnv: defaultPresetEnv
});

module.exports = {
'otplib-authenticator': {
alias: 'authenticator',
bundler: 'rollup',
external: [],
files: ['index.ts'],
format: 'cjs',
presetEnv: defaultPresetEnv
// core
'otplib-authenticator': standard('authenticator'),
'otplib-core': standard('core'),
'otplib-hotp': standard('hotp'),
'otplib-totp': standard('totp'),

// base32
'otplib-plugin-base32-enc-dec': {
...standard('plugin-base32-enc-dec'),
external: ['base32-encode', 'base32-decode']
},
'otplib-base32': {
alias: 'base32',
bundler: 'rollup',
external: ['thirty-two', 'base32-encode', 'base32-decode'],
files: ['base32-endec.ts', 'thirty-two.ts'],
format: 'cjs',
presetEnv: defaultPresetEnv
'otplib-plugin-thirty-two': {
...standard('plugin-thirty-two'),
external: ['thirty-two']
},
'otplib-browser': {
alias: 'browser',

// crypto
'otplib-plugin-crypto': {
...standard('plugin-crypto'),
external: ['crypto']
},
'otplib-plugin-crypto-js': {
...standard('plugin-crypto-js'),
external: ['crypto']
},

// presets
'otplib-preset-browser': {
alias: 'preset-browser',
bundler: 'webpack',
files: ['index.ts'],
format: 'umd',
presetEnv: {
targets: 'cover 99.5%'
}
},
'otplib-core': {
alias: 'core',
bundler: 'rollup',
external: [],
files: ['index.ts'],
format: 'cjs',
presetEnv: defaultPresetEnv
},
'otplib-cryptojs': {
alias: 'cryptojs',
bundler: 'rollup',
external: ['crypto-js'],
files: ['index.ts'],
format: 'cjs',
presetEnv: defaultPresetEnv
},
'otplib-legacy': {
alias: 'legacy',
bundler: 'rollup',
external: [],
files: ['index.js'],
format: 'cjs',
presetEnv: defaultPresetEnv
},
'otplib-node': {
alias: 'node',
bundler: 'rollup',
external: ['crypto'],
files: ['index.ts'],
format: 'cjs',
presetEnv: defaultPresetEnv

'otplib-preset-default': standard('preset-default'),
'otplib-preset-legacy': {
...standard('preset-legacy'),
files: ['index.js']
}
};
3 changes: 2 additions & 1 deletion configs/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function rollupConfig(config, helpers) {
]
}),
nodeResolve({
extensions: helpers.EXTENSIONS
extensions: helpers.EXTENSIONS,
preferBuiltins: true
}),
commonjs({
include: 'node_modules/**'
Expand Down
5 changes: 0 additions & 5 deletions configs/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ function webpackConfig(config, helpers) {
path: config.buildFolderPath,
filename: config.buildFileName
},
// externals: {
// Buffer: {
// root: 'buffer.Buffer'
// }
// },
node: {
Buffer: false
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "otplib",
"version": "11.1.0-0",
"description": "HMAC-based (HOTP) and Time-based (TOTP) One-Time Password library",
"main": "./node/index.js",
"typings": "./node/index.d.ts",
"main": "./core/index.js",
"typings": "./core/index.d.ts",
"scripts": {
"build": "./scripts/build.sh",
"build:site": "./scripts/build-site.sh",
Expand Down
37 changes: 34 additions & 3 deletions packages/otplib-authenticator/authenticator.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
import { AUTHENTICATOR_DATASET } from 'tests-suites';
import * as totp from 'otplib-core/totp';
import { HashAlgorithms } from 'otplib-core';
import * as totp from 'otplib-totp/totp';
import { HashAlgorithms } from 'otplib-hotp';
import {
AuthenticatorOptions,
authenticatorOptionValidator,
Authenticator
} from './authenticator';

interface AuthenticatorTestCase {
decoded: string;
digest: string;
secret: string;
epoch: number;
token: string;
}

export const AUTHENTICATOR_DATASET: AuthenticatorTestCase[] = [
{
decoded: '68442f372b67474e2f47617679706f6e30756f51',
digest: '422eb1a849cf0650fef4dbdd8b0ee0fe57a87eb9',
epoch: 1565103854545,
secret: 'NBCC6NZLM5DU4L2HMF3HS4DPNYYHK32R',
token: '566155'
},
{
decoded: '68442f372b67474e2f47617679706f6e30756f51',
digest: 'c305b82dbf2a8d2d8a22e9d3992e4e666222d0e2',
secret: 'NBCC6NZLM5DU4L2HMF3HS4DPNYYHK32R',
epoch: 1565103878581,
token: '522154'
},
{
decoded: '636c6c4e506479436f314f6b4852623167564f76',
digest: '64a959e511420af1a406424f87b4412977b3cbd4',
secret: 'MNWGYTSQMR4UG3ZRJ5VUQUTCGFTVMT3W',
epoch: 1565103903110,
token: '540849'
}
];

const runOptionValidator = (
opt: Partial<AuthenticatorOptions>
): { error: boolean; message?: string } => {
Expand Down
8 changes: 5 additions & 3 deletions packages/otplib-authenticator/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {
HashAlgorithms,
KeyEncodings,
SecretKey,
createInstance
} from 'otplib-hotp';
import {
TOTP,
TOTPOptions,
totpCheckWithWindow,
totpCreateHmacKey,
totpOptionsValidator,
totpToken,
createInstance
} from 'otplib-core';
totpToken
} from 'otplib-totp';

/**
* RFC4648 / RFC3548 Base32 String.
Expand Down
4 changes: 0 additions & 4 deletions packages/otplib-base32/base32-endec.test.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/otplib-base32/thirty-two.test.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/otplib-browser/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/otplib-core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './hotp';
export * from './totp';
export * from './utils';
export * from 'otplib-hotp';
export * from 'otplib-totp';
export * from 'otplib-authenticator';
18 changes: 0 additions & 18 deletions packages/otplib-cryptojs/index.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { secret } from 'tests-data/rfc4226';
import { HOTPOptions, hotpOptionsValidator, HOTP } from './hotp';
import { HashAlgorithms } from './utils';
import { HOTPOptions, hotpOptionsValidator, HOTP } from './hotp';

interface HOTPCheckTestCase {
token: string;
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/otplib-hotp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './hotp';
export * from './utils';
File renamed without changes.
File renamed without changes.
20 changes: 0 additions & 20 deletions packages/otplib-legacy/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions packages/otplib-node/index.test.ts

This file was deleted.

Loading

0 comments on commit a8a65ee

Please sign in to comment.