Skip to content

Commit 9c04e00

Browse files
committed
fix: updated server for upload auth endpoint
1 parent 99e8da1 commit 9c04e00

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

Server/.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
PRIVATE_KEY =
1+
PUBLIC_KEY=
2+
PRIVATE_KEY=

Server/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.lock

Server/index.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
const dotenv = require('dotenv');
22
const express = require('express');
3-
const router = express.Router();
4-
var cors = require('cors');
3+
const jwt = require('jsonwebtoken');
54
const app = express();
6-
app.use(cors());
5+
const cors = require('cors');
76

87
dotenv.config();
98

10-
const uuid = require('uuid');
11-
const crypto = require("crypto");
9+
app.use(cors());
10+
app.use(express.json());
1211

12+
const publicKey = process.env.PUBLIC_KEY;
1313
const privateKey = process.env.PRIVATE_KEY;
1414

15-
router.get("/auth", function(req, res) {
16-
var token = req.query.token || uuid.v4();
17-
var expire = req.query.expire || parseInt(Date.now()/1000)+2400;
18-
var privateAPIKey = `${privateKey}`;
19-
var signature = crypto.createHmac('sha1', privateAPIKey).update(token+expire).digest('hex');
20-
res.status(200);
21-
res.send({
22-
token : token,
23-
expire : expire,
24-
signature : signature
25-
});
15+
app.post("/auth", function (req, res) {
16+
const token = jwt.sign(
17+
req.body.uploadPayload,
18+
privateKey,
19+
{
20+
expiresIn: req.body.expire,
21+
header: {
22+
alg: "HS256",
23+
typ: "JWT",
24+
kid: publicKey,
25+
},
26+
})
27+
res.status(200);
28+
res.send({ token });
2629
});
2730

28-
app.use("/",router);
29-
30-
app.listen(8080,function(){
31+
app.listen(8080, function () {
3132
console.log("Live at Port 8080");
3233
});

Server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"cors": "^2.8.5",
88
"dotenv": "^8.2.0",
99
"express": "^4.17.1",
10+
"jsonwebtoken": "^9.0.2",
1011
"uuid": "^8.3.0"
1112
}
1213
}

0 commit comments

Comments
 (0)