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

acme.go « acme « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a6be01dec0d81600ab7dc26ffc8144307bc77b6a (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
package acme

import (
	"net/http"
	"net/url"
	"path/filepath"
	"strings"

	log "github.com/sirupsen/logrus"

	"gitlab.com/gitlab-org/gitlab-pages/internal/host"
)

// Middleware handles acme challenges by redirecting them to GitLab instance
type Middleware struct {
	GitlabURL string
}

// Domain interface represent D from domain package
type Domain interface {
	HasAcmeChallenge(*http.Request, string) bool
}

// ServeAcmeChallenges identifies if request is acme-challenge and redirects to GitLab in that case
func (m *Middleware) ServeAcmeChallenges(w http.ResponseWriter, r *http.Request, domain Domain) bool {
	if m == nil {
		return false
	}

	if !isAcmeChallenge(r.URL.Path) {
		return false
	}

	if domain.HasAcmeChallenge(r, filepath.Base(r.URL.Path)) {
		return false
	}

	return m.redirectToGitlab(w, r)
}

func isAcmeChallenge(path string) bool {
	return strings.HasPrefix(filepath.Clean(path), "/.well-known/acme-challenge/")
}

func (m *Middleware) redirectToGitlab(w http.ResponseWriter, r *http.Request) bool {
	redirectURL, err := url.Parse(m.GitlabURL)
	if err != nil {
		log.WithError(err).Error("Can't parse GitLab URL for acme challenge redirect")
		return false
	}

	redirectURL.Path = "/-/acme-challenge"
	query := redirectURL.Query()
	query.Set("domain", host.FromRequest(r))
	query.Set("token", filepath.Base(r.URL.Path))
	redirectURL.RawQuery = query.Encode()

	log.WithField("redirect_url", redirectURL).Debug("Redirecting to GitLab for processing acme challenge")

	http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
	return true
}