Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "github.com/dgrijalva/jwt-go"
- "time"
- )
- func main() {
- hs256Encodedtoken()
- }
- func hs256Encodedtoken() {
- expiresAt := time.Now().Add(time.Minute * 1).Unix()
- token := jwt.New(jwt.SigningMethodHS256)
- token.Claims = &AuthTokenClaim{
- StandardClaims: &jwt.StandardClaims{
- ExpiresAt: expiresAt,
- },
- User: User{Username: "bastien", Password: "toto"},
- }
- tokenString, error := token.SignedString([]byte("secret"))
- if error != nil {
- fmt.Println(error)
- }
- fmt.Println(tokenString)
- }
- // User ...
- // Custom object which can be stored in the claims
- type User struct {
- Username string `json:"username"`
- Password string `json:"password"`
- }
- // AuthTokenClaim ...
- // This is the cliam object which gets parsed from the authorization header
- type AuthTokenClaim struct {
- *jwt.StandardClaims
- User
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement