2016-09-29 02:44:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-12-07 15:42:58 +00:00
|
|
|
"encoding/base64"
|
2021-07-19 18:08:41 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server"
|
2016-12-28 16:55:03 +00:00
|
|
|
"html/template"
|
2016-09-29 02:44:05 +00:00
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mail"
|
2021-04-05 20:28:43 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-09-29 02:44:05 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc Service) InviteNewUser(ctx context.Context, payload fleet.InvitePayload) (*fleet.Invite, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
// verify that the user with the given email does not already exist
|
|
|
|
_, err := svc.ds.UserByEmail(*payload.Email)
|
|
|
|
if err == nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("email", "a user with this account already exists")
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
if _, ok := err.(fleet.NotFoundError); !ok {
|
2016-09-29 02:44:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the user who created the invite
|
2021-04-05 20:28:43 +00:00
|
|
|
v, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing viewer context for create invite")
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
2021-04-05 20:28:43 +00:00
|
|
|
inviter := v.User
|
2016-09-29 02:44:05 +00:00
|
|
|
|
2021-07-19 18:08:41 +00:00
|
|
|
random, err := server.GenerateRandomText(svc.config.App.TokenKeySize)
|
2016-09-29 02:44:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-07 15:42:58 +00:00
|
|
|
token := base64.URLEncoding.EncodeToString([]byte(random))
|
2016-09-29 02:44:05 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
invite := &fleet.Invite{
|
2021-04-07 01:27:10 +00:00
|
|
|
Email: *payload.Email,
|
|
|
|
InvitedBy: inviter.ID,
|
|
|
|
Token: token,
|
|
|
|
GlobalRole: payload.GlobalRole,
|
2021-04-12 16:51:05 +00:00
|
|
|
Teams: payload.Teams,
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
if payload.Position != nil {
|
|
|
|
invite.Position = *payload.Position
|
|
|
|
}
|
|
|
|
if payload.Name != nil {
|
|
|
|
invite.Name = *payload.Name
|
|
|
|
}
|
2017-05-10 16:26:05 +00:00
|
|
|
if payload.SSOEnabled != nil {
|
|
|
|
invite.SSOEnabled = *payload.SSOEnabled
|
|
|
|
}
|
2016-09-29 02:44:05 +00:00
|
|
|
|
|
|
|
invite, err = svc.ds.NewInvite(invite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:30 +00:00
|
|
|
config, err := svc.AppConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:37:00 +00:00
|
|
|
invitedBy := inviter.Name
|
|
|
|
if invitedBy == "" {
|
2021-06-24 20:42:29 +00:00
|
|
|
invitedBy = inviter.Email
|
2017-01-17 21:37:00 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
inviteEmail := fleet.Email{
|
2019-04-04 22:56:15 +00:00
|
|
|
Subject: "You are Invited to Fleet",
|
2016-12-20 21:54:30 +00:00
|
|
|
To: []string{invite.Email},
|
|
|
|
Config: config,
|
2020-03-30 02:22:04 +00:00
|
|
|
Mailer: &mail.InviteMailer{
|
2021-06-24 20:42:29 +00:00
|
|
|
Invite: invite,
|
|
|
|
BaseURL: template.URL(config.ServerURL + svc.config.Server.URLPrefix),
|
|
|
|
AssetURL: getAssetURL(),
|
|
|
|
OrgName: config.OrgName,
|
|
|
|
InvitedBy: invitedBy,
|
2016-12-28 16:55:03 +00:00
|
|
|
},
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
err = svc.mailService.SendEmail(inviteEmail)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return invite, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListInvites(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Invite, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-14 15:59:27 +00:00
|
|
|
return svc.ds.ListInvites(opt)
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) VerifyInvite(ctx context.Context, token string) (*fleet.Invite, error) {
|
2021-06-03 23:24:15 +00:00
|
|
|
// skipauth: There is no viewer context at this point. We rely on verifying
|
|
|
|
// the invite for authNZ.
|
|
|
|
svc.authz.SkipAuthorization(ctx)
|
|
|
|
|
2016-12-30 01:58:12 +00:00
|
|
|
invite, err := svc.ds.InviteByToken(token)
|
2016-09-29 02:44:05 +00:00
|
|
|
if err != nil {
|
2016-12-30 01:58:12 +00:00
|
|
|
return nil, err
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if invite.Token != token {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("invite_token", "Invite Token does not match Email Address.")
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expiresAt := invite.CreatedAt.Add(svc.config.App.InviteTokenValidityPeriod)
|
|
|
|
if svc.clock.Now().After(expiresAt) {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("invite_token", "Invite token has expired.")
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 01:58:12 +00:00
|
|
|
return invite, nil
|
2016-09-29 02:44:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
func (svc *Service) DeleteInvite(ctx context.Context, id uint) error {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-01-04 18:18:21 +00:00
|
|
|
return svc.ds.DeleteInvite(id)
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|