Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 96ced5d

Browse files
committed
add token utils
1 parent 548ccb2 commit 96ced5d

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
APP_TOKEN_KEY=

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('dotenv').config()
2+
13
const app = require('./app')
24

35
const PORT = process.env.PORT || 3000

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"body-parser": "^1.19.0",
12+
"dotenv": "^8.2.0",
1213
"express": "^4.17.1"
1314
},
1415
"devDependencies": {

server/auth/token.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { promisify } = require('util')
2+
const crypto = require('crypto')
3+
4+
const randomBytes = promisify(crypto.randomBytes)
5+
const tokenKey = Buffer.from(process.env.APP_TOKEN_KEY, 'base64')
6+
7+
const tokenKinds = {
8+
auth: 0
9+
}
10+
11+
const encryptToken = async (content) => {
12+
const iv = await randomBytes(12)
13+
const cipher = crypto.createCipheriv('aes-256-gcm', tokenKey, iv)
14+
const cipherText = cipher.update(JSON.stringify(content))
15+
cipher.final()
16+
const tokenContent = Buffer.concat([iv, cipherText, cipher.getAuthTag()])
17+
return tokenContent.toString('base64')
18+
}
19+
20+
const decryptToken = async (token) => {
21+
try {
22+
const tokenContent = Buffer.from(token, 'base64')
23+
const iv = tokenContent.slice(0, 12)
24+
const authTag = tokenContent.slice(tokenContent.length - 16)
25+
const cipher = crypto.createDecipheriv('aes-256-gcm', tokenKey, iv)
26+
cipher.setAuthTag(authTag)
27+
const plainText = cipher.update(tokenContent.slice(12, tokenContent.length - 16))
28+
return JSON.parse(plainText)
29+
} catch (e) {
30+
return null
31+
}
32+
}
33+
34+
const getUserId = async (token) => {
35+
const content = await decryptToken(token)
36+
if (content === null) {
37+
return null
38+
}
39+
const { k: kind, id: userId } = content
40+
if (kind !== tokenKinds.auth) {
41+
return null
42+
}
43+
return userId
44+
}
45+
46+
const getToken = async (userId) => {
47+
const token = await encryptToken({
48+
k: tokenKinds.auth,
49+
id: userId
50+
})
51+
return token
52+
}
53+
54+
module.exports = {
55+
getUserId,
56+
getToken
57+
}

static/.gitkeep

Whitespace-only changes.

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ doctrine@^3.0.0:
289289
dependencies:
290290
esutils "^2.0.2"
291291

292+
dotenv@^8.2.0:
293+
version "8.2.0"
294+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
295+
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
296+
292297
293298
version "1.1.1"
294299
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"

0 commit comments

Comments
 (0)