repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
f10bd2a
parent
550b987
author
Antonio Mika
date
2023-11-10 20:17:36 +0000 UTC
Add key endpoint for sish
1 files changed,  +31, -0
M auth/auth.go
+31, -0
 1@@ -189,6 +189,36 @@ func tokenHandler(w http.ResponseWriter, r *http.Request) {
 2 	}
 3 }
 4 
 5+type sishData struct {
 6+	PublicKey     string `json:"auth_key"`
 7+	Username      string `json:"user"`
 8+	RemoteAddress string `json:"remote_addr"`
 9+}
10+
11+func keyHandler(w http.ResponseWriter, r *http.Request) {
12+	client := getClient(r)
13+
14+	var data sishData
15+
16+	err := json.NewDecoder(r.Body).Decode(&data)
17+	if err != nil {
18+		client.Logger.Error(err)
19+		http.Error(w, err.Error(), http.StatusBadRequest)
20+		return
21+	}
22+
23+	client.Logger.Infof("handle key (%s, %s, %s)", data.RemoteAddress, data.Username, data.PublicKey)
24+
25+	_, err = client.Dbpool.FindUserForKey(data.Username, data.PublicKey)
26+	if err != nil {
27+		client.Logger.Error(err)
28+		http.Error(w, err.Error(), http.StatusUnauthorized)
29+		return
30+	}
31+
32+	w.WriteHeader(http.StatusOK)
33+}
34+
35 func createMainRoutes() []shared.Route {
36 	fileServer := http.FileServer(http.Dir("auth/public"))
37 
38@@ -197,6 +227,7 @@ func createMainRoutes() []shared.Route {
39 		shared.NewRoute("POST", "/introspect", introspectHandler),
40 		shared.NewRoute("GET", "/authorize", authorizeHandler),
41 		shared.NewRoute("POST", "/token", tokenHandler),
42+		shared.NewRoute("POST", "/key", keyHandler),
43 		shared.NewRoute("POST", "/redirect", redirectHandler),
44 		shared.NewRoute("GET", "/main.css", fileServer.ServeHTTP),
45 		shared.NewRoute("GET", "/card.png", fileServer.ServeHTTP),