2019-05-13 16:53:52 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2020-03-30 10:08:50 +01:00
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2019-05-13 16:53:52 +01:00
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// ResetPasswordTokens is interface for working with reset password tokens.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2019-05-13 16:53:52 +01:00
|
|
|
type ResetPasswordTokens interface {
|
|
|
|
// Create creates new reset password token
|
|
|
|
Create(ctx context.Context, ownerID uuid.UUID) (*ResetPasswordToken, error)
|
|
|
|
// GetBySecret retrieves ResetPasswordToken with given secret
|
|
|
|
GetBySecret(ctx context.Context, secret ResetPasswordSecret) (*ResetPasswordToken, error)
|
|
|
|
// GetByOwnerID retrieves ResetPasswordToken by ownerID
|
|
|
|
GetByOwnerID(ctx context.Context, ownerID uuid.UUID) (*ResetPasswordToken, error)
|
|
|
|
// Delete deletes ResetPasswordToken by ResetPasswordSecret
|
|
|
|
Delete(ctx context.Context, secret ResetPasswordSecret) error
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// ResetPasswordSecret stores secret of registration token.
|
2019-05-13 16:53:52 +01:00
|
|
|
type ResetPasswordSecret [32]byte
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// ResetPasswordToken describing reset password model in the database.
|
2019-05-13 16:53:52 +01:00
|
|
|
type ResetPasswordToken struct {
|
|
|
|
// Secret is PK of the table and keeps unique value for reset password token
|
|
|
|
Secret ResetPasswordSecret
|
|
|
|
// OwnerID stores current token owner ID
|
|
|
|
OwnerID *uuid.UUID
|
|
|
|
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewResetPasswordSecret creates new reset password secret.
|
2019-05-13 16:53:52 +01:00
|
|
|
func NewResetPasswordSecret() (ResetPasswordSecret, error) {
|
|
|
|
var b [32]byte
|
|
|
|
|
|
|
|
_, err := rand.Read(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return b, errs.New("error creating registration secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// String implements Stringer.
|
2019-05-13 16:53:52 +01:00
|
|
|
func (secret ResetPasswordSecret) String() string {
|
|
|
|
return base64.URLEncoding.EncodeToString(secret[:])
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// ResetPasswordSecretFromBase64 creates new reset password secret from base64 string.
|
2019-05-13 16:53:52 +01:00
|
|
|
func ResetPasswordSecretFromBase64(s string) (ResetPasswordSecret, error) {
|
|
|
|
var secret ResetPasswordSecret
|
|
|
|
|
|
|
|
b, err := base64.URLEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return secret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(secret[:], b)
|
|
|
|
|
|
|
|
return secret, nil
|
|
|
|
}
|