Welcome to mirror list, hosted at ThFree Co, Russian Federation.

session.go « auth « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0a3d370fbe8af5b2628dcbf3f40f3e991d4ebbb (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package auth

import (
	"net/http"
	"strings"

	"github.com/gorilla/sessions"
	"gitlab.com/gitlab-org/labkit/log"

	"gitlab.com/gitlab-org/gitlab-pages/internal/errortracking"
	"gitlab.com/gitlab-org/gitlab-pages/internal/feature"
	"gitlab.com/gitlab-org/gitlab-pages/internal/httperrors"
	"gitlab.com/gitlab-org/gitlab-pages/internal/request"
)

const (
	sessionHostKey     = "_session_host"
	namespaceInPathKey = "_namespace_in_path"
)

type hostSession struct {
	*sessions.Session
}

func (s *hostSession) Save(r *http.Request, w http.ResponseWriter) error {
	s.Session.Values[sessionHostKey] = r.Host

	return s.Session.Save(r, w)
}

func (s *hostSession) getNamespaceInPathFromSession() string {
	if s.Values[namespaceInPathKey] != nil {
		return s.Values[namespaceInPathKey].(string)
	}
	return ""
}

func (s *hostSession) appendPath(path string) {
	if feature.ProjectPrefixCookiePath.Enabled() && len(path) > 0 {
		s.Options.Path = strings.TrimSuffix(s.Options.Path, "/") + "/" + strings.Trim(path, "/")
	}
}

func (a *Auth) getSessionFromStore(r *http.Request) (*hostSession, error) {
	session, err := a.store.Get(r, "gitlab-pages")

	if session != nil {
		namespaceInPath := a.getNamespaceInPath(r)

		// Cookie just for this domain
		session.Options.Path = "/" + namespaceInPath
		session.Options.HttpOnly = true
		session.Options.Secure = request.IsHTTPS(r)
		session.Options.MaxAge = int(a.cookieSessionTimeout.Seconds())

		if session.Values[sessionHostKey] == nil || session.Values[sessionHostKey] != r.Host {
			logRequest(r).WithFields(log.Fields{
				"Session host":      session.Values[sessionHostKey],
				"Request host":      r.Host,
				"Namespace in path": namespaceInPath,
			}).Info("Resetting session values")

			session.Values = make(map[interface{}]interface{})
		}
		if len(namespaceInPath) > 0 {
			session.Values[namespaceInPathKey] = namespaceInPathKey
		}
	}

	return &hostSession{session}, err
}

func (a *Auth) checkSession(w http.ResponseWriter, r *http.Request) (*hostSession, error) {
	// Create or get session
	session, errsession := a.getSessionFromStore(r)

	if errsession != nil {
		// Save cookie again
		errsave := session.Save(r, w)
		if errsave != nil {
			logRequest(r).WithError(errsave).Error(saveSessionErrMsg)
			errortracking.CaptureErrWithReqAndStackTrace(errsave, r)
			httperrors.Serve500(w)
			return nil, errsave
		}

		http.Redirect(w, r, getRequestAddress(r), http.StatusFound)
		return nil, errsession
	}

	return session, nil
}