Advertisement
Guest User

Untitled

a guest
Apr 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "github.com/dgrijalva/jwt-go"
  6. "time"
  7. )
  8.  
  9. func main() {
  10. hs256Encodedtoken()
  11. }
  12.  
  13. func hs256Encodedtoken() {
  14. expiresAt := time.Now().Add(time.Minute * 1).Unix()
  15. token := jwt.New(jwt.SigningMethodHS256)
  16. token.Claims = &AuthTokenClaim{
  17. StandardClaims: &jwt.StandardClaims{
  18. ExpiresAt: expiresAt,
  19. },
  20. User: User{Username: "bastien", Password: "toto"},
  21. }
  22. tokenString, error := token.SignedString([]byte("secret"))
  23. if error != nil {
  24. fmt.Println(error)
  25. }
  26. fmt.Println(tokenString)
  27. }
  28.  
  29. // User ...
  30. // Custom object which can be stored in the claims
  31. type User struct {
  32. Username string `json:"username"`
  33. Password string `json:"password"`
  34. }
  35.  
  36. // AuthTokenClaim ...
  37. // This is the cliam object which gets parsed from the authorization header
  38. type AuthTokenClaim struct {
  39. *jwt.StandardClaims
  40. User
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement