foronce 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 reaper <ahoy@barelyhuman.dev>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1 +1,89 @@
1
1
  # foronce
2
+
3
+ - [foronce](#foronce)
4
+ - [Usage](#usage)
5
+ - [Examples](#examples)
6
+ - [One Time Email Validation](#one-time-email-validation)
7
+ - [API](#api)
8
+
9
+ > TOTP in a few functions
10
+
11
+ ## Usage
12
+
13
+ - Install the `foronce` library
14
+
15
+ ```sh
16
+ ; npm install foronce
17
+ ```
18
+
19
+ ### Examples
20
+
21
+ #### One Time Email Validation
22
+
23
+ ```ts
24
+ import { generateTOTPSecret, totp, isTOTPValid } from 'foronce'
25
+
26
+ app.post('/login', (req, res) => {
27
+ const email = req.body.email
28
+ const secret = generateTOTPSecret()
29
+
30
+ const user = new User()
31
+ user.email = email
32
+ user.OTPSecret = secret
33
+ await user.save()
34
+
35
+ const otp = totp(secret)
36
+
37
+ EmailProvider.sendLoginEmail(email, otp)
38
+ })
39
+
40
+ app.post('/verify', (req, res) => {
41
+ const { email, otp } = req.body
42
+ const user = await User.find({
43
+ where: {
44
+ email: email,
45
+ },
46
+ })
47
+
48
+ if (!isTOTPValid(user.OTPSecret, otp)) {
49
+ res.status(400)
50
+ res.send({ message: 'Invalid OTP' })
51
+ return
52
+ }
53
+
54
+ res.send(generateLoginToken(user))
55
+ return
56
+ })
57
+ ```
58
+
59
+ ## API
60
+
61
+ ```ts
62
+ export function totp(
63
+ secret: string,
64
+ when?: number,
65
+ options?: {
66
+ period?: number
67
+ algorithm?: 'sha1' | 'sha256' | 'sha512'
68
+ }
69
+ ): string
70
+
71
+ export function isTOTPValid(
72
+ secret: string,
73
+ token: string,
74
+ options?: {
75
+ period?: number
76
+ algorithm?: 'sha1' | 'sha256' | 'sha512'
77
+ }
78
+ ): boolean
79
+
80
+ export function generateTOTPURL(
81
+ secret: string,
82
+ options: {
83
+ company: string
84
+ email: string
85
+ }
86
+ ): string
87
+
88
+ export function generateTOTPSecret(num?: number): string
89
+ ```
package/dist/index.cjs CHANGED
@@ -4,14 +4,13 @@ var node_crypto = require('node:crypto');
4
4
  var node_buffer = require('node:buffer');
5
5
  var base32 = require('./base32.cjs');
6
6
 
7
- /**
8
- * Simplified implementation of TOTP with existing node libs
9
- *
10
- * helpers to handle the following
11
- * - generate QR codes with the otpauth:// URL scheme
12
- * - algo and implmentation taken from https://drewdevault.com/2022/10/18/TOTP-is-easy.html
7
+ /*!
8
+ * base-32.js
9
+ * Copyright(c) 2024 Reaper
10
+ * MIT Licensed
13
11
  */
14
12
 
13
+
15
14
  const { floor } = Math;
16
15
 
17
16
  /**
@@ -20,14 +19,15 @@ const { floor } = Math;
20
19
  * @param {number} when
21
20
  * @param {object} [options]
22
21
  * @param {number} [options.period] in seconds (eg: 30 => 30 seconds)
22
+ * @param {"sha1" | "sha256" | "sha512"} [options.algorithm] (default: sha512)
23
23
  * @returns {string}
24
24
  */
25
25
  function totp(secret, when = floor(Date.now() / 1000), options = {}) {
26
- const _options = Object.assign({ period: 30 }, options);
26
+ const _options = Object.assign({ period: 30, algorithm: 'sha512' }, options);
27
27
  const now = floor(when / _options.period);
28
28
  const key = base32.decode(secret);
29
29
  const buff = bigEndian64(BigInt(now));
30
- const hmac = node_crypto.createHmac('sha512', key).update(buff).digest();
30
+ const hmac = node_crypto.createHmac(_options.algorithm, key).update(buff).digest();
31
31
  const offset = hmac[hmac.length - 1] & 0xf;
32
32
  const truncatedHash = hmac.subarray(offset, offset + 4);
33
33
  const otp = (
@@ -42,10 +42,11 @@ function totp(secret, when = floor(Date.now() / 1000), options = {}) {
42
42
  * @param {string} token
43
43
  * @param {object} [options]
44
44
  * @param {number} [options.period] in seconds (eg: 30 => 30 seconds)
45
+ * @param {"sha1" | "sha256" | "sha512"} [options.algorithm] (default: sha512)
45
46
  * @returns {boolean}
46
47
  */
47
- function isValid(secret, token, options = {}) {
48
- const _options = Object.assign({ period: 30 }, options);
48
+ function isTOTPValid(secret, token, options = {}) {
49
+ const _options = Object.assign({ period: 30, algorithm: 'sha512' }, options);
49
50
  for (let index = -2; index < 3; index += 1) {
50
51
  const fromSys = totp(secret, Date.now() / 1000 + index, _options);
51
52
  const valid = fromSys === token;
@@ -88,5 +89,5 @@ function generateTOTPSecret(num = 32) {
88
89
 
89
90
  exports.generateTOTPSecret = generateTOTPSecret;
90
91
  exports.generateTOTPURL = generateTOTPURL;
91
- exports.isValid = isValid;
92
+ exports.isTOTPValid = isTOTPValid;
92
93
  exports.totp = totp;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export function totp(secret: string, when?: number, options?: {
2
2
  period?: number;
3
+ algorithm?: "sha1" | "sha256" | "sha512";
3
4
  }): string;
4
- export function isValid(secret: string, token: string, options?: {
5
+ export function isTOTPValid(secret: string, token: string, options?: {
5
6
  period?: number;
7
+ algorithm?: "sha1" | "sha256" | "sha512";
6
8
  }): boolean;
7
9
  export function generateTOTPURL(secret: string, options: {
8
10
  company: string;
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "foronce",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
+ "description": "TOTP in a few functions",
4
5
  "repository": "git@github.com:dumbjs/foronce.git",
5
6
  "license": "MIT",
6
7
  "author": "Reaper <ahoy@barelyhuman.dev>",
@@ -39,10 +40,6 @@
39
40
  "*.{js,css,md,json}": "prettier --write"
40
41
  },
41
42
  "prettier": "@barelyhuman/prettier-config",
42
- "dependencies": {
43
- "hi-base32": "^0.5.1",
44
- "uncrypto": "^0.1.3"
45
- },
46
43
  "devDependencies": {
47
44
  "@barelyhuman/prettier-config": "^1.0.0",
48
45
  "@types/node": "^20.10.8",
package/src/index.js CHANGED
@@ -1,3 +1,9 @@
1
+ /*!
2
+ * base-32.js
3
+ * Copyright(c) 2024 Reaper
4
+ * MIT Licensed
5
+ */
6
+
1
7
  /**
2
8
  * Simplified implementation of TOTP with existing node libs
3
9
  *
@@ -17,14 +23,15 @@ const { floor } = Math
17
23
  * @param {number} when
18
24
  * @param {object} [options]
19
25
  * @param {number} [options.period] in seconds (eg: 30 => 30 seconds)
26
+ * @param {"sha1" | "sha256" | "sha512"} [options.algorithm] (default: sha512)
20
27
  * @returns {string}
21
28
  */
22
29
  export function totp(secret, when = floor(Date.now() / 1000), options = {}) {
23
- const _options = Object.assign({ period: 30 }, options)
30
+ const _options = Object.assign({ period: 30, algorithm: 'sha512' }, options)
24
31
  const now = floor(when / _options.period)
25
32
  const key = decode(secret)
26
33
  const buff = bigEndian64(BigInt(now))
27
- const hmac = createHmac('sha512', key).update(buff).digest()
34
+ const hmac = createHmac(_options.algorithm, key).update(buff).digest()
28
35
  const offset = hmac[hmac.length - 1] & 0xf
29
36
  const truncatedHash = hmac.subarray(offset, offset + 4)
30
37
  const otp = (
@@ -39,10 +46,11 @@ export function totp(secret, when = floor(Date.now() / 1000), options = {}) {
39
46
  * @param {string} token
40
47
  * @param {object} [options]
41
48
  * @param {number} [options.period] in seconds (eg: 30 => 30 seconds)
49
+ * @param {"sha1" | "sha256" | "sha512"} [options.algorithm] (default: sha512)
42
50
  * @returns {boolean}
43
51
  */
44
- export function isValid(secret, token, options = {}) {
45
- const _options = Object.assign({ period: 30 }, options)
52
+ export function isTOTPValid(secret, token, options = {}) {
53
+ const _options = Object.assign({ period: 30, algorithm: 'sha512' }, options)
46
54
  for (let index = -2; index < 3; index += 1) {
47
55
  const fromSys = totp(secret, Date.now() / 1000 + index, _options)
48
56
  const valid = fromSys === token