-
Notifications
You must be signed in to change notification settings - Fork 15
/
webauthn.go
90 lines (73 loc) · 2.45 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package types
import (
"net/url"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
)
type SmartContractUser struct {
address string
credential *webauthn.Credential
}
func (s SmartContractUser) WebAuthnID() []byte {
return []byte(s.address)
}
func (s SmartContractUser) WebAuthnName() string {
return s.address
}
func (s SmartContractUser) WebAuthnDisplayName() string {
return s.WebAuthnName()
}
func (s SmartContractUser) WebAuthnCredentials() []webauthn.Credential {
return []webauthn.Credential{*s.credential}
}
func (s SmartContractUser) WebAuthnIcon() string {
return ""
}
var _ webauthn.User = SmartContractUser{}
func VerifyRegistration(rp *url.URL, contractAddr string, challenge string, credentialCreationData *protocol.ParsedCredentialCreationData) (*webauthn.Credential, error) {
config := webauthn.Config{
RPID: rp.Host,
RPDisplayName: rp.String(),
RPOrigins: []string{rp.String()},
AttestationPreference: "",
AuthenticatorSelection: protocol.AuthenticatorSelection{},
}
webAuthn, err := webauthn.New(&config)
if err != nil {
return nil, err
}
var smartContractUser = SmartContractUser{address: contractAddr}
var session = webauthn.SessionData{
Challenge: challenge,
UserID: smartContractUser.WebAuthnID(),
UserVerification: protocol.VerificationPreferred,
}
return webAuthn.CreateCredential(smartContractUser, session, credentialCreationData)
}
func VerifyAuthentication(rp *url.URL, contractAddr string, challenge string, credential *webauthn.Credential, credentialAssertionData *protocol.ParsedCredentialAssertionData) (bool, error) {
config := webauthn.Config{
RPID: rp.Host,
RPDisplayName: rp.String(),
RPOrigins: []string{rp.String()},
AttestationPreference: "",
AuthenticatorSelection: protocol.AuthenticatorSelection{},
}
webAuthn, err := webauthn.New(&config)
if err != nil {
return false, err
}
var smartContractUser = SmartContractUser{
address: contractAddr,
credential: credential,
}
var session = webauthn.SessionData{
Challenge: challenge,
UserID: smartContractUser.WebAuthnID(),
UserVerification: protocol.VerificationPreferred,
AllowedCredentialIDs: [][]byte{credential.ID},
}
if _, err := webAuthn.ValidateLogin(smartContractUser, session, credentialAssertionData); err != nil {
return false, err
}
return true, nil
}