Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var GenerateJWT = func(userID, secret string, expiresIn int) (string, error) { claims := jwt.MapClaims{} claims["authorized"] = true claims["sub"] = userID claims["iat"] = time.Now().Unix() if expiresIn != 0 { claims["exp"] = time.Now().Add(time.Minute * time.Duration(expiresIn)).Unix() } at := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) return at.SignedString([]byte(secret)) }
GenerateJWT creates a JWT with custom
var VerifyJWT = func(token, secret string) (string, error) { jwtToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } return []byte(secret), nil }) if err != nil { return "", err } claims, ok := jwtToken.Claims.(jwt.MapClaims) if ok && jwtToken.Valid { userID, ok := claims["sub"].(string) if !ok { return "", errors.New("Failed to verify JWT and extract the subject") } return userID, nil } return "", errors.New("Failed to verify JWT and extract the subject") }
VerifyJWT checks that the JWT is well formed (i.e. it can be parsed) and returns the user ID encoded in the JWT.
Functions ¶
func GetTokenFromRequest ¶
GetTokenFromRequest extracts the token from an HTTP request
func WriteErrorResponse ¶
func WriteErrorResponse(w http.ResponseWriter, statusCode int, err error)
WriteErrorResponse is a helper function that returns JSON response for errors
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client has all the handlers for user related activities
func (*Client) CreateUser ¶
func (c *Client) CreateUser(w http.ResponseWriter, r *http.Request)
CreateUser is a handler that creates a new user
func (*Client) IsAuthenticated ¶
IsAuthenticated is a middleware that checks if a requester is authenticated or not
type CreateUserResponse ¶
CreateUserResponse is the payload for a successful created user We don't want to send password details in the response
type LoginPayload ¶
LoginPayload is the payload for a login request
type LoginResponse ¶
type LoginResponse struct {
Token string `json:"token"`
}
LoginResponse is the payload for a successful login response