1
1
const dotenv = require ( 'dotenv' ) ;
2
2
const express = require ( 'express' ) ;
3
- const router = express . Router ( ) ;
4
- var cors = require ( 'cors' ) ;
3
+ const jwt = require ( 'jsonwebtoken' ) ;
5
4
const app = express ( ) ;
6
- app . use ( cors ( ) ) ;
5
+ const cors = require ( 'cors' ) ;
7
6
8
7
dotenv . config ( ) ;
9
8
10
- const uuid = require ( 'uuid' ) ;
11
- const crypto = require ( "crypto" ) ;
9
+ app . use ( cors ( ) ) ;
10
+ app . use ( express . json ( ) ) ;
12
11
12
+ const publicKey = process . env . PUBLIC_KEY ;
13
13
const privateKey = process . env . PRIVATE_KEY ;
14
14
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 } ) ;
26
29
} ) ;
27
30
28
- app . use ( "/" , router ) ;
29
-
30
- app . listen ( 8080 , function ( ) {
31
+ app . listen ( 8080 , function ( ) {
31
32
console . log ( "Live at Port 8080" ) ;
32
33
} ) ;
0 commit comments