7999d24f81
this commit updates our monkit dependency to the v3 version where it outputs in an influx style. this makes discovery much easier as many tools are built to look at it this way. graphite and rothko will suffer some due to no longer being a tree based on dots. hopefully time will exist to update rothko to index based on the new metric format. it adds an influx output for the statreceiver so that we can write to influxdb v1 or v2 directly. Change-Id: Iae9f9494a6d29cfbd1f932a5e71a891b490415ff
189 lines
4.4 KiB
Go
189 lines
4.4 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package referrals
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"storj.io/common/pb"
|
|
"storj.io/common/rpc"
|
|
"storj.io/common/signing"
|
|
"storj.io/common/storj"
|
|
"storj.io/storj/private/dbutil"
|
|
"storj.io/storj/satellite/console"
|
|
)
|
|
|
|
var mon = monkit.Package()
|
|
|
|
var (
|
|
// ErrUsedEmail is an error class for reporting already used emails.
|
|
ErrUsedEmail = errs.Class("email used error")
|
|
)
|
|
|
|
// Config is for referrals service.
|
|
type Config struct {
|
|
ReferralManagerURL storj.NodeURL
|
|
}
|
|
|
|
// Service allows communicating with the Referral Manager
|
|
//
|
|
// architecture: Service
|
|
type Service struct {
|
|
log *zap.Logger
|
|
signer signing.Signer
|
|
config Config
|
|
dialer rpc.Dialer
|
|
db console.Users
|
|
passwordCost int
|
|
}
|
|
|
|
// NewService returns a service for handling referrals information.
|
|
func NewService(log *zap.Logger, signer signing.Signer, config Config, dialer rpc.Dialer, db console.Users, passwordCost int) *Service {
|
|
return &Service{
|
|
log: log,
|
|
signer: signer,
|
|
config: config,
|
|
dialer: dialer,
|
|
db: db,
|
|
passwordCost: passwordCost,
|
|
}
|
|
}
|
|
|
|
// GetTokens returns tokens based on user ID.
|
|
func (service *Service) GetTokens(ctx context.Context, userID *uuid.UUID) (tokens []uuid.UUID, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
if userID.IsZero() {
|
|
return nil, errs.New("user ID is not defined")
|
|
}
|
|
|
|
conn, err := service.referralManagerConn(ctx)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
defer func() {
|
|
err = conn.Close()
|
|
}()
|
|
|
|
client := pb.NewDRPCReferralManagerClient(conn.Raw())
|
|
response, err := client.GetTokens(ctx, &pb.GetTokensRequest{
|
|
OwnerUserId: userID[:],
|
|
OwnerSatelliteId: service.signer.ID(),
|
|
})
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
tokensInBytes := response.GetTokenSecrets()
|
|
if tokensInBytes != nil && len(tokensInBytes) == 0 {
|
|
return nil, errs.New("no available tokens")
|
|
}
|
|
|
|
tokens = make([]uuid.UUID, len(tokensInBytes))
|
|
for i := range tokensInBytes {
|
|
token, err := dbutil.BytesToUUID(tokensInBytes[i])
|
|
if err != nil {
|
|
service.log.Debug("failed to convert bytes to UUID", zap.Error(err))
|
|
continue
|
|
}
|
|
tokens[i] = token
|
|
}
|
|
|
|
return tokens, nil
|
|
}
|
|
|
|
// CreateUser validates user's registration information and creates a new user.
|
|
func (service *Service) CreateUser(ctx context.Context, user CreateUser) (_ *console.User, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
if err := user.IsValid(); err != nil {
|
|
return nil, ErrValidation.Wrap(err)
|
|
}
|
|
|
|
if len(user.ReferralToken) == 0 {
|
|
return nil, errs.New("referral token is not defined")
|
|
}
|
|
|
|
_, err = service.db.GetByEmail(ctx, user.Email)
|
|
if err == nil {
|
|
return nil, ErrUsedEmail.New("")
|
|
}
|
|
|
|
userID, err := uuid.New()
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
err = service.redeemToken(ctx, userID, user.ReferralToken)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), service.passwordCost)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
newUser := &console.User{
|
|
ID: *userID,
|
|
Email: user.Email,
|
|
FullName: user.FullName,
|
|
ShortName: user.ShortName,
|
|
PasswordHash: hash,
|
|
}
|
|
|
|
u, err := service.db.Insert(ctx,
|
|
newUser,
|
|
)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
return u, nil
|
|
}
|
|
|
|
func (service *Service) redeemToken(ctx context.Context, userID *uuid.UUID, token string) error {
|
|
conn, err := service.referralManagerConn(ctx)
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
defer func() {
|
|
err = conn.Close()
|
|
}()
|
|
|
|
if userID.IsZero() || len(token) == 0 {
|
|
return errs.New("invalid argument")
|
|
}
|
|
|
|
referralToken, err := uuid.Parse(token)
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
|
|
client := pb.NewDRPCReferralManagerClient(conn.Raw())
|
|
_, err = client.RedeemToken(ctx, &pb.RedeemTokenRequest{
|
|
Token: referralToken[:],
|
|
RedeemUserId: userID[:],
|
|
RedeemSatelliteId: service.signer.ID(),
|
|
})
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (service *Service) referralManagerConn(ctx context.Context) (*rpc.Conn, error) {
|
|
if service.config.ReferralManagerURL.IsZero() {
|
|
return nil, errs.New("missing referral manager url configuration")
|
|
}
|
|
|
|
return service.dialer.DialAddressID(ctx, service.config.ReferralManagerURL.Address, service.config.ReferralManagerURL.ID)
|
|
}
|