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.

pirms 2 gadiem
pirms 1 gada
pirms 2 gadiem
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. }