Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
docker-compose.yaml
LICENSE
*.md
node_modules
node_modules
*.pem
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
node_modules
**/*.html
!examples
!examples
*.pem
42 changes: 41 additions & 1 deletion src/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,44 @@ const jwt = require("jsonwebtoken")
const fs = require("fs")
const path = require("path")

const privateKey = fs.readFileSync(path.join(__dirname, "./keys/private.key"))
const logger = require("./logger")

const { generateKeyPairSync } = require("crypto")

const keyPath = path.join(__dirname, "../secrets/private_key.pem")

let privateKey

function CheckForKey() {
if (fs.existsSync(keyPath)) {
privateKey = fs.readFileSync(keyPath, "utf8")

logger.log("Loaded existing RSA private key")
} else {
const { privateKey: genPrivKey, publicKey: genPubKey } =
generateKeyPairSync("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
},
})

fs.mkdirSync(path.dirname(keyPath), { recursive: true })
fs.writeFileSync(keyPath, genPrivKey)
fs.writeFileSync(
path.join(__dirname, "../secrets/public_key.pem"),
genPubKey
)
privateKey = genPrivKey

logger.log("Generated new RSA key pair")
}
}

function SignToken(payload) {
const options = {
Expand All @@ -17,5 +54,8 @@ function DecodeToken(idToken) {
return jwt.decode(idToken)
}

CheckForKey()

exports.SignToken = SignToken
exports.DecodeToken = DecodeToken
exports.CheckForKey = CheckForKey