-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovider.go
212 lines (182 loc) · 6.92 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package email
import (
"context"
"crypto/rand"
"fmt"
"io"
"math/big"
"strings"
"time"
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
ethcrypto "github.com/0xsequence/ethkit/go-ethereum/crypto"
"github.com/0xsequence/go-sequence/intents"
"github.com/0xsequence/waas-authenticator/proto"
"github.com/0xsequence/waas-authenticator/proto/builder"
"github.com/0xsequence/waas-authenticator/rpc/attestation"
"github.com/0xsequence/waas-authenticator/rpc/auth"
"github.com/0xsequence/waas-authenticator/rpc/tenant"
)
// AuthProvider is a Verifier that uses a secret code, delivered to user's email address, as the auth challenge.
type AuthProvider struct {
Sender Sender
Builder builder.Builder
}
func NewAuthProvider(sender Sender, builder builder.Builder) auth.Provider {
return &AuthProvider{
Sender: sender,
Builder: builder,
}
}
func (*AuthProvider) IsEnabled(tenant *proto.TenantData) bool {
return tenant.AuthConfig.Email.Enabled == true
}
// InitiateAuth for Email ignores any preexisting auth session data. Instead, if called multiple times, the auth session
// is replaced. This allows the user to resend the verification code in case of issues. Note that this invalidates the
// previous auth session - only the most recent one is stored and used in Verify.
func (p *AuthProvider) InitiateAuth(
ctx context.Context,
verifCtx *proto.VerificationContext,
verifier string,
sessionID string,
storeFn auth.StoreVerificationContextFn,
) (*intents.IntentResponseAuthInitiated, error) {
att := attestation.FromContext(ctx)
tnt := tenant.FromContext(ctx)
// the verifier consists of the email address and sessionID separated by ';'
emailAddress, expectedSessionID, err := extractVerifier(verifier)
if err != nil {
return nil, err
}
if sessionID != expectedSessionID {
return nil, fmt.Errorf("invalid session ID")
}
// TODO: validate email address
// Retrieve the email template from the Builder.
tplType := builder.EmailTemplateType_LOGIN
tpl, err := p.Builder.GetEmailTemplate(ctx, tnt.ProjectID, &tplType)
if err != nil {
return nil, fmt.Errorf("failed to build email template: %w", err)
}
// generate the secret verification code to be sent to the user
secretCode, err := randomDigits(att, 6)
if err != nil {
return nil, err
}
// client salt is sent back to the client in the intent response
clientSalt, err := randomHex(att, 12)
if err != nil {
return nil, err
}
// server salt is sent to the WaaS API and stored in the auth session
serverSalt, err := randomHex(att, 12)
if err != nil {
return nil, err
}
// clientAnswer is the value that we expect the client to produce
clientAnswer := hexutil.Encode(ethcrypto.Keccak256([]byte(clientSalt + secretCode)))
// serverAnswer is the value we compare the answer against during verification
serverAnswer := hexutil.Encode(ethcrypto.Keccak256([]byte(serverSalt + clientAnswer)))
// WaaS API is expected to store the answer and salt for later verification
verifCtx = &proto.VerificationContext{
ProjectID: tnt.ProjectID,
SessionID: sessionID,
IdentityType: proto.IdentityType_Email,
Verifier: verifier,
Challenge: &serverSalt, // the SERVER salt is a challenge in server's context
Answer: &serverAnswer, // the final answer, after hashing clientAnswer with serverSalt
ExpiresAt: time.Now().Add(30 * time.Minute),
}
if err := storeFn(ctx, verifCtx); err != nil {
return nil, err
}
// Build the email message. The template is expected to contain the `{auth_code}` tag to be replaced with the
// generated secret code.
msg := &Message{
Recipient: emailAddress,
Subject: tpl.Subject,
HTML: strings.Replace(*tpl.Template, "{auth_code}", secretCode, 1),
Text: tpl.IntroText + "\n\n" + secretCode,
}
if tpl.FromEmail != nil {
msg.Source = *tpl.FromEmail
}
if tpl.SesConfig != nil {
msg.SourceARN = tpl.SesConfig.SourceARN
msg.AccessRoleARN = tpl.SesConfig.AccessRoleARN
}
if err := p.Sender.Send(ctx, msg); err != nil {
return nil, fmt.Errorf("failed to send email: %w", err)
}
// Client should combine the challenge from the response with the code from the email address and hash it.
// The resulting value is the clientAnswer that is then send with the openSession intent and passed to Verify.
res := &intents.IntentResponseAuthInitiated{
SessionID: verifCtx.SessionID,
IdentityType: intents.IdentityType_Email,
ExpiresIn: int(verifCtx.ExpiresAt.Sub(time.Now()).Seconds()),
Challenge: &clientSalt, // the CLIENT salt is a challenge in client's context
}
return res, nil
}
// Verify requires the auth session to exist as it contains the challenge and final answer. The challenge (server salt)
// from the auth session is combined with the client's answer and the resulting value compared with the final answer.
// Verify returns the identity if this is successful.
func (p *AuthProvider) Verify(ctx context.Context, verifCtx *proto.VerificationContext, sessionID string, answer string) (proto.Identity, error) {
if verifCtx == nil {
return proto.Identity{}, fmt.Errorf("auth session not found")
}
if verifCtx.Challenge == nil || verifCtx.Answer == nil {
return proto.Identity{}, fmt.Errorf("auth session did not have challenge/answer")
}
// the verifier consists of the email address and sessionID separated by ';'
emailAddress, verifierSessionID, err := extractVerifier(verifCtx.Verifier)
if err != nil {
return proto.Identity{}, err
}
if verifierSessionID != sessionID {
return proto.Identity{}, fmt.Errorf("invalid session ID")
}
// challenge here is the server salt; combined with the client's answer and hashed it produces the serverAnswer
serverAnswer := hexutil.Encode(ethcrypto.Keccak256([]byte(*verifCtx.Challenge + answer)))
if serverAnswer != *verifCtx.Answer {
return proto.Identity{}, fmt.Errorf("incorrect answer")
}
ident := proto.Identity{
Type: proto.IdentityType_Email,
Subject: emailAddress,
Email: emailAddress,
}
return ident, nil
}
// ValidateTenant always succeeds as there are no email-specific settings to validate.
func (p *AuthProvider) ValidateTenant(ctx context.Context, tenant *proto.TenantData) error {
return nil
}
func extractVerifier(verifier string) (emailAddress string, sessionID string, err error) {
emailAddress, sessionID, found := strings.Cut(verifier, ";")
if !found {
return "", "", fmt.Errorf("invalid verifier")
}
return Normalize(emailAddress), sessionID, nil
}
func Normalize(email string) string {
return strings.ToLower(strings.TrimSpace(email))
}
func randomDigits(source io.Reader, n int) (string, error) {
const digits = "0123456789"
result := make([]byte, n)
for i := range result {
num, err := rand.Int(source, big.NewInt(int64(len(digits))))
if err != nil {
return "", err
}
result[i] = digits[num.Int64()]
}
return string(result), nil
}
func randomHex(source io.Reader, n int) (string, error) {
b := make([]byte, n)
if _, err := source.Read(b); err != nil {
return "", err
}
return hexutil.Encode(b), nil
}