Skip to content

Commit

Permalink
semantic rename and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Jan 11, 2024
1 parent b240a00 commit 41b7f3b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 32 deletions.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
# foronce

- [foronce](#foronce)
- [Usage](#usage)
- [Examples](#examples)
- [One Time Email Validation](#one-time-email-validation)
- [API](#api)

> TOTP in a few functions
## Usage

- Install the `foronce` library

```sh
; npm install foronce
```

### Examples

#### One Time Email Validation

```ts
import { generateTOTPSecret, totp, isTOTPValid } from 'foronce'

app.post('/login', (req, res) => {
const email = req.body.email
const secret = generateTOTPSecret()

const user = new User()
user.email = email
user.OTPSecret = secret
await user.save()

const otp = totp(secret)

EmailProvider.sendLoginEmail(email, otp)
})

app.post('/verify', (req, res) => {
const { email, otp } = req.body
const user = await User.find({
where: {
email: email,
},
})

if (!isTOTPValid(user.OTPSecret, otp)) {
res.status(400)
res.send({ message: 'Invalid OTP' })
return
}

res.send(generateLoginToken(user))
return
})
```

## API

```ts
export function totp(
secret: string,
when?: number,
options?: {
period?: number
algorithm?: 'sha1' | 'sha256' | 'sha512'
}
): string

export function isTOTPValid(
secret: string,
token: string,
options?: {
period?: number
algorithm?: 'sha1' | 'sha256' | 'sha512'
}
): boolean

export function generateTOTPURL(
secret: string,
options: {
company: string
email: string
}
): string

export function generateTOTPSecret(num?: number): string
```
30 changes: 3 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "foronce",
"version": "0.0.2",
"description": "TOTP in a few functions",
"repository": "[email protected]:dumbjs/foronce.git",
"license": "MIT",
"author": "Reaper <[email protected]>",
Expand Down Expand Up @@ -39,10 +40,6 @@
"*.{js,css,md,json}": "prettier --write"
},
"prettier": "@barelyhuman/prettier-config",
"dependencies": {
"hi-base32": "^0.5.1",
"uncrypto": "^0.1.3"
},
"devDependencies": {
"@barelyhuman/prettier-config": "^1.0.0",
"@types/node": "^20.10.8",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function totp(secret, when = floor(Date.now() / 1000), options = {}) {
* @param {"sha1" | "sha256" | "sha512"} [options.algorithm] (default: sha512)
* @returns {boolean}
*/
export function validateTOTP(secret, token, options = {}) {
export function isTOTPValid(secret, token, options = {}) {
const _options = Object.assign({ period: 30, algorithm: 'sha512' }, options)
for (let index = -2; index < 3; index += 1) {
const fromSys = totp(secret, Date.now() / 1000 + index, _options)
Expand Down

0 comments on commit 41b7f3b

Please sign in to comment.