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"
|
|
|
|
)
|
2019-02-06 09:45:48 -06:00
|
|
|
|
|
|
|
// Typed errors
|
|
|
|
var (
|
|
|
|
ErrUserTokenNotFound = errors.New("user token not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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 {
|
2019-04-30 07:42:01 -05:00
|
|
|
CreateToken(ctx context.Context, userId int64, clientIP, userAgent string) (*UserToken, error)
|
|
|
|
LookupToken(ctx context.Context, unhashedToken string) (*UserToken, error)
|
|
|
|
TryRotateToken(ctx context.Context, token *UserToken, clientIP, userAgent string) (bool, error)
|
|
|
|
RevokeToken(ctx context.Context, token *UserToken) error
|
|
|
|
RevokeAllUserTokens(ctx context.Context, userId int64) error
|
|
|
|
ActiveTokenCount(ctx context.Context) (int64, error)
|
|
|
|
GetUserToken(ctx context.Context, userId, userTokenId int64) (*UserToken, error)
|
|
|
|
GetUserTokens(ctx context.Context, userId int64) ([]*UserToken, error)
|
2019-02-06 09:45:48 -06:00
|
|
|
}
|