2022-11-20 08:14:41 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2022-11-21 05:54:42 +00:00
|
|
|
"fmt"
|
2022-11-20 08:14:41 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-12-02 05:29:11 +00:00
|
|
|
const (
|
|
|
|
apiKeyMaxLength = 100
|
|
|
|
)
|
|
|
|
|
2022-11-20 08:14:41 +00:00
|
|
|
type APIKey struct {
|
2022-11-21 05:54:42 +00:00
|
|
|
ID int64
|
2022-12-02 05:29:11 +00:00
|
|
|
Name string
|
2022-11-21 05:54:42 +00:00
|
|
|
Key string
|
|
|
|
UserID int64
|
|
|
|
CreatedAt time.Time
|
2022-11-20 08:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateAPIKeyParams struct {
|
2022-12-02 05:29:11 +00:00
|
|
|
name string
|
2022-11-20 08:14:41 +00:00
|
|
|
key string
|
|
|
|
userID int64
|
|
|
|
}
|
|
|
|
|
2022-12-02 05:29:11 +00:00
|
|
|
func NewCreateAPIKeyParams(name, key string, userID int64) (CreateAPIKeyParams, error) {
|
|
|
|
if name == "" {
|
|
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
|
|
Field: "Name",
|
|
|
|
Err: ErrRequired,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) > apiKeyMaxLength {
|
|
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
|
|
Field: "Name",
|
|
|
|
Err: MaxLengthError{
|
|
|
|
Max: apiKeyMaxLength,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 08:14:41 +00:00
|
|
|
if key == "" {
|
|
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
|
|
Field: "Key",
|
|
|
|
Err: ErrRequired,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 05:17:32 +00:00
|
|
|
if userID < userIDMin {
|
2022-11-20 08:14:41 +00:00
|
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
|
|
Field: "UserID",
|
|
|
|
Err: MinError{
|
2022-11-24 05:17:32 +00:00
|
|
|
Min: userIDMin,
|
2022-11-20 08:14:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 05:29:11 +00:00
|
|
|
return CreateAPIKeyParams{name: name, key: key, userID: userID}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CreateAPIKeyParams) Name() string {
|
|
|
|
return c.name
|
2022-11-20 08:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c CreateAPIKeyParams) Key() string {
|
|
|
|
return c.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CreateAPIKeyParams) UserID() int64 {
|
|
|
|
return c.userID
|
|
|
|
}
|
2022-11-21 05:54:42 +00:00
|
|
|
|
|
|
|
type APIKeyNotFoundError struct {
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e APIKeyNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("API key (key=%s) not found", e.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e APIKeyNotFoundError) UserError() string {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e APIKeyNotFoundError) Code() ErrorCode {
|
|
|
|
return ErrorCodeEntityNotFound
|
|
|
|
}
|