/
provider.go
96 lines (79 loc) · 2 KB
/
provider.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
91
92
93
94
95
96
package siwe
import (
"crypto/ecdsa"
"sort"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/util/clock"
"github.com/authgear/authgear-server/pkg/util/uuid"
)
// nolint: golint
type SIWEService interface {
VerifyMessage(msg string, signature string) (*model.SIWEWallet, *ecdsa.PublicKey, error)
}
type Provider struct {
Store *Store
Clock clock.Clock
SIWE SIWEService
}
func (p *Provider) List(userID string) ([]*identity.SIWE, error) {
ss, err := p.Store.List(userID)
if err != nil {
return nil, err
}
sortIdentities(ss)
return ss, nil
}
func (p *Provider) Get(userID, id string) (*identity.SIWE, error) {
return p.Store.Get(userID, id)
}
func (p *Provider) GetByMessage(msg string, signature string) (*identity.SIWE, error) {
wallet, _, err := p.SIWE.VerifyMessage(msg, signature)
if err != nil {
return nil, err
}
return p.Store.GetByAddress(wallet.ChainID, wallet.Address)
}
func (p *Provider) GetMany(ids []string) ([]*identity.SIWE, error) {
return p.Store.GetMany(ids)
}
func (p *Provider) New(
userID string,
msg string,
signature string,
) (*identity.SIWE, error) {
wallet, pubKey, err := p.SIWE.VerifyMessage(msg, signature)
if err != nil {
return nil, err
}
encodedPublicKey, err := model.NewSIWEPublicKey(pubKey)
if err != nil {
return nil, err
}
i := &identity.SIWE{
ID: uuid.New(),
UserID: userID,
Address: wallet.Address,
ChainID: wallet.ChainID,
Data: &model.SIWEVerifiedData{
Message: msg,
Signature: signature,
EncodedPublicKey: encodedPublicKey,
},
}
return i, nil
}
func (p *Provider) Create(i *identity.SIWE) error {
now := p.Clock.NowUTC()
i.CreatedAt = now
i.UpdatedAt = now
return p.Store.Create(i)
}
func (p *Provider) Delete(i *identity.SIWE) error {
return p.Store.Delete(i)
}
func sortIdentities(is []*identity.SIWE) {
sort.Slice(is, func(i, j int) bool {
return is[i].CreatedAt.Before(is[j].CreatedAt)
})
}