repos / pico

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

commit
2be62f3
parent
ea40a79
author
Antonio Mika
date
2024-08-27 03:52:31 +0000 UTC
Add user handler to auth system
1 files changed,  +41, -0
M auth/api.go
+41, -0
 1@@ -298,6 +298,46 @@ func keyHandler(w http.ResponseWriter, r *http.Request) {
 2 	}
 3 }
 4 
 5+func userHandler(w http.ResponseWriter, r *http.Request) {
 6+	client := getClient(r)
 7+
 8+	if !client.hasPrivilegedAccess(getApiToken(r)) {
 9+		w.WriteHeader(http.StatusForbidden)
10+		return
11+	}
12+
13+	var data sishData
14+
15+	err := json.NewDecoder(r.Body).Decode(&data)
16+	if err != nil {
17+		client.Logger.Error(err.Error())
18+		http.Error(w, err.Error(), http.StatusBadRequest)
19+		return
20+	}
21+
22+	client.Logger.Info(
23+		"handle key",
24+		"remoteAddress", data.RemoteAddress,
25+		"user", data.Username,
26+		"publicKey", data.PublicKey,
27+	)
28+
29+	user, err := client.Dbpool.FindUserForName(data.Username)
30+	if err != nil {
31+		client.Logger.Error(err.Error())
32+		http.Error(w, err.Error(), http.StatusNotFound)
33+		return
34+	}
35+
36+	w.Header().Set("Content-Type", "application/json")
37+	w.WriteHeader(http.StatusOK)
38+	err = json.NewEncoder(w).Encode(user)
39+	if err != nil {
40+		client.Logger.Error(err.Error())
41+		http.Error(w, err.Error(), http.StatusInternalServerError)
42+	}
43+}
44+
45 func genFeedItem(now time.Time, expiresAt time.Time, warning time.Time, txt string) *feeds.Item {
46 	if now.After(warning) {
47 		content := fmt.Sprintf(
48@@ -549,6 +589,7 @@ func createMainRoutes() []shared.Route {
49 		shared.NewRoute("GET", "/authorize", authorizeHandler),
50 		shared.NewRoute("POST", "/token", tokenHandler),
51 		shared.NewRoute("POST", "/key", keyHandler),
52+		shared.NewRoute("POST", "/user", userHandler),
53 		shared.NewRoute("GET", "/rss/(.+)", rssHandler),
54 		shared.NewRoute("POST", "/redirect", redirectHandler),
55 		shared.NewRoute("POST", "/webhook", paymentWebhookHandler(secret)),