Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: validate node config in middleware #229

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@ import (

// TODO: echo methods should not be on Service object

func (svc *Service) ValidateUserMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
func (svc *Service) ValidateAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// TODO: check if login is required and check if user is logged in
//sess, _ := session.Get(CookieName, c)
// if user == nil {
// return c.NoContent(http.StatusUnauthorized)
// }
if svc.lnClient == nil {
return c.JSON(http.StatusMethodNotAllowed, ErrorResponse{
Message: "Lightning Client not initialized",
})
}
return next(c)
}
}

func (svc *Service) ValidateNodeMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if svc.lnClient == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good check but doesn't fit the name of the middleware, and is not related to the middleware that this would be for (checking if the user has unlocked NWC / has authed in http mode)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to ValidateNodeMiddleware

return c.JSON(http.StatusMethodNotAllowed, ErrorResponse{
Message: "Lightning Client not initialized",
})
}
return next(c)
}
}
Expand All @@ -38,11 +49,11 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) {
}))
e.Use(session.Middleware(sessions.NewCookieStore([]byte(svc.cfg.CookieSecret))))

authMiddleware := svc.ValidateUserMiddleware
e.GET("/api/apps", svc.AppsListHandler, authMiddleware)
e.GET("/api/apps/:pubkey", svc.AppsShowHandler, authMiddleware)
e.DELETE("/api/apps/:pubkey", svc.AppsDeleteHandler, authMiddleware)
e.POST("/api/apps", svc.AppsCreateHandler, authMiddleware)
nodeMiddleware := svc.ValidateNodeMiddleware
e.GET("/api/apps", svc.AppsListHandler, nodeMiddleware)
e.GET("/api/apps/:pubkey", svc.AppsShowHandler, nodeMiddleware)
e.DELETE("/api/apps/:pubkey", svc.AppsDeleteHandler, nodeMiddleware)
e.POST("/api/apps", svc.AppsCreateHandler, nodeMiddleware)

e.GET("/api/csrf", svc.CSRFHandler)
e.GET("/api/info", svc.InfoHandler)
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/screens/apps/AppsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { PlusIcon } from "src/components/icons/PlusIcon";
import Loading from "src/components/Loading";

function AppsList() {
const { data: apps } = useApps();
const { data: apps, error } = useApps();

const navigate = useNavigate();

if (error) {
return <p className="text-red-500">{error.message}</p>;
}

if (!apps) {
return <Loading />;
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewService(ctx context.Context) *Service {
if err != nil {
//err being non-nil means that we have an error on the websocket error channel. In this case we just try to reconnect.
svc.Logger.WithError(err).Error("Got an error from the relay while listening to subscription. Reconnecting...")
relay, err = nostr.RelayConnect(ctx, cfg.Relay)
relay, err = nostr.RelayConnect(ctx, cfg.Relay, nostr.WithNoticeHandler(svc.noticeHandler))
if err != nil {
svc.Logger.Fatal(err)
}
Expand Down
Loading