2022-08-02 09:55:19 -05:00
|
|
|
package apikey
|
|
|
|
|
2022-08-04 07:19:09 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-08-04 07:19:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNotFound = errors.New("API key not found")
|
|
|
|
ErrInvalid = errors.New("invalid API key")
|
|
|
|
ErrInvalidExpiration = errors.New("negative value for SecondsToLive")
|
|
|
|
ErrDuplicate = errors.New("API key, organization ID and name must be unique")
|
|
|
|
)
|
|
|
|
|
|
|
|
type APIKey struct {
|
|
|
|
Id int64
|
|
|
|
OrgId int64
|
|
|
|
Name string
|
|
|
|
Key string
|
2022-08-10 04:56:48 -05:00
|
|
|
Role org.RoleType
|
2022-08-04 07:19:09 -05:00
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
LastUsedAt *time.Time `xorm:"last_used_at"`
|
|
|
|
Expires *int64
|
|
|
|
ServiceAccountId *int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k APIKey) TableName() string { return "api_key" }
|
|
|
|
|
|
|
|
// swagger:model
|
|
|
|
type AddCommand struct {
|
2022-08-10 04:56:48 -05:00
|
|
|
Name string `json:"name" binding:"Required"`
|
|
|
|
Role org.RoleType `json:"role" binding:"Required"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
Key string `json:"-"`
|
|
|
|
SecondsToLive int64 `json:"secondsToLive"`
|
|
|
|
Result *APIKey `json:"-"`
|
2022-08-04 07:19:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteCommand struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetApiKeysQuery struct {
|
|
|
|
OrgId int64
|
|
|
|
IncludeExpired bool
|
2022-08-10 04:56:48 -05:00
|
|
|
User *user.SignedInUser
|
2022-08-04 07:19:09 -05:00
|
|
|
Result []*APIKey
|
|
|
|
}
|
|
|
|
type GetByNameQuery struct {
|
|
|
|
KeyName string
|
|
|
|
OrgId int64
|
|
|
|
Result *APIKey
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetByIDQuery struct {
|
|
|
|
ApiKeyId int64
|
|
|
|
Result *APIKey
|
|
|
|
}
|