/
memory.go
56 lines (48 loc) · 1.44 KB
/
memory.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
package storage
import (
"context"
"encoding/base64"
"fmt"
"sync"
"github.com/pomerium/webauthn"
)
// A InMemoryBackend stores data in-memory.
type InMemoryBackend struct {
mu sync.Mutex
credentials map[string]*webauthn.Credential
}
// NewInMemoryBackend creates a new InMemoryBackend.
func NewInMemoryBackend() *InMemoryBackend {
return &InMemoryBackend{
credentials: make(map[string]*webauthn.Credential),
}
}
// GetCredential retrieves a credential.
func (backend *InMemoryBackend) GetCredential(ctx context.Context, credentialID []byte) (*webauthn.Credential, error) {
key := base64.RawURLEncoding.EncodeToString(credentialID)
backend.mu.Lock()
credential, ok := backend.credentials[key]
backend.mu.Unlock()
if !ok {
return nil, fmt.Errorf("not found")
}
return credential, nil
}
// SetAuthenticateRequest is a no-op.
func (backend *InMemoryBackend) SetAuthenticateRequest(ctx context.Context, req *WebAuthnAuthenticateRequest) error {
// ignored
return nil
}
// SetCredential saves a credential.
func (backend *InMemoryBackend) SetCredential(ctx context.Context, credential *webauthn.Credential) error {
key := base64.RawURLEncoding.EncodeToString(credential.ID)
backend.mu.Lock()
backend.credentials[key] = credential
backend.mu.Unlock()
return nil
}
// SetRegisterRequest is a no-op.
func (backend *InMemoryBackend) SetRegisterRequest(ctx context.Context, req *WebAuthnRegisterRequest) error {
// ignored
return nil
}