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: bbe83ba5c14a49735b5b8f39d9b97c18cad95e98 (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
package auth

import (
	"github.com/gorilla/sessions"
	"gitlab.com/gitlab-org/gitlab-pages/internal/errortracking"
	"gitlab.com/gitlab-org/gitlab-pages/internal/httperrors"
	"gitlab.com/gitlab-org/gitlab-pages/internal/request"
	"net/http"
)

type hostSession struct {
	*sessions.Session
}

const sessionHostKey = "_session_host"

func (s *hostSession) Save(r *http.Request, w http.ResponseWriter) error {
	logRequest(r).WithField("_session_host", s.Session.Values[sessionHostKey]).Debug("> Session save")
	logRequest(r).WithField("request host", r.Host).Debug("> Session save")
	s.Session.Values[sessionHostKey] = r.Host

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

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

	if session != nil {
		namespaceInPath := request.GetNamespaceInPathFromRequest(r, a.pagesDomain)
		logRequest(r).WithField("X-Gitlab-Namespace-In-Path", namespaceInPath).Debug("> Inside getSessionFromStore")

		// 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())

		logRequest(r).WithField("_session_host", session.Values[sessionHostKey]).Debug("> Inside getSessionFromStore: before host compare")
		logRequest(r).WithField("request host", r.Host).Debug("> Inside getSessionFromStore: before host compare")
		if session.Values[sessionHostKey] == nil || session.Values[sessionHostKey] != r.Host {
			session.Values = make(map[interface{}]interface{})
		}
	}

	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
}