summaryrefslogtreecommitdiff
path: root/system/system.go
blob: 80f562199f1184fd211058580c1492874a43c593 (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
// Package system contains a collection of packages that make up the internal
// Ponzu system, which handles addons, administration, the Admin server, the API
// server, analytics, databases, search, TLS, and various internal types.
package system

import (
	"net/http"

	"github.com/haturatu/ponzu/system/db"
)

// BasicAuth adds HTTP Basic Auth check for requests that should implement it
func BasicAuth(next http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		u := db.ConfigCache("backup_basic_auth_user").(string)
		p := db.ConfigCache("backup_basic_auth_password").(string)

		if u == "" || p == "" {
			res.WriteHeader(http.StatusForbidden)
			return
		}

		user, password, ok := req.BasicAuth()

		if !ok {
			res.WriteHeader(http.StatusForbidden)
			return
		}

		if u != user || p != password {
			res.WriteHeader(http.StatusUnauthorized)
			return
		}

		next.ServeHTTP(res, req)
	})
}