summaryrefslogtreecommitdiff
path: root/app/middleware/token_parser.go
blob: a719b2f4c8a4948cefad127eda38a796c9d72408 (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
package middleware

import (
	"net/http"

	"github.com/xlgmokha/x/pkg/log"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
)

type TokenParser func(*http.Request) oidc.RawToken

func IDTokenFromSessionCookie(r *http.Request) oidc.RawToken {
	cookies := r.CookiesNamed("session")

	if len(cookies) != 1 {
		return ""
	}

	tokens, err := oidc.TokensFromBase64String(cookies[0].Value)
	if err != nil {
		log.WithFields(r.Context(), log.Fields{"error": err})
		return ""
	}

	return tokens.IDToken
}