Auth: Refactor auth package (#58920)
* Auth: move interface to its own file * Auth: move to test package * Auth: move quota consts to auth file * Auth: move service to impl package * Auth: move interfaces and related models to auth package * Auth: Create sub package and type alias to avoid circular dependency
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models/usertoken"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
const (
|
||||
QuotaTargetSrv quota.TargetSrv = "auth"
|
||||
QuotaTarget quota.Target = "session"
|
||||
)
|
||||
|
||||
type ActiveTokenService interface {
|
||||
ActiveTokenCount(ctx context.Context, _ *quota.ScopeParameters) (*quota.Map, error)
|
||||
}
|
||||
|
||||
// Typed errors
|
||||
var (
|
||||
ErrUserTokenNotFound = errors.New("user token not found")
|
||||
)
|
||||
|
||||
type TokenRevokedError = usertoken.TokenRevokedError
|
||||
|
||||
// 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"
|
||||
}
|
||||
|
||||
type TokenExpiredError struct {
|
||||
UserID int64
|
||||
TokenID int64
|
||||
}
|
||||
|
||||
func (e *TokenExpiredError) Error() string { return "user token expired" }
|
||||
|
||||
type UserToken = usertoken.UserToken
|
||||
|
||||
type RevokeAuthTokenCmd struct {
|
||||
AuthTokenId int64 `json:"authTokenId"`
|
||||
}
|
||||
|
||||
// UserTokenService are used for generating and validating user tokens
|
||||
type UserTokenService interface {
|
||||
CreateToken(ctx context.Context, user *user.User, clientIP net.IP, userAgent string) (*UserToken, error)
|
||||
LookupToken(ctx context.Context, unhashedToken string) (*UserToken, error)
|
||||
TryRotateToken(ctx context.Context, token *UserToken, clientIP net.IP, userAgent string) (bool, error)
|
||||
RevokeToken(ctx context.Context, token *UserToken, soft bool) error
|
||||
RevokeAllUserTokens(ctx context.Context, userId int64) error
|
||||
GetUserToken(ctx context.Context, userId, userTokenId int64) (*UserToken, error)
|
||||
GetUserTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
||||
GetUserRevokedTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
||||
}
|
||||
|
||||
type UserTokenBackgroundService interface {
|
||||
registry.BackgroundService
|
||||
}
|
||||
Reference in New Issue
Block a user