-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebauthn.go
53 lines (47 loc) · 1.42 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
package webauthn
import (
"context"
"crypto/rand"
"encoding/base64"
"github.com/spiretechnology/go-jwt/v2"
"github.com/spiretechnology/go-webauthn/pkg/challenge"
"github.com/spiretechnology/go-webauthn/pkg/codec"
"github.com/spiretechnology/go-webauthn/pkg/pubkey"
)
type WebAuthn interface {
CreateRegistration(ctx context.Context, user User) (*RegistrationChallenge, error)
VerifyRegistration(ctx context.Context, user User, res *RegistrationResponse) (*RegistrationResult, error)
CreateAuthentication(ctx context.Context, user User) (*AuthenticationChallenge, error)
VerifyAuthentication(ctx context.Context, user User, res *AuthenticationResponse) (*AuthenticationResult, error)
}
type Options struct {
RP RelyingParty
Codec codec.Codec
PublicKeyTypes []pubkey.KeyType
Credentials Credentials
Tokener Tokener
ChallengeFunc func() (challenge.Challenge, error)
}
func New(options Options) WebAuthn {
if options.Codec == nil {
options.Codec = base64.RawURLEncoding
}
if options.PublicKeyTypes == nil {
options.PublicKeyTypes = pubkey.AllKeyTypes
}
if options.ChallengeFunc == nil {
options.ChallengeFunc = challenge.GenerateChallenge
}
if options.Tokener == nil {
secret := make([]byte, 64)
rand.Read(secret)
options.Tokener = NewJwtTokener(
jwt.HS256Signer(secret),
jwt.HS256Verifier(secret),
)
}
return &webauthn{options}
}
type webauthn struct {
options Options
}