-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (60 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { randomUUID } = require("crypto")
const express = require("express")
const {generateRegistrationOptions, verifyAuthenticationResponse} = require("@simplewebauthn/server")
const cors = require("cors")
const crypto = require("node:crypto")
if(!globalThis.crypto){
globalThis.crypto = crypto
}
const app = express()
app.use(cors())
app.use(express.static('./public'))
app.use(express.json())
const userStore = {}
app.post("/signup" , (req,res)=>{
const {username , password} = req.body
const id = randomUUID()
const user = {
id ,
username ,
password
}
userStore[id] = user
console.log("resgistered successffuly")
return res.json({id})
})
app.post("/register-challenge" , async (req,res)=>{
const {userid} = req.body
const user = userStore[userid];
if (!userStore[userid]) return res.status(404).json({error : "user not found"})
const registrationOptions = await generateRegistrationOptions({
rpName : "example.com",
rpId : "example.com",
userName : user.username,
})
userStore[userid] = registrationOptions.challenge
return res.json({
options: registrationOptions
})
})
app.post("/register-verify" , async(req,res)=>{
const {userid , cred} = req.body
const user = userStore[userid]
const challenge = challengeStore[userid]
const verificationresult = await verifyAuthenticationResponse({
expectedChallenge: challenge,
expectedOrigin:"http://localhost:3000",
expectedRPID: "example.com",
response: cred
})
if (verificationresult === "valid") {
userStore[userid].passkey = verificationresult.registrationInfo
console.log("user verified")
return res.json({message : "user verified"})
} else {
return res.status(401).json({error : "invalid credentials"})
}
})
app.listen(3000 , ()=>{
console.log("server is running on port 3000")
})