sig-auth.git

git clone https://git.crispbyte.dev/sig-auth.git

commit
d0958d2
parent
8d6102c
author
cheddar
date
2025-02-21 05:12:04 +0100 CET
Remove caddy simulation
3 files changed,  +12, -25
M client/client.go
+1, -6
 1@@ -19,7 +19,7 @@ import (
 2 	"github.com/opencontainers/go-digest"
 3 )
 4 
 5-func Post(baseUrl *url.URL, key crypto.PrivateKey, keyId string, data []byte, simulateCaddy bool) (*http.Response, error) {
 6+func Post(baseUrl *url.URL, key crypto.PrivateKey, keyId string, data []byte) (*http.Response, error) {
 7 	client, err := getSigningClient(key, keyId)
 8 
 9 	if err != nil {
10@@ -41,11 +41,6 @@ func Post(baseUrl *url.URL, key crypto.PrivateKey, keyId string, data []byte, si
11 	req.Header.Add("Content-Digest", string(id.Algorithm())+"="+id.Encoded())
12 	req.Header.Add("Content-Type", "application/json")
13 
14-	if simulateCaddy {
15-		req.Header.Add("X-Forwarded-Method", req.Method)
16-		req.Header.Add("X-Forwarded-Uri", req.RequestURI)
17-	}
18-
19 	resp, err := client.Do(req)
20 
21 	return resp, err
M main.go
+6, -7
 1@@ -23,7 +23,6 @@ func main() {
 2 	user := flag.String("user", "", "Username to register")
 3 	keyPath := flag.String("key", "", "Path to the private key (client mode) or public key (registration mode) to use")
 4 	baseUrlString := flag.String("base-url", "http://localhost:8080", "Base URL of the server")
 5-	simulateCaddy := flag.Bool("caddy", false, "Simulate caddy reverse proxy")
 6 	useTempDb := flag.Bool("temp-db", false, "Use a temporary in-memory database")
 7 	dbPath := flag.String("db", "", "Path to the sqlite database file")
 8 
 9@@ -42,7 +41,7 @@ func main() {
10 			return
11 		}
12 
13-		runClient(baseUrl, *keyPath, *keyId, *simulateCaddy)
14+		runClient(baseUrl, *keyPath, *keyId)
15 	} else if *register {
16 		if *keyPath == "" || *user == "" {
17 			flag.PrintDefaults()
18@@ -56,11 +55,11 @@ func main() {
19 			return
20 		}
21 
22-		runServer(*simulateCaddy, *useTempDb, *dbPath)
23+		runServer(*useTempDb, *dbPath)
24 	}
25 }
26 
27-func runClient(baseUrl *url.URL, keyFile string, keyId string, simulateCaddy bool) {
28+func runClient(baseUrl *url.URL, keyFile string, keyId string) {
29 	testData := map[string]string{"hello": "world"}
30 	json_data, _ := json.Marshal(testData)
31 
32@@ -70,7 +69,7 @@ func runClient(baseUrl *url.URL, keyFile string, keyId string, simulateCaddy boo
33 		log.Fatal(err)
34 	}
35 
36-	resp, err := client.Post(baseUrl, key, keyId, json_data, simulateCaddy)
37+	resp, err := client.Post(baseUrl, key, keyId, json_data)
38 
39 	if err != nil {
40 		log.Fatal(err)
41@@ -117,7 +116,7 @@ func registerKey(baseUrl *url.URL, keyFile string, userId string) {
42 	fmt.Printf("Registered key id: %s\n", keyId)
43 }
44 
45-func runServer(simulateCaddy bool, useTempDb bool, dbPath string) {
46+func runServer(useTempDb bool, dbPath string) {
47 	var keyDir keydirectory.RegistrationDirectory
48 
49 	if useTempDb {
50@@ -131,5 +130,5 @@ func runServer(simulateCaddy bool, useTempDb bool, dbPath string) {
51 		}
52 	}
53 
54-	server.Start(simulateCaddy, keyDir)
55+	server.Start(keyDir)
56 }
M server/server.go
+5, -12
 1@@ -13,7 +13,7 @@ import (
 2 	"golang.org/x/crypto/ssh"
 3 )
 4 
 5-func Start(isCaddyAuth bool, keyDir keydirectory.RegistrationDirectory) error {
 6+func Start(keyDir keydirectory.RegistrationDirectory) error {
 7 	mux := http.NewServeMux()
 8 
 9 	verifier := httpsig.Middleware(httpsig.MiddlewareOpts{
10@@ -32,15 +32,11 @@ func Start(isCaddyAuth bool, keyDir keydirectory.RegistrationDirectory) error {
11 		},
12 	})
13 
14-	verifyHandler := verifier(getDefaultHandler(isCaddyAuth))
15+	verifyHandler := verifier(getDefaultHandler())
16 
17 	var handler http.Handler
18 
19-	if isCaddyAuth {
20-		handler = rewriteHeaders(verifyHandler)
21-	} else {
22-		handler = verifyHandler
23-	}
24+	handler = rewriteHeaders(verifyHandler)
25 
26 	mux.Handle("/auth", handler)
27 	mux.Handle("/register", getRegistrationHandler(keyDir))
28@@ -50,14 +46,11 @@ func Start(isCaddyAuth bool, keyDir keydirectory.RegistrationDirectory) error {
29 	return err
30 }
31 
32-func getDefaultHandler(isCaddyAuth bool) http.Handler {
33+func getDefaultHandler() http.Handler {
34 	handler := func(w http.ResponseWriter, r *http.Request) {
35 		attr := httpsig.AttributesFromContext(r.Context()).(string)
36 
37-		if isCaddyAuth {
38-			w.Header().Add("Remote-User", attr)
39-		}
40-
41+		w.Header().Add("Remote-User", attr)
42 		msg := fmt.Sprintf("hello, %s!", attr)
43 		w.Write([]byte(msg))
44 	}