feat: integration v2 and API key PART 1/3
This commit is contained in:
1 parent
7e9bd9ba30
commit
844842fdae
40 files changed
+3782
-64
No files matched your search
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user