2019-02-06 09:45:48 -06:00
|
|
|
package models
|
|
|
|
|
2019-03-08 08:15:38 -06:00
|
|
|
import (
|
2019-04-30 07:42:01 -05:00
|
|
|
"context"
|
2019-03-08 08:15:38 -06:00
|
|
|
"errors"
|
2020-11-25 00:55:22 -06:00
|
|
|
"net"
|
2021-08-25 08:11:22 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2019-03-08 08:15:38 -06:00
|
|
|
)
|
2019-02-06 09:45:48 -06:00
|
|
|
|
|
|
|
// Typed errors
|
|
|
|
var (
|
|
|
|
ErrUserTokenNotFound = errors.New("user token not found")
|
|
|
|
)
|
|
|
|
|
2021-02-25 08:30:51 -06:00
|
|
|
// CreateTokenErr represents a token creation error; used in Enterprise
|
|
|
|
type CreateTokenErr struct {
|
|
|
|
StatusCode int
|
|
|
|
InternalErr error
|
|
|
|
ExternalErr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CreateTokenErr) Error() string {
|
|
|
|
if e.InternalErr != nil {
|
|
|
|
return e.InternalErr.Error()
|
|
|
|
}
|
|
|
|
return "failed to create token"
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:55:53 -06:00
|
|
|
type TokenExpiredError struct {
|
|
|
|
UserID int64
|
|
|
|
TokenID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *TokenExpiredError) Error() string { return "user token expired" }
|
|
|
|
|
2021-03-16 11:44:02 -05:00
|
|
|
type TokenRevokedError struct {
|
|
|
|
UserID int64
|
|
|
|
TokenID int64
|
|
|
|
MaxConcurrentSessions int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *TokenRevokedError) Error() string { return "user token revoked" }
|
|
|
|
|
2019-02-06 09:45:48 -06:00
|
|
|
// UserToken represents a user token
|
|
|
|
type UserToken struct {
|
|
|
|
Id int64
|
|
|
|
UserId int64
|
|
|
|
AuthToken string
|
|
|
|
PrevAuthToken string
|
|
|
|
UserAgent string
|
|
|
|
ClientIp string
|
|
|
|
AuthTokenSeen bool
|
|
|
|
SeenAt int64
|
|
|
|
RotatedAt int64
|
|
|
|
CreatedAt int64
|
|
|
|
UpdatedAt int64
|
2021-03-16 11:44:02 -05:00
|
|
|
RevokedAt int64
|
2019-02-06 09:45:48 -06:00
|
|
|
UnhashedToken string
|
|
|
|
}
|
|
|
|
|
2019-03-08 08:15:38 -06:00
|
|
|
type RevokeAuthTokenCmd struct {
|
|
|
|
AuthTokenId int64 `json:"authTokenId"`
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:45:48 -06:00
|
|
|
// UserTokenService are used for generating and validating user tokens
|
|
|
|
type UserTokenService interface {
|
2022-06-28 07:32:25 -05:00
|
|
|
CreateToken(ctx context.Context, user *user.User, clientIP net.IP, userAgent string) (*UserToken, error)
|
2019-04-30 07:42:01 -05:00
|
|
|
LookupToken(ctx context.Context, unhashedToken string) (*UserToken, error)
|
2020-11-25 00:55:22 -06:00
|
|
|
TryRotateToken(ctx context.Context, token *UserToken, clientIP net.IP, userAgent string) (bool, error)
|
2021-03-16 11:44:02 -05:00
|
|
|
RevokeToken(ctx context.Context, token *UserToken, soft bool) error
|
2019-04-30 07:42:01 -05:00
|
|
|
RevokeAllUserTokens(ctx context.Context, userId int64) error
|
|
|
|
GetUserToken(ctx context.Context, userId, userTokenId int64) (*UserToken, error)
|
|
|
|
GetUserTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
2021-03-16 11:44:02 -05:00
|
|
|
GetUserRevokedTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
2019-02-06 09:45:48 -06:00
|
|
|
}
|
2021-08-25 08:11:22 -05:00
|
|
|
|
2022-07-29 09:30:46 -05:00
|
|
|
type ActiveTokenService interface {
|
|
|
|
ActiveTokenCount(ctx context.Context) (int64, error)
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
type UserTokenBackgroundService interface {
|
|
|
|
registry.BackgroundService
|
|
|
|
}
|