This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
local.go
265 lines (221 loc) · 7.52 KB
/
local.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2020 the Exposure Notifications Verification Server authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"context"
"encoding/json"
"fmt"
"github.com/gorilla/sessions"
)
const (
sessionKeyLocalCookie = sessionKey("localCookie")
)
type localAuth struct{}
// NewLocal creates a new auth provider for local auth.
func NewLocal(ctx context.Context) (Provider, error) {
return &localAuth{}, nil
}
// CheckRevoked checks if the users auth has been revoked.
func (a *localAuth) CheckRevoked(ctx context.Context, session *sessions.Session) error {
data, err := a.loadCookie(ctx, session)
if err != nil {
return err
}
if data.Revoked {
return fmt.Errorf("session is revoked")
}
return nil
}
// StoreSession stores information about the session.
func (a *localAuth) StoreSession(ctx context.Context, session *sessions.Session, i *SessionInfo) error {
if i == nil || i.Data == nil {
a.ClearSession(ctx, session)
return ErrSessionInfoMissing
}
email, ok := i.Data["email"].(string)
if !ok {
a.ClearSession(ctx, session)
return fmt.Errorf("missing email: %w", ErrSessionInfoMissing)
}
emailVerified, ok := i.Data["email_verified"].(bool)
if !ok {
a.ClearSession(ctx, session)
return fmt.Errorf("missing email_verified: %w", ErrSessionInfoMissing)
}
mfaEnabled, ok := i.Data["mfa_enabled"].(bool)
if !ok {
a.ClearSession(ctx, session)
return fmt.Errorf("missing mfa_enabled: %w", ErrSessionInfoMissing)
}
revoked, ok := i.Data["revoked"].(bool)
if !ok {
a.ClearSession(ctx, session)
return fmt.Errorf("missing revoked: %w", ErrSessionInfoMissing)
}
// Convert ID token to long-lived cookie
cookie, err := json.Marshal(&localCookieData{
Email: email,
EmailVerified: emailVerified,
MFAEnabled: mfaEnabled,
Revoked: revoked,
})
if err != nil {
a.ClearSession(ctx, session)
return err
}
// Set cookie
if err := sessionSet(session, sessionKeyLocalCookie, string(cookie)); err != nil {
a.ClearSession(ctx, session)
return err
}
return nil
}
// ClearSession removes any session information for this auth.
func (a *localAuth) ClearSession(ctx context.Context, session *sessions.Session) {
sessionClear(session, sessionKeyLocalCookie)
}
// RevokeSession revokes the upstream session. It's a no-op for this provider.
func (a *localAuth) RevokeSession(ctx context.Context, session *sessions.Session) error {
a.ClearSession(ctx, session)
return nil
}
// CreateUser creates a user in the upstream auth system with the given name and
// email. It returns true if the user was created or false if the user already
// exists.
func (a *localAuth) CreateUser(ctx context.Context, name, email, pass string, sendInvite bool, emailer InviteUserEmailFunc) (bool, error) {
if !sendInvite {
return true, nil
}
if emailer == nil {
return true, nil
}
// For local auth, this is a noop since the controllers create the user in the
// database.
// Send the welcome email.
inviteLink, err := a.passwordResetLink(ctx, email)
if err != nil {
return true, err
}
if err := emailer(ctx, inviteLink); err != nil {
return true, fmt.Errorf("failed to send new user invitation email: %w", err)
}
return true, nil
}
// EmailAddress extracts the users email from the session.
func (a *localAuth) EmailAddress(ctx context.Context, session *sessions.Session) (string, error) {
data, err := a.loadCookie(ctx, session)
if err != nil {
return "", err
}
return data.Email, nil
}
// EmailVerified returns true if the current user is verified, false otherwise.
func (a *localAuth) EmailVerified(ctx context.Context, session *sessions.Session) (bool, error) {
data, err := a.loadCookie(ctx, session)
if err != nil {
return false, err
}
return data.EmailVerified, nil
}
// MFAEnabled returns whether MFA is enabled on the account.
func (a *localAuth) MFAEnabled(ctx context.Context, session *sessions.Session) (bool, error) {
data, err := a.loadCookie(ctx, session)
if err != nil {
return false, err
}
return data.MFAEnabled, nil
}
// ChangePassword changes the users password. The data is not used. Since local
// auth does not use passwords, this is a noop.
func (a *localAuth) ChangePassword(ctx context.Context, newPassword string, data interface{}) error {
return nil
}
// SendResetPasswordEmail resets the password for the given user. If the user does not
// exist, an error is returned.
func (a *localAuth) SendResetPasswordEmail(ctx context.Context, email string, emailer ResetPasswordEmailFunc) error {
if emailer == nil {
return fmt.Errorf("emailer is required for local auth")
}
resetLink, err := a.passwordResetLink(ctx, email)
if err != nil {
return err
}
if err := emailer(ctx, resetLink); err != nil {
return fmt.Errorf("failed to send password reset email: %w", err)
}
return nil
}
// VerifyPasswordResetCode does nothing. It returns the empty string.
func (a *localAuth) VerifyPasswordResetCode(ctx context.Context, code string) (string, error) {
return "", nil
}
// SendEmailVerificationEmail sends an message to the currently authenticated
// user, asking them to verify ownership of the email address.
func (a *localAuth) SendEmailVerificationEmail(ctx context.Context, email string, data interface{}, emailer EmailVerificationEmailFunc) error {
if emailer == nil {
return fmt.Errorf("emailer is required for local auth")
}
verifyLink, err := a.emailVerificationLink(ctx, email)
if err != nil {
return err
}
if err := emailer(ctx, verifyLink); err != nil {
return fmt.Errorf("failed to send email verification email: %w", err)
}
return nil
}
// passwordResetLink generates and returns the password reset link for the given
// email (user).
func (a *localAuth) passwordResetLink(ctx context.Context, email string) (string, error) {
return "test-link", nil
}
// emailVerificationLink generates an email verification link for the given
// email.
func (a *localAuth) emailVerificationLink(ctx context.Context, email string) (string, error) {
return "test-link", nil
}
type localCookieData struct {
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
MFAEnabled bool `json:"mfa_enabled"`
Revoked bool `json:"revoked"`
}
// dataFromCookie extracts the information from the provided local cookie, if it
// exists. The local cookie is actually just a JSON payload.
func (a *localAuth) dataFromCookie(ctx context.Context, cookie string) (*localCookieData, error) {
var data localCookieData
if err := json.Unmarshal([]byte(cookie), &data); err != nil {
return nil, err
}
return &data, nil
}
// loadCookie loads and parses the local cookie from the session.
func (a *localAuth) loadCookie(ctx context.Context, session *sessions.Session) (*localCookieData, error) {
raw, err := sessionGet(session, sessionKeyLocalCookie)
if err != nil {
a.ClearSession(ctx, session)
return nil, err
}
cookie, ok := raw.(string)
if !ok || cookie == "" {
a.ClearSession(ctx, session)
return nil, ErrSessionMissing
}
data, err := a.dataFromCookie(ctx, cookie)
if err != nil {
a.ClearSession(ctx, session)
return nil, err
}
return data, nil
}