The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839
  1. package auth
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/golang-jwt/jwt/v4"
  7. "github.com/spf13/viper"
  8. )
  9. type Claims struct {
  10. Username string `json:"username"`
  11. Privileges uint `json:"privileges"`
  12. jwt.RegisteredClaims
  13. }
  14. func GetUserClaims(r *http.Request) (rights *Claims, err error) {
  15. var token *jwt.Token
  16. authHeader := r.Header.Get("Authorization")
  17. if authHeader == "" {
  18. return nil, nil
  19. }
  20. claims := &Claims{}
  21. _, t, _ := strings.Cut(r.Header.Get("Authorization"), "Bearer ")
  22. token, _ = jwt.ParseWithClaims(t, claims, func(token *jwt.Token) (interface{}, error) {
  23. if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
  24. return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
  25. }
  26. return []byte(viper.GetString("app.secretKey")), nil
  27. })
  28. if !token.Valid {
  29. return nil, nil
  30. }
  31. return claims, nil
  32. }