refactor(handler/default): clean up access token getter

This commit is contained in:
Trong Huu Nguyen
2022-07-28 22:21:12 +02:00
parent 13fd194318
commit cbc49de826

View File

@@ -13,11 +13,8 @@ func (h *Handler) Default(w http.ResponseWriter, r *http.Request) {
logger := logentry.LogEntry(r).WithField("request_path", r.URL.Path)
isAuthenticated := false
sessionData, err := h.getSessionFromCookie(w, r)
hasSessionData := err == nil && sessionData != nil
hasAccessToken := hasSessionData && len(sessionData.AccessToken) > 0
if hasAccessToken {
accessToken, ok := h.accessToken(w, r)
if ok {
// add authentication if session cookie and token checks out
isAuthenticated = true
@@ -41,12 +38,21 @@ func (h *Handler) Default(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if isAuthenticated {
ctx = withAccessToken(ctx, sessionData.AccessToken)
ctx = withAccessToken(ctx, accessToken)
}
h.ReverseProxy.ServeHTTP(w, r.WithContext(ctx))
}
func (h *Handler) accessToken(w http.ResponseWriter, r *http.Request) (string, bool) {
sessionData, err := h.getSessionFromCookie(w, r)
if err != nil || sessionData == nil || len(sessionData.AccessToken) == 0 {
return "", false
}
return sessionData.AccessToken, true
}
type contextKey string
const (