-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (68 loc) · 1.88 KB
/
main.go
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
77
78
79
80
81
82
package main
import (
"log"
"os"
"example/store"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
"github.com/streamerd/fiber-webauthn"
)
func main() {
// Get port from environment or use default
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
// Initialize SQLite store
store, err := store.NewSQLiteStore("webauthn.db")
if err != nil {
log.Fatal(err)
}
// Initialize template engine
engine := html.New("./views", ".html")
// Initialize Fiber
app := fiber.New(fiber.Config{
Views: engine,
DisableStartupMessage: true,
})
// Add proxy configuration
app.Use(func(c *fiber.Ctx) error {
c.Set("X-Forwarded-Proto", "http")
c.Set("X-Forwarded-Host", "localhost:3000")
return c.Next()
})
// Serve static files
app.Static("/", "./views")
// Add redirect middleware FIRST
app.Use(func(c *fiber.Ctx) error {
if c.Hostname() == "127.0.0.1" {
originalURL := c.OriginalURL()
if originalURL == "" {
originalURL = "/"
}
return c.Redirect("http://localhost:3000" + originalURL)
}
return c.Next()
})
// Initialize WebAuthn middleware
webAuthnMiddleware := webauthn.New(webauthn.Config{
RPDisplayName: "WebAuthn Example",
RPID: "localhost",
RPOrigins: []string{"http://localhost:3000"},
CredentialStore: store,
})
// Routes
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "WebAuthn Demo",
}, "")
})
// WebAuthn endpoints
auth := app.Group("/auth/passkey")
auth.Post("/register/begin", webAuthnMiddleware.BeginRegistration())
auth.Post("/register/finish", webAuthnMiddleware.FinishRegistration())
auth.Post("/login/begin", webAuthnMiddleware.BeginAuthentication())
auth.Post("/login/finish", webAuthnMiddleware.FinishAuthentication())
log.Printf("Server started at http://localhost:%s\n", port)
log.Fatal(app.Listen("localhost:" + port))
}