foronce 0.0.2 → 0.0.4

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,88 @@
1
- # foronce
1
+ <p text-align="center">
2
+ <img src="https://og.barelyhuman.xyz/generate?fontSize=20&title=foronce&subtitle=The+OTP+Library&fontSizeTwo=8&color=%23a1a1aa&backgroundColor=%2318181b" />
3
+ </p>
4
+
5
+ - [Usage](#usage)
6
+ - [Examples](#examples)
7
+ - [One Time Email Validation](#one-time-email-validation)
8
+ - [API](#api)
9
+
10
+ ## Usage
11
+
12
+ - Install the `foronce` library
13
+
14
+ ```sh
15
+ ; npm install foronce
16
+ ```
17
+
18
+ ### Examples
19
+
20
+ #### One Time Email Validation
21
+
22
+ ```ts
23
+ import { generateTOTPSecret, isTOTPValid, totp } from 'foronce'
24
+
25
+ app.post('/login', (req, res) => {
26
+ const email = req.body.email
27
+ const secret = generateTOTPSecret()
28
+
29
+ const user = new User()
30
+ user.email = email
31
+ user.OTPSecret = secret
32
+ await user.save()
33
+
34
+ const otp = totp(secret)
35
+
36
+ EmailProvider.sendLoginEmail(email, otp)
37
+ })
38
+
39
+ app.post('/verify', (req, res) => {
40
+ const { email, otp } = req.body
41
+ const user = await User.find({
42
+ where: {
43
+ email: email,
44
+ },
45
+ })
46
+
47
+ if (!isTOTPValid(user.OTPSecret, otp)) {
48
+ res.status(400)
49
+ res.send({ message: 'Invalid OTP' })
50
+ return
51
+ }
52
+
53
+ res.send(generateLoginToken(user))
54
+ return
55
+ })
56
+ ```
57
+
58
+ ## API
59
+
60
+ ```ts
61
+ export function totp(
62
+ secret: string,
63
+ when?: number,
64
+ options?: {
65
+ period?: number
66
+ algorithm?: 'sha1' | 'sha256' | 'sha512'
67
+ }
68
+ ): string
69
+
70
+ export function isTOTPValid(
71
+ secret: string,
72
+ token: string,
73
+ options?: {
74
+ period?: number
75
+ algorithm?: 'sha1' | 'sha256' | 'sha512'
76
+ }
77
+ ): boolean
78
+
79
+ export function generateTOTPURL(
80
+ secret: string,
81
+ options: {
82
+ company: string
83
+ email: string
84
+ }
85
+ ): string
86
+
87
+ export function generateTOTPSecret(num?: number): string
88
+ ```
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.4",
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