2015-01-27 15:26:11 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-28 19:30:09 +08:00
|
|
|
var (
|
|
|
|
|
ErrApiKeyNotFound = errors.New("API key not found")
|
|
|
|
|
ErrInvalidApiKey = errors.New("invalid API key")
|
|
|
|
|
ErrInvalidApiKeyExpiration = errors.New("negative value for SecondsToLive")
|
|
|
|
|
ErrDuplicateApiKey = errors.New("API key, organization ID and name must be unique")
|
|
|
|
|
)
|
2015-01-27 15:26:11 +08:00
|
|
|
|
|
|
|
|
type ApiKey struct {
|
2021-10-20 20:36:11 +08:00
|
|
|
Id int64
|
|
|
|
|
OrgId int64
|
|
|
|
|
Name string
|
|
|
|
|
Key string
|
|
|
|
|
Role RoleType
|
|
|
|
|
Created time.Time
|
|
|
|
|
Updated time.Time
|
|
|
|
|
Expires *int64
|
|
|
|
|
ServiceAccountId int64
|
2015-01-27 15:26:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
|
// COMMANDS
|
|
|
|
|
type AddApiKeyCommand struct {
|
2021-10-20 20:36:11 +08:00
|
|
|
Name string `json:"name" binding:"Required"`
|
|
|
|
|
Role RoleType `json:"role" binding:"Required"`
|
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
|
Key string `json:"-"`
|
|
|
|
|
SecondsToLive int64 `json:"secondsToLive"`
|
|
|
|
|
ServiceAccountId int64 `json:"serviceAccount"`
|
|
|
|
|
CreateNewServiceAccount bool `json:"createServiceAccount"`
|
2015-01-27 15:26:11 +08:00
|
|
|
|
|
|
|
|
Result *ApiKey `json:"-"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeleteApiKeyCommand struct {
|
2015-02-24 03:07:49 +08:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
|
OrgId int64 `json:"-"`
|
2015-01-27 15:26:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
|
// QUERIES
|
|
|
|
|
|
|
|
|
|
type GetApiKeysQuery struct {
|
2019-06-26 14:47:03 +08:00
|
|
|
OrgId int64
|
2019-11-20 19:14:57 +08:00
|
|
|
IncludeExpired bool
|
2019-06-26 14:47:03 +08:00
|
|
|
Result []*ApiKey
|
2015-01-27 15:26:11 +08:00
|
|
|
}
|
|
|
|
|
|
2015-02-27 00:23:28 +08:00
|
|
|
type GetApiKeyByNameQuery struct {
|
|
|
|
|
KeyName string
|
|
|
|
|
OrgId int64
|
|
|
|
|
Result *ApiKey
|
2015-01-27 15:26:11 +08:00
|
|
|
}
|
|
|
|
|
|
2015-04-08 14:59:12 +08:00
|
|
|
type GetApiKeyByIdQuery struct {
|
|
|
|
|
ApiKeyId int64
|
|
|
|
|
Result *ApiKey
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 15:26:11 +08:00
|
|
|
// ------------------------
|
|
|
|
|
// DTO & Projections
|
|
|
|
|
|
|
|
|
|
type ApiKeyDTO struct {
|
2019-06-26 14:47:03 +08:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Role RoleType `json:"role"`
|
|
|
|
|
Expiration *time.Time `json:"expiration,omitempty"`
|
2015-01-27 15:26:11 +08:00
|
|
|
}
|