43 lines
987 B
Go
43 lines
987 B
Go
package apikey
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var ErrAPIKeyNotFound = errors.New("api key not found")
|
|
var ErrAPIKeyInvalid = errors.New("api key invalid")
|
|
var ErrExpiryNotInFuture = errors.New("api key expiry not in the future")
|
|
|
|
const RawKeyPrefix = "rcms_"
|
|
|
|
type Status string
|
|
|
|
const (
|
|
StatusActive Status = "active"
|
|
StatusExpired Status = "expired"
|
|
StatusRevoked Status = "revoked"
|
|
)
|
|
|
|
type APIKey struct {
|
|
ID uint32 `json:"id"`
|
|
UserID uint32 `json:"user_id"`
|
|
Name string `json:"name"`
|
|
Prefix string `json:"prefix"`
|
|
HashedKey string `json:"hashed_key"`
|
|
ExpiresAt *time.Time `json:"expires_at"`
|
|
RevokedAt *time.Time `json:"revoked_at"`
|
|
LastUsedAt *time.Time `json:"last_used_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (k APIKey) StatusAt(now time.Time) Status {
|
|
if k.RevokedAt != nil {
|
|
return StatusRevoked
|
|
}
|
|
if k.ExpiresAt != nil && !now.Before(*k.ExpiresAt) {
|
|
return StatusExpired
|
|
}
|
|
return StatusActive
|
|
}
|