The backend for the project formerly known as signet, now known as beignet.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

39 rindas
856 B

  1. package auth
  2. import (
  3. "fmt"
  4. "github.com/golang-jwt/jwt/v4"
  5. "github.com/spf13/viper"
  6. "net/http"
  7. "strings"
  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. }