Documentation
¶
Overview ¶
Package jwt generates and validates JSON Web Tokens.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateToken ¶
GenerateToken generates a new JWT token.
Example ¶
Sign a token, validate it with the same secret, then read a claim back out.
package main
import (
"fmt"
"github.com/rahmadafandi/fibr/jwt"
)
func main() {
secret := "super-secret-key"
token, err := jwt.GenerateToken(jwt.MapClaims{"sub": "user-42", "role": "admin"}, secret)
if err != nil {
panic(err)
}
parsed, err := jwt.ValidateToken(token, secret)
if err != nil {
panic(err)
}
claims, err := jwt.ExtractClaimsFromJwt(parsed)
if err != nil {
panic(err)
}
fmt.Println("sub:", claims["sub"])
fmt.Println("role:", claims["role"])
}
Output: sub: user-42 role: admin
Types ¶
type MapClaims ¶
MapClaims is re-exported from golang-jwt so callers need not import that package directly.
func Claims ¶
Claims gets claims from a JWT token in the fiber context or set claims to the fiber context.
func ExtractClaimsFromJwt ¶
ExtractClaimsFromJwt extracts the MapClaims from a parsed JWT token. It returns an error if the token's Claims field is not a MapClaims value.
type Token ¶
Token is re-exported from golang-jwt so callers need not import that package directly.
func ValidateToken ¶
ValidateToken validates a JWT token.
Example (WrongSecret) ¶
Validating with the wrong secret fails.
package main
import (
"fmt"
"github.com/rahmadafandi/fibr/jwt"
)
func main() {
token, _ := jwt.GenerateToken(jwt.MapClaims{"sub": "user-42"}, "right-secret")
_, err := jwt.ValidateToken(token, "wrong-secret")
fmt.Println("valid:", err == nil)
}
Output: valid: false