-
Notifications
You must be signed in to change notification settings - Fork 332
/
invite.go
66 lines (57 loc) · 1.57 KB
/
invite.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
package auth
import (
"context"
"time"
"github.com/treeverse/lakefs/pkg/auth/email"
"github.com/treeverse/lakefs/pkg/auth/model"
"github.com/treeverse/lakefs/pkg/logging"
)
type InviteHandler interface {
InviteUser(ctx context.Context, email string) error
IsInviteSupported() bool
}
type EmailInviteHandler struct {
svc Service
log logging.Logger
emailer *email.Emailer
}
func NewEmailInviteHandler(svc Service, log logging.Logger, emailer *email.Emailer) *EmailInviteHandler {
return &EmailInviteHandler{svc: svc, log: log, emailer: emailer}
}
const (
DefaultInvitePasswordExpiration = 6 * time.Hour
)
func (i *EmailInviteHandler) inviteUserRequest(emailAddr string) error {
secret := i.svc.SecretStore().SharedSecret()
currentTime := time.Now()
token, err := GenerateJWTResetPassword(secret, emailAddr, currentTime, currentTime.Add(DefaultInvitePasswordExpiration))
if err != nil {
return err
}
params := map[string]string{
"token": token,
"email": emailAddr,
}
err = i.emailer.SendInviteUserEmail([]string{emailAddr}, params)
if err != nil {
return err
}
i.log.WithField("email", emailAddr).Info("invite email sent")
return nil
}
func (i *EmailInviteHandler) InviteUser(ctx context.Context, email string) error {
u := &model.User{
CreatedAt: time.Now().UTC(),
Username: email,
Source: "internal",
Email: &email,
}
_, err := i.svc.CreateUser(ctx, u)
if err != nil {
return err
}
return i.inviteUserRequest(email)
}
func (i *EmailInviteHandler) IsInviteSupported() bool {
return i.emailer != nil && i.emailer.Params.SMTPHost != ""
}