summaryrefslogtreecommitdiff
path: root/pkg/authz/token.go
blob: 2794bf4ab4fa0aa6137d6095d0bbeafddd62d34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package authz

import (
	"net/http"
	"strings"

	"github.com/lestrrat-go/jwx/v3/jwt"
	"github.com/xlgmokha/x/pkg/log"
)

func TokenFrom(r *http.Request) jwt.Token {
	authorization := r.Header.Get("Authorization")
	if authorization == "" || !strings.Contains(authorization, "Bearer") {
		return jwt.New()
	}

	token, err := jwt.ParseRequest(r,
		jwt.WithContext(r.Context()),
		jwt.WithHeaderKey("Authorization"),
		jwt.WithValidate(false), // TODO:: Connect this to a JSON Web Key Set
		jwt.WithVerify(false),   // TODO:: Connect this to a JSON Web Key Set
	)

	if err != nil {
		log.WithFields(r.Context(), log.Fields{"error": err})
		return jwt.New()
	}

	return token
}