102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
|
// Copyright (C) 2018 Storj Labs, Inc.
|
||
|
// See LICENSE for copying information.
|
||
|
|
||
|
package satellitedb
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
|
||
|
"github.com/skyrings/skyring-common/tools/uuid"
|
||
|
|
||
|
"storj.io/storj/satellite/console"
|
||
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
||
|
)
|
||
|
|
||
|
type resetPasswordTokens struct {
|
||
|
db dbx.Methods
|
||
|
}
|
||
|
|
||
|
// Create creates new reset password token
|
||
|
func (rpt *resetPasswordTokens) Create(ctx context.Context, ownerID uuid.UUID) (*console.ResetPasswordToken, error) {
|
||
|
secret, err := console.NewResetPasswordSecret()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
resToken, err := rpt.db.Create_ResetPasswordToken(
|
||
|
ctx,
|
||
|
dbx.ResetPasswordToken_Secret(secret[:]),
|
||
|
dbx.ResetPasswordToken_OwnerId(ownerID[:]),
|
||
|
)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return resetPasswordTokenFromDBX(resToken)
|
||
|
}
|
||
|
|
||
|
// GetBySecret retrieves ResetPasswordToken with given Secret
|
||
|
func (rpt *resetPasswordTokens) GetBySecret(ctx context.Context, secret console.ResetPasswordSecret) (*console.ResetPasswordToken, error) {
|
||
|
resToken, err := rpt.db.Get_ResetPasswordToken_By_Secret(
|
||
|
ctx,
|
||
|
dbx.ResetPasswordToken_Secret(secret[:]),
|
||
|
)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return resetPasswordTokenFromDBX(resToken)
|
||
|
}
|
||
|
|
||
|
// GetByOwnerID retrieves ResetPasswordToken by ownerID
|
||
|
func (rpt *resetPasswordTokens) GetByOwnerID(ctx context.Context, ownerID uuid.UUID) (*console.ResetPasswordToken, error) {
|
||
|
resToken, err := rpt.db.Get_ResetPasswordToken_By_OwnerId(
|
||
|
ctx,
|
||
|
dbx.ResetPasswordToken_OwnerId(ownerID[:]),
|
||
|
)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return resetPasswordTokenFromDBX(resToken)
|
||
|
}
|
||
|
|
||
|
// Delete deletes ResetPasswordToken by ResetPasswordSecret
|
||
|
func (rpt *resetPasswordTokens) Delete(ctx context.Context, secret console.ResetPasswordSecret) error {
|
||
|
_, err := rpt.db.Delete_ResetPasswordToken_By_Secret(
|
||
|
ctx,
|
||
|
dbx.ResetPasswordToken_Secret(secret[:]),
|
||
|
)
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// resetPasswordTokenFromDBX is used for creating ResetPasswordToken entity from autogenerated dbx.ResetPasswordToken struct
|
||
|
func resetPasswordTokenFromDBX(resetToken *dbx.ResetPasswordToken) (*console.ResetPasswordToken, error) {
|
||
|
if resetToken == nil {
|
||
|
return nil, errors.New("token parameter is nil")
|
||
|
}
|
||
|
|
||
|
var secret [32]byte
|
||
|
|
||
|
copy(secret[:], resetToken.Secret)
|
||
|
|
||
|
result := &console.ResetPasswordToken{
|
||
|
Secret: secret,
|
||
|
OwnerID: nil,
|
||
|
CreatedAt: resetToken.CreatedAt,
|
||
|
}
|
||
|
|
||
|
if resetToken.OwnerId != nil {
|
||
|
ownerID, err := bytesToUUID(resetToken.OwnerId)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
result.OwnerID = &ownerID
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|