Commit 7fe30e4
Eric Bower
·
2026-01-22 18:12:11 -0500 EST
parent 78dfe01
feat(auth): `/pubkeys/{user}` public endpoint
1 files changed,
+45,
-6
+45,
-6
| ... | ... | @@ -69,7 +69,7 @@ func wellKnownHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 69 | 69 | w.WriteHeader(http.StatusOK) | |
| 70 | 70 | err := json.NewEncoder(w).Encode(p) | |
| 71 | 71 | if err != nil { | |
| 72 | - | apiConfig.Cfg.Logger.Error(err.Error()) | |
| 72 | + | apiConfig.Cfg.Logger.Error("cannot json encode", "err", err.Error()) | |
| 73 | 73 | http.Error(w, err.Error(), http.StatusInternalServerError) | |
| 74 | 74 | } | |
| 75 | 75 | } |
| ... | ... | @@ -108,7 +108,7 @@ func introspectHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 108 | 108 | w.WriteHeader(http.StatusOK) | |
| 109 | 109 | err = json.NewEncoder(w).Encode(p) | |
| 110 | 110 | if err != nil { | |
| 111 | - | apiConfig.Cfg.Logger.Error(err.Error()) | |
| 111 | + | apiConfig.Cfg.Logger.Error("cannot json encode", "err", err.Error()) | |
| 112 | 112 | http.Error(w, err.Error(), http.StatusInternalServerError) | |
| 113 | 113 | } | |
| 114 | 114 | } |
| ... | ... | @@ -151,7 +151,7 @@ func authorizeHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 151 | 151 | }) | |
| 152 | 152 | ||
| 153 | 153 | if err != nil { | |
| 154 | - | apiConfig.Cfg.Logger.Error(err.Error()) | |
| 154 | + | apiConfig.Cfg.Logger.Error("cannot execture template", "err", err.Error()) | |
| 155 | 155 | http.Error(w, err.Error(), http.StatusUnauthorized) | |
| 156 | 156 | return | |
| 157 | 157 | } |
| ... | ... | @@ -221,7 +221,7 @@ func tokenHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 221 | 221 | w.WriteHeader(http.StatusOK) | |
| 222 | 222 | err = json.NewEncoder(w).Encode(p) | |
| 223 | 223 | if err != nil { | |
| 224 | - | apiConfig.Cfg.Logger.Error(err.Error()) | |
| 224 | + | apiConfig.Cfg.Logger.Error("cannot json encode", "err", err.Error()) | |
| 225 | 225 | http.Error(w, err.Error(), http.StatusInternalServerError) | |
| 226 | 226 | } | |
| 227 | 227 | } |
| ... | ... | @@ -348,7 +348,7 @@ func userHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 348 | 348 | w.WriteHeader(http.StatusOK) | |
| 349 | 349 | err = json.NewEncoder(w).Encode(keys) | |
| 350 | 350 | if err != nil { | |
| 351 | - | apiConfig.Cfg.Logger.Error(err.Error()) | |
| 351 | + | apiConfig.Cfg.Logger.Error("cannot json encode", "err", err.Error()) | |
| 352 | 352 | http.Error(w, err.Error(), http.StatusInternalServerError) | |
| 353 | 353 | } | |
| 354 | 354 | } |
| ... | ... | @@ -382,7 +382,45 @@ func rssHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 382 | 382 | w.Header().Add("Content-Type", "application/atom+xml") | |
| 383 | 383 | _, err = w.Write([]byte(rss)) | |
| 384 | 384 | if err != nil { | |
| 385 | - | apiConfig.Cfg.Logger.Error(err.Error()) | |
| 385 | + | apiConfig.Cfg.Logger.Error("cannot write to http handler", "err", err.Error()) | |
| 386 | + | } | |
| 387 | + | } | |
| 388 | + | } | |
| 389 | + | ||
| 390 | + | func pubkeysHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 391 | + | return func(w http.ResponseWriter, r *http.Request) { | |
| 392 | + | userName := r.PathValue("user") | |
| 393 | + | user, err := apiConfig.Dbpool.FindUserByName(userName) | |
| 394 | + | if err != nil { | |
| 395 | + | apiConfig.Cfg.Logger.Error( | |
| 396 | + | "could not find user by name", | |
| 397 | + | "err", err.Error(), | |
| 398 | + | "user", userName, | |
| 399 | + | ) | |
| 400 | + | http.Error(w, "user not found", http.StatusNotFound) | |
| 401 | + | return | |
| 402 | + | } | |
| 403 | + | ||
| 404 | + | pubkeys, err := apiConfig.Dbpool.FindKeysByUser(user) | |
| 405 | + | if err != nil { | |
| 406 | + | apiConfig.Cfg.Logger.Error( | |
| 407 | + | "could not find pubkeys for user", | |
| 408 | + | "err", err.Error(), | |
| 409 | + | "user", userName, | |
| 410 | + | ) | |
| 411 | + | http.Error(w, "user pubkeys not found", http.StatusNotFound) | |
| 412 | + | return | |
| 413 | + | } | |
| 414 | + | ||
| 415 | + | keys := []string{} | |
| 416 | + | for _, pubkeys := range pubkeys { | |
| 417 | + | keys = append(keys, pubkeys.Key) | |
| 418 | + | } | |
| 419 | + | ||
| 420 | + | w.Header().Add("Content-Type", "text/plain") | |
| 421 | + | _, err = w.Write([]byte(strings.Join(keys, "\n"))) | |
| 422 | + | if err != nil { | |
| 423 | + | apiConfig.Cfg.Logger.Error("cannot write to http handler", "err", err.Error()) | |
| 386 | 424 | } | |
| 387 | 425 | } | |
| 388 | 426 | } |
| ... | ... | @@ -815,6 +853,7 @@ func authMux(apiConfig *shared.ApiConfig) *http.ServeMux { | |
| 815 | 853 | mux.Handle("POST /key", keyHandler(apiConfig)) | |
| 816 | 854 | mux.Handle("POST /user", userHandler(apiConfig)) | |
| 817 | 855 | mux.Handle("GET /rss/{token}", rssHandler(apiConfig)) | |
| 856 | + | mux.Handle("GET /pubkeys/{user}", pubkeysHandler(apiConfig)) | |
| 818 | 857 | mux.Handle("POST /redirect", redirectHandler(apiConfig)) | |
| 819 | 858 | mux.Handle("POST /webhook", paymentWebhookHandler(apiConfig)) | |
| 820 | 859 | mux.HandleFunc("GET /main.css", fileServer.ServeHTTP) |