Documentation ¶
Index ¶
- Variables
- type Claims
- type CreateTokenRequest
- type IJWTService
- type JWTResponse
- type JWTService
- func (s JWTService) CreateToken(createRequest CreateTokenRequest) (string, error)
- func (s JWTService) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}
- func (s JWTService) GetUserFromToken(token *jwt.Token) (string, string)
- func (s JWTService) IsTokenValid(token *jwt.Token) error
- func (s JWTService) ParseToken(tokenFromHeader string) (*jwt.Token, error)
- type JWTServiceConfig
- type JWTServiceMock
- func (m JWTServiceMock) CreateToken(createRequest CreateTokenRequest) (string, error)
- func (m JWTServiceMock) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}
- func (m JWTServiceMock) GetUserFromToken(token *jwt.Token) (string, string)
- func (m JWTServiceMock) IsTokenValid(token *jwt.Token) error
- func (m JWTServiceMock) ParseToken(tokenFromHeader string) (*jwt.Token, error)
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidIssuer error = fmt.Errorf("Invalid issuer")
var ErrInvalidToken error = fmt.Errorf("Invalid token")
var ErrInvalidUser error = fmt.Errorf("Invalid user")
var ErrTokenMissingClaims error = fmt.Errorf("Token is missing claims")
Functions ¶
This section is empty.
Types ¶
type Claims ¶
type Claims struct { jwt.StandardClaims UserID string `json:"userID"` UserName string `json:"userName"` AdditionalData map[string]interface{} }
type CreateTokenRequest ¶
type CreateTokenRequest struct { UserID string UserName string AdditionalData map[string]interface{} }
A CreateTokenRequest is used when creating a new JWT token. It contians basic information about a user, and then allows for additional data
type IJWTService ¶
type IJWTService interface { CreateToken(createRequest CreateTokenRequest) (string, error) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{} GetUserFromToken(token *jwt.Token) (string, string) ParseToken(tokenFromHeader string) (*jwt.Token, error) IsTokenValid(token *jwt.Token) error }
IJWTService describes methods for working with JWT tokens.
type JWTResponse ¶
type JWTResponse struct { Token string `json:"token"` UserID string `json:"userID"` UserName string `json:"userName"` }
JWTResponse is a generic reponse that can be used to communicate a new JWT token to a caller.
type JWTService ¶
type JWTService struct {
// contains filtered or unexported fields
}
JWTService provides methods for working with JWT tokens
func NewJWTService ¶
func NewJWTService(config JWTServiceConfig) JWTService
NewJWTService creates a new instance of the JWTService struct
func (JWTService) CreateToken ¶
func (s JWTService) CreateToken(createRequest CreateTokenRequest) (string, error)
CreateToken creates a new JWT token, encrypts it, and returns it Base64 encoded. Tokens are encrypted using AES-256
func (JWTService) GetAdditionalDataFromToken ¶
func (s JWTService) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}
GetAdditionalDataFromToken retrieves the additional data from the claims object
func (JWTService) GetUserFromToken ¶
func (s JWTService) GetUserFromToken(token *jwt.Token) (string, string)
GetUserFromToken retrieves the user ID and name from the claims in a JWT token
func (JWTService) IsTokenValid ¶
func (s JWTService) IsTokenValid(token *jwt.Token) error
IsTokenValid returns an error if there are any issues with the provided JWT token. Possible issues include:
- Missing claims
- Invalid token format
- Invalid issuer
- User doesn't have a corresponding entry in the credentials table
func (JWTService) ParseToken ¶
func (s JWTService) ParseToken(tokenFromHeader string) (*jwt.Token, error)
ParseToken decrypts the provided token and returns a JWT token object
type JWTServiceConfig ¶
type JWTServiceConfig struct { AuthSalt string AuthSecret string Issuer string TimeoutInMinutes int }
JWTServiceConfig is a configuration object for initializing the JWTService struct
type JWTServiceMock ¶
type JWTServiceMock struct { CreateTokenFunc func(createRequest CreateTokenRequest) (string, error) GetAdditionalDataFromTokenFunc func(token *jwt.Token) map[string]interface{} GetUserFromTokenFunc func(token *jwt.Token) (string, string) ParseTokenFunc func(tokenFromHeader string) (*jwt.Token, error) IsTokenValidFunc func(token *jwt.Token) error }
func (JWTServiceMock) CreateToken ¶
func (m JWTServiceMock) CreateToken(createRequest CreateTokenRequest) (string, error)
func (JWTServiceMock) GetAdditionalDataFromToken ¶
func (m JWTServiceMock) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}
func (JWTServiceMock) GetUserFromToken ¶
func (m JWTServiceMock) GetUserFromToken(token *jwt.Token) (string, string)
func (JWTServiceMock) IsTokenValid ¶
func (m JWTServiceMock) IsTokenValid(token *jwt.Token) error
func (JWTServiceMock) ParseToken ¶
func (m JWTServiceMock) ParseToken(tokenFromHeader string) (*jwt.Token, error)