-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.go
More file actions
76 lines (62 loc) · 1.71 KB
/
Copy pathauth.go
File metadata and controls
76 lines (62 loc) · 1.71 KB
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
package main
import (
"fmt"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/pocketbase/pocketbase/core"
)
// AuthService handles authentication setup and operations
type AuthService struct {
webAuthn *webauthn.WebAuthn
datastore PasskeyStore
logger Logger
config *AppConfig
}
// NewAuthService creates a new authentication service
func NewAuthService(config *AppConfig, logger Logger) (*AuthService, error) {
// Configure WebAuthn
wconfig := &webauthn.Config{
RPDisplayName: "PB Experiments WebAuthn",
RPID: config.Host,
RPOrigins: []string{config.Origin},
}
webAuthn, err := webauthn.New(wconfig)
if err != nil {
return nil, fmt.Errorf("failed to initialize WebAuthn: %w", err)
}
return &AuthService{
webAuthn: webAuthn,
logger: logger,
config: config,
}, nil
}
// SetDatastore sets the datastore for the auth service
func (a *AuthService) SetDatastore(datastore PasskeyStore) {
a.datastore = datastore
}
// GetWebAuthn returns the WebAuthn instance
func (a *AuthService) GetWebAuthn() *webauthn.WebAuthn {
return a.webAuthn
}
// GetDatastore returns the datastore instance
func (a *AuthService) GetDatastore() PasskeyStore {
return a.datastore
}
// GetLogger returns the logger instance
func (a *AuthService) GetLogger() Logger {
return a.logger
}
// GetTOTPIssuer returns the TOTP issuer from config
func (a *AuthService) GetTOTPIssuer() string {
return a.config.TOTPIssuer
}
// getEmail extracts email from request body
func getEmail(e *core.RequestEvent) (string, error) {
type User struct {
Email string `json:"email"`
}
var u User
if err := e.BindBody(&u); err != nil {
return "", fmt.Errorf("failed to read request data: %w", err)
}
return u.Email, nil
}