main
 1package authz
 2
 3import (
 4	"net/http"
 5	"strings"
 6
 7	"github.com/lestrrat-go/jwx/v3/jwt"
 8	"github.com/xlgmokha/x/pkg/log"
 9)
10
11func TokenFrom(r *http.Request) jwt.Token {
12	authorization := r.Header.Get("Authorization")
13	if authorization == "" || !strings.Contains(authorization, "Bearer") {
14		return jwt.New()
15	}
16
17	token, err := jwt.ParseRequest(r,
18		jwt.WithContext(r.Context()),
19		jwt.WithHeaderKey("Authorization"),
20		jwt.WithValidate(false), // TODO:: Connect this to a JSON Web Key Set
21		jwt.WithVerify(false),   // TODO:: Connect this to a JSON Web Key Set
22	)
23
24	if err != nil {
25		log.WithFields(r.Context(), log.Fields{"error": err})
26		return jwt.New()
27	}
28
29	return token
30}