generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webauthn.go
70 lines (61 loc) · 1.79 KB
/
webauthn.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
package webauthn
import (
_ "embed"
"encoding/json"
di "github.com/fluffy-bunny/fluffy-dozm-di"
contracts_webauthn "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/webauthn"
go_webauthn "github.com/go-webauthn/webauthn/webauthn"
uuid "github.com/gofrs/uuid"
)
type (
service struct {
config *contracts_webauthn.WebAuthNConfig
w *go_webauthn.WebAuthn
aaGUIDMap map[string]AAGUIDFriendlyName
}
AAGUIDFriendlyName struct {
Name string `json:"name"`
}
)
var stemService = (*service)(nil)
//go:embed aaguid.json
var authenticatorMetadataJson []byte
func init() {
var _ contracts_webauthn.IWebAuthN = stemService
}
func (s *service) Ctor(
config *contracts_webauthn.WebAuthNConfig,
) (contracts_webauthn.IWebAuthN, error) {
wConfig := &go_webauthn.Config{
RPDisplayName: config.RPDisplayName, // Display Name for your site
RPID: config.RPID, // Generally the FQDN for your site
RPOrigins: config.RPOrigins, // The origin URLs allowed for WebAuthn requests
}
w, err := go_webauthn.New(wConfig)
if err != nil {
return nil, err
}
var aaGUIDMap map[string]AAGUIDFriendlyName = make(map[string]AAGUIDFriendlyName)
err = json.Unmarshal(authenticatorMetadataJson, &aaGUIDMap)
if err != nil {
return nil, err
}
ss := &service{
w: w,
config: config,
aaGUIDMap: aaGUIDMap,
}
return ss, nil
}
func AddSingletonIWebAuthN(cb di.ContainerBuilder) {
di.AddSingleton[contracts_webauthn.IWebAuthN](cb, stemService.Ctor)
}
func (s *service) GetWebAuthN() *go_webauthn.WebAuthn {
return s.w
}
func (s *service) GetFriendlyNameByAAGUID(aaguid uuid.UUID) string {
if friendlyName, ok := s.aaGUIDMap[aaguid.String()]; ok {
return friendlyName.Name
}
return ""
}