2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-15 12:00:08 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package console
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/subtle"
|
2019-01-30 15:04:40 +00:00
|
|
|
"fmt"
|
2018-11-14 10:50:15 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-30 13:40:13 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-08 14:05:14 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-12-20 20:10:27 +00:00
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
"storj.io/storj/pkg/auth"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 20:10:27 +00:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2018-12-26 14:00:53 +00:00
|
|
|
const (
|
|
|
|
// maxLimit specifies the limit for all paged queries
|
2019-01-30 15:04:40 +00:00
|
|
|
maxLimit = 50
|
|
|
|
tokenExpirationTime = 24 * time.Hour
|
2019-02-05 17:31:53 +00:00
|
|
|
|
|
|
|
// DefaultPasswordCost is the hashing complexity
|
|
|
|
DefaultPasswordCost = bcrypt.DefaultCost
|
|
|
|
// TestPasswordCost is the hashing complexity to use for testing
|
|
|
|
TestPasswordCost = bcrypt.MinCost
|
2018-12-26 14:00:53 +00:00
|
|
|
)
|
2018-12-17 14:28:58 +00:00
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
// Service is handling accounts related logic
|
|
|
|
type Service struct {
|
|
|
|
Signer
|
|
|
|
|
|
|
|
store DB
|
2018-11-21 15:51:43 +00:00
|
|
|
log *zap.Logger
|
2019-02-05 17:31:53 +00:00
|
|
|
|
|
|
|
passwordCost int
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns new instance of Service
|
2019-02-05 17:31:53 +00:00
|
|
|
func NewService(log *zap.Logger, signer Signer, store DB, passwordCost int) (*Service, error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
if signer == nil {
|
|
|
|
return nil, errs.New("signer can't be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if store == nil {
|
|
|
|
return nil, errs.New("store can't be nil")
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
if log == nil {
|
|
|
|
return nil, errs.New("log can't be nil")
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:31:53 +00:00
|
|
|
if passwordCost == 0 {
|
|
|
|
passwordCost = bcrypt.DefaultCost
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{Signer: signer, store: store, log: log, passwordCost: passwordCost}, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// CreateUser gets password hash value and creates new inactive User
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) CreateUser(ctx context.Context, user CreateUser) (u *User, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-10 15:57:06 +00:00
|
|
|
if err := user.IsValid(); err != nil {
|
2018-11-29 16:23:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// TODO: store original email input in the db,
|
2019-01-08 13:54:12 +00:00
|
|
|
// add normalization
|
|
|
|
email := normalizeEmail(user.Email)
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
u, err = s.store.Users().GetByEmail(ctx, email)
|
|
|
|
if u != nil {
|
|
|
|
return nil, errs.New(fmt.Sprintf("%s is already in use", email))
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:31:53 +00:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), s.passwordCost)
|
2018-11-21 15:51:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-08 13:54:12 +00:00
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
u, err = s.store.Users().Insert(ctx, &User{
|
2019-02-05 17:22:17 +00:00
|
|
|
Email: user.Email,
|
2018-12-10 15:57:06 +00:00
|
|
|
FirstName: user.FirstName,
|
|
|
|
LastName: user.LastName,
|
|
|
|
PasswordHash: hash,
|
2018-11-21 15:51:43 +00:00
|
|
|
})
|
2019-01-30 15:04:40 +00:00
|
|
|
|
|
|
|
// TODO: send "finish registration email" when email service will be ready
|
|
|
|
//activationToken, err := s.GenerateActivationToken(ctx, u.ID, email, u.CreatedAt.Add(tokenExpirationTime))
|
2019-02-05 17:22:17 +00:00
|
|
|
//if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
//}
|
2019-01-30 15:04:40 +00:00
|
|
|
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateActivationToken - is a method for generating activation token
|
|
|
|
func (s *Service) GenerateActivationToken(ctx context.Context, id uuid.UUID, email string, expirationDate time.Time) (activationToken string, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
claims := &consoleauth.Claims{
|
|
|
|
ID: id,
|
|
|
|
Email: email,
|
|
|
|
Expiration: expirationDate,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.createToken(claims)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateAccount - is a method for activating user account after registration
|
|
|
|
func (s *Service) ActivateAccount(ctx context.Context, activationToken string) (authToken string, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
token, err := consoleauth.FromBase64URLString(activationToken)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, err := s.authenticate(token)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.store.Users().Get(ctx, claims.ID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
if user.Email != "" {
|
|
|
|
return "", errs.New("account is already active")
|
|
|
|
}
|
|
|
|
|
|
|
|
if now.After(user.CreatedAt.Add(tokenExpirationTime)) {
|
|
|
|
return "", errs.New("activation token is expired")
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Email = normalizeEmail(claims.Email)
|
|
|
|
err = s.store.Users().Update(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims = &consoleauth.Claims{
|
|
|
|
ID: user.ID,
|
|
|
|
Expiration: time.Now().Add(tokenExpirationTime),
|
|
|
|
}
|
|
|
|
|
|
|
|
authToken, err = s.createToken(claims)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authToken, err
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 10:31:15 +00:00
|
|
|
// Token authenticates User by credentials and returns auth token
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) Token(ctx context.Context, email, password string) (token string, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-08 13:54:12 +00:00
|
|
|
|
|
|
|
email = normalizeEmail(email)
|
|
|
|
|
2018-12-10 13:47:48 +00:00
|
|
|
user, err := s.store.Users().GetByEmail(ctx, email)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword(user.PasswordHash, []byte(password))
|
|
|
|
if err != nil {
|
2018-12-18 17:43:02 +00:00
|
|
|
return "", ErrUnauthorized.New("password is incorrect: %s", err.Error())
|
2018-12-10 13:47:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
claims := consoleauth.Claims{
|
2018-11-14 10:50:15 +00:00
|
|
|
ID: user.ID,
|
2019-01-30 15:04:40 +00:00
|
|
|
Expiration: time.Now().Add(tokenExpirationTime),
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:10:27 +00:00
|
|
|
token, err = s.createToken(&claims)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:31:15 +00:00
|
|
|
// GetUser returns User by id
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) GetUser(ctx context.Context, id uuid.UUID) (u *User, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = GetAuth(ctx)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
return s.store.Users().Get(ctx, id)
|
|
|
|
}
|
2018-11-14 10:50:15 +00:00
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
// UpdateAccount updates User
|
|
|
|
func (s *Service) UpdateAccount(ctx context.Context, info UserInfo) (err error) {
|
2018-12-20 20:10:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = GetAuth(ctx)
|
2018-12-24 12:52:52 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-11-28 10:31:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:23:44 +00:00
|
|
|
if err = info.IsValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:54:12 +00:00
|
|
|
//TODO: store original email input in the db,
|
|
|
|
// add normalization
|
|
|
|
email := normalizeEmail(info.Email)
|
|
|
|
|
2018-11-28 10:31:15 +00:00
|
|
|
return s.store.Users().Update(ctx, &User{
|
2018-12-24 12:52:52 +00:00
|
|
|
ID: auth.User.ID,
|
2018-11-28 10:31:15 +00:00
|
|
|
FirstName: info.FirstName,
|
|
|
|
LastName: info.LastName,
|
2019-01-08 13:54:12 +00:00
|
|
|
Email: email,
|
2018-12-10 15:57:06 +00:00
|
|
|
PasswordHash: nil,
|
2018-11-28 10:31:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
// ChangePassword updates password for a given user
|
|
|
|
func (s *Service) ChangePassword(ctx context.Context, pass, newPass string) (err error) {
|
2018-12-20 20:10:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-24 12:52:52 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-12-10 15:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword(auth.User.PasswordHash, []byte(pass))
|
2018-12-10 15:57:06 +00:00
|
|
|
if err != nil {
|
2018-12-18 17:43:02 +00:00
|
|
|
return ErrUnauthorized.New("origin password is incorrect: %s", err.Error())
|
2018-12-10 15:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validatePassword(newPass); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:31:53 +00:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(newPass), s.passwordCost)
|
2018-12-10 15:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
auth.User.PasswordHash = hash
|
|
|
|
return s.store.Users().Update(ctx, &auth.User)
|
2018-12-10 15:57:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
// DeleteAccount deletes User
|
|
|
|
func (s *Service) DeleteAccount(ctx context.Context, password string) (err error) {
|
2018-12-20 20:10:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-14 16:14:17 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-11-27 14:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-14 16:14:17 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword(auth.User.PasswordHash, []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return ErrUnauthorized.New("origin password is incorrect")
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return s.store.Users().Delete(ctx, auth.User.ID)
|
2018-11-27 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
// GetProject is a method for querying project by id
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) GetProject(ctx context.Context, projectID uuid.UUID) (p *Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = GetAuth(ctx)
|
2018-11-26 10:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.store.Projects().Get(ctx, projectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsersProjects is a method for querying all projects
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) GetUsersProjects(ctx context.Context) (ps []Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-06 15:19:47 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-11-26 10:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:19:47 +00:00
|
|
|
return s.store.Projects().GetByUserID(ctx, auth.User.ID)
|
2018-11-26 10:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 13:14:10 +00:00
|
|
|
// CreateProject is a method for creating new project
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) CreateProject(ctx context.Context, projectInfo ProjectInfo) (p *Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-27 14:20:58 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-11-26 10:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
project := &Project{
|
2019-01-28 18:20:33 +00:00
|
|
|
Description: projectInfo.Description,
|
|
|
|
Name: projectInfo.Name,
|
2018-11-26 10:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 12:29:01 +00:00
|
|
|
transaction, err := s.store.BeginTx(ctx)
|
2018-12-06 15:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-26 14:00:53 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = errs.Combine(err, transaction.Rollback())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = transaction.Commit()
|
|
|
|
}()
|
|
|
|
|
2018-12-10 12:29:01 +00:00
|
|
|
prj, err := transaction.Projects().Insert(ctx, project)
|
|
|
|
if err != nil {
|
2018-12-26 14:00:53 +00:00
|
|
|
return nil, err
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = transaction.ProjectMembers().Insert(ctx, auth.User.ID, prj.ID)
|
|
|
|
if err != nil {
|
2018-12-26 14:00:53 +00:00
|
|
|
return nil, err
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
2018-12-06 15:19:47 +00:00
|
|
|
|
2018-12-26 14:00:53 +00:00
|
|
|
return prj, nil
|
2018-11-26 10:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteProject is a method for deleting project by id
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) DeleteProject(ctx context.Context, projectID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = GetAuth(ctx)
|
2018-12-18 17:43:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:18:08 +00:00
|
|
|
// TODO: before deletion we should check if user is a project member
|
2018-11-26 10:47:23 +00:00
|
|
|
return s.store.Projects().Delete(ctx, projectID)
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:20:23 +00:00
|
|
|
// UpdateProject is a method for updating project description by id
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) UpdateProject(ctx context.Context, projectID uuid.UUID, description string) (p *Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = GetAuth(ctx)
|
2018-11-26 10:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := s.store.Projects().Get(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("Project doesn't exist!")
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:20:23 +00:00
|
|
|
project.Description = description
|
2018-11-26 10:47:23 +00:00
|
|
|
|
|
|
|
err = s.store.Projects().Update(ctx, project)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return project, nil
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
// AddProjectMembers adds users by email to given project
|
|
|
|
func (s *Service) AddProjectMembers(ctx context.Context, projectID uuid.UUID, emails []string) (err error) {
|
2018-12-20 20:10:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-27 15:30:15 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
if _, err = s.isProjectMember(ctx, auth.User.ID, projectID); err != nil {
|
|
|
|
return ErrUnauthorized.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
var userIDs []uuid.UUID
|
|
|
|
var userErr errs.Group
|
|
|
|
|
|
|
|
// collect user querying errors
|
|
|
|
for _, email := range emails {
|
|
|
|
user, err := s.store.Users().GetByEmail(ctx, email)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
userErr.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs = append(userIDs, user.ID)
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
if err = userErr.Err(); err != nil {
|
2018-12-21 15:41:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add project members in transaction scope
|
|
|
|
tx, err := s.store.BeginTx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = errs.Combine(err, tx.Rollback())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
}()
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
for _, uID := range userIDs {
|
2018-12-27 15:30:15 +00:00
|
|
|
_, err = tx.ProjectMembers().Insert(ctx, uID, projectID)
|
2018-12-21 15:41:53 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-12-27 15:30:15 +00:00
|
|
|
return err
|
2018-12-21 15:41:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
return nil
|
2018-12-06 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
// DeleteProjectMembers removes users by email from given project
|
|
|
|
func (s *Service) DeleteProjectMembers(ctx context.Context, projectID uuid.UUID, emails []string) (err error) {
|
2018-12-20 20:10:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-27 15:30:15 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
if _, err = s.isProjectMember(ctx, auth.User.ID, projectID); err != nil {
|
|
|
|
return ErrUnauthorized.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
var userIDs []uuid.UUID
|
|
|
|
var userErr errs.Group
|
|
|
|
|
|
|
|
// collect user querying errors
|
|
|
|
for _, email := range emails {
|
|
|
|
user, err := s.store.Users().GetByEmail(ctx, email)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
userErr.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs = append(userIDs, user.ID)
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
if err = userErr.Err(); err != nil {
|
2018-12-21 15:41:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete project members in transaction scope
|
|
|
|
tx, err := s.store.BeginTx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = errs.Combine(err, tx.Rollback())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
}()
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
for _, uID := range userIDs {
|
2018-12-27 15:30:15 +00:00
|
|
|
err = tx.ProjectMembers().Delete(ctx, uID, projectID)
|
2018-12-21 15:41:53 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-12-27 15:30:15 +00:00
|
|
|
return err
|
2018-12-21 15:41:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
return nil
|
2018-12-06 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:38:42 +00:00
|
|
|
// GetProjectMembers returns ProjectMembers for given Project
|
2018-12-28 12:07:35 +00:00
|
|
|
func (s *Service) GetProjectMembers(ctx context.Context, projectID uuid.UUID, pagination Pagination) (pm []ProjectMember, err error) {
|
2018-12-20 20:10:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = GetAuth(ctx)
|
2018-12-10 11:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-28 12:07:35 +00:00
|
|
|
if pagination.Limit > maxLimit {
|
|
|
|
pagination.Limit = maxLimit
|
2018-12-19 13:03:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 12:07:35 +00:00
|
|
|
return s.store.ProjectMembers().GetByProjectID(ctx, projectID, pagination)
|
2018-12-10 11:38:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
// CreateAPIKey creates new api key
|
|
|
|
func (s *Service) CreateAPIKey(ctx context.Context, projectID uuid.UUID, name string) (*APIKeyInfo, *APIKey, error) {
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-26 14:00:53 +00:00
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
auth, err := GetAuth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
_, err = s.isProjectMember(ctx, auth.User.ID, projectID)
|
2018-12-26 14:00:53 +00:00
|
|
|
if err != nil {
|
2018-12-27 15:30:15 +00:00
|
|
|
return nil, nil, ErrUnauthorized.Wrap(err)
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 10:08:51 +00:00
|
|
|
key, err := CreateAPIKey()
|
2018-12-26 14:00:53 +00:00
|
|
|
if err != nil {
|
2018-12-27 15:30:15 +00:00
|
|
|
return nil, nil, err
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
info, err := s.store.APIKeys().Create(ctx, *key, APIKeyInfo{
|
2018-12-26 14:00:53 +00:00
|
|
|
Name: name,
|
|
|
|
ProjectID: projectID,
|
|
|
|
})
|
2018-12-27 15:30:15 +00:00
|
|
|
return info, key, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIKeyInfo retrieves api key by id
|
|
|
|
func (s *Service) GetAPIKeyInfo(ctx context.Context, id uuid.UUID) (*APIKeyInfo, error) {
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
auth, err := GetAuth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := s.store.APIKeys().Get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.isProjectMember(ctx, auth.User.ID, key.ProjectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrUnauthorized.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAPIKey deletes api key by id
|
|
|
|
func (s *Service) DeleteAPIKey(ctx context.Context, id uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
auth, err := GetAuth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := s.store.APIKeys().Get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.isProjectMember(ctx, auth.User.ID, key.ProjectID)
|
|
|
|
if err != nil {
|
|
|
|
return ErrUnauthorized.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.store.APIKeys().Delete(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIKeysInfoByProjectID retrieves all api keys for a given project
|
|
|
|
func (s *Service) GetAPIKeysInfoByProjectID(ctx context.Context, projectID uuid.UUID) (info []APIKeyInfo, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
auth, err := GetAuth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.isProjectMember(ctx, auth.User.ID, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrUnauthorized.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.store.APIKeys().GetByProjectID(ctx, projectID)
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// Authorize validates token from context and returns authorized Authorization
|
2018-12-20 20:10:27 +00:00
|
|
|
func (s *Service) Authorize(ctx context.Context) (a Authorization, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-27 14:20:58 +00:00
|
|
|
tokenS, ok := auth.GetAPIKey(ctx)
|
2018-11-21 15:51:43 +00:00
|
|
|
if !ok {
|
2018-12-18 17:43:02 +00:00
|
|
|
return Authorization{}, ErrUnauthorized.New("no api key was provided")
|
2018-11-21 15:51:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
token, err := consoleauth.FromBase64URLString(string(tokenS))
|
2018-11-21 15:51:43 +00:00
|
|
|
if err != nil {
|
2018-12-18 17:43:02 +00:00
|
|
|
return Authorization{}, ErrUnauthorized.Wrap(err)
|
2018-11-21 15:51:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
claims, err := s.authenticate(token)
|
|
|
|
if err != nil {
|
2018-12-18 17:43:02 +00:00
|
|
|
return Authorization{}, ErrUnauthorized.Wrap(err)
|
2018-11-27 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.authorize(ctx, claims)
|
|
|
|
if err != nil {
|
2018-12-18 17:43:02 +00:00
|
|
|
return Authorization{}, ErrUnauthorized.Wrap(err)
|
2018-11-27 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Authorization{
|
|
|
|
User: *user,
|
|
|
|
Claims: *claims,
|
|
|
|
}, nil
|
2018-11-21 15:51:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// createToken creates string representation
|
2019-01-15 13:03:24 +00:00
|
|
|
func (s *Service) createToken(claims *consoleauth.Claims) (string, error) {
|
2018-11-22 10:38:58 +00:00
|
|
|
json, err := claims.JSON()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
token := consoleauth.Token{Payload: json}
|
2018-11-22 10:38:58 +00:00
|
|
|
err = signToken(&token, s.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token.String(), nil
|
|
|
|
}
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
// authenticate validates token signature and returns authenticated *satelliteauth.Authorization
|
2019-01-15 13:03:24 +00:00
|
|
|
func (s *Service) authenticate(token consoleauth.Token) (*consoleauth.Claims, error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
signature := token.Signature
|
|
|
|
|
2018-11-27 14:20:58 +00:00
|
|
|
err := signToken(&token, s.Signer)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if subtle.ConstantTimeCompare(signature, token.Signature) != 1 {
|
|
|
|
return nil, errs.New("incorrect signature")
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
claims, err := consoleauth.FromJSON(token.Payload)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return claims, nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
// authorize checks claims and returns authorized User
|
2019-01-15 13:03:24 +00:00
|
|
|
func (s *Service) authorize(ctx context.Context, claims *consoleauth.Claims) (*User, error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
if !claims.Expiration.IsZero() && claims.Expiration.Before(time.Now()) {
|
2018-11-21 15:51:43 +00:00
|
|
|
return nil, errs.New("token is outdated")
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
user, err := s.store.Users().Get(ctx, claims.ID)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
2018-11-21 15:51:43 +00:00
|
|
|
return nil, errs.New("authorization failed. no user with id: %s", claims.ID.String())
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
return user, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
2018-12-27 15:30:15 +00:00
|
|
|
|
|
|
|
// isProjectMember is return type of isProjectMember service method
|
|
|
|
type isProjectMember struct {
|
|
|
|
project *Project
|
|
|
|
membership *ProjectMember
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrNoMembership is error type of not belonging to a specific project
|
|
|
|
var ErrNoMembership = errs.Class("no membership error")
|
|
|
|
|
|
|
|
// isProjectMember checks if the user is a member of given project
|
|
|
|
func (s *Service) isProjectMember(ctx context.Context, userID uuid.UUID, projectID uuid.UUID) (result isProjectMember, err error) {
|
|
|
|
project, err := s.store.Projects().Get(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships, err := s.store.ProjectMembers().GetByMemberID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, membership := range memberships {
|
|
|
|
if membership.ProjectID == projectID {
|
|
|
|
result.membership = &membership
|
|
|
|
result.project = project
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 12:07:35 +00:00
|
|
|
return isProjectMember{}, ErrNoMembership.New("user %s is not a member of project %s", userID, project.ID)
|
2018-12-27 15:30:15 +00:00
|
|
|
}
|