neko/server/internal/api/router.go

86 lines
2.0 KiB
Go
Raw Permalink Normal View History

2020-10-30 10:23:30 +13:00
package api
import (
2021-09-17 10:58:50 +12:00
"context"
2021-08-30 03:23:49 +12:00
"errors"
2020-11-01 00:27:55 +13:00
"net/http"
"github.com/demodesk/neko/internal/api/members"
"github.com/demodesk/neko/internal/api/room"
"github.com/demodesk/neko/internal/api/sessions"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
2020-10-30 10:23:30 +13:00
)
2020-11-02 04:54:06 +13:00
type ApiManagerCtx struct {
2020-12-13 05:07:45 +13:00
sessions types.SessionManager
2021-03-15 02:44:32 +13:00
members types.MemberManager
2020-12-13 05:07:45 +13:00
desktop types.DesktopManager
capture types.CaptureManager
2021-09-17 10:58:50 +12:00
routers map[string]func(types.Router)
2020-10-30 10:23:30 +13:00
}
2020-10-31 06:16:21 +13:00
func New(
sessions types.SessionManager,
2021-03-15 02:44:32 +13:00
members types.MemberManager,
2020-11-02 04:54:06 +13:00
desktop types.DesktopManager,
capture types.CaptureManager,
) *ApiManagerCtx {
2020-10-31 06:16:21 +13:00
2020-11-02 04:54:06 +13:00
return &ApiManagerCtx{
2020-12-13 05:07:45 +13:00
sessions: sessions,
2021-03-15 02:44:32 +13:00
members: members,
2020-12-13 05:07:45 +13:00
desktop: desktop,
capture: capture,
2021-09-17 10:58:50 +12:00
routers: make(map[string]func(types.Router)),
2020-10-31 06:16:21 +13:00
}
2020-10-30 10:23:30 +13:00
}
2021-09-17 10:58:50 +12:00
func (api *ApiManagerCtx) Route(r types.Router) {
2021-01-30 10:22:14 +13:00
r.Post("/login", api.Login)
2020-10-30 10:23:30 +13:00
2021-01-30 10:22:14 +13:00
// Authenticated area
2021-09-17 10:58:50 +12:00
r.Group(func(r types.Router) {
2021-01-30 10:22:14 +13:00
r.Use(api.Authenticate)
2021-01-30 10:22:14 +13:00
r.Post("/logout", api.Logout)
r.Get("/whoami", api.Whoami)
sessionsHandler := sessions.New(api.sessions)
r.Route("/sessions", sessionsHandler.Route)
2020-10-31 22:48:24 +13:00
2021-03-15 02:44:32 +13:00
membersHandler := members.New(api.members)
2021-01-30 10:22:14 +13:00
r.Route("/members", membersHandler.Route)
2021-04-06 09:03:49 +12:00
r.Route("/members_bulk", membersHandler.RouteBulk)
2020-12-13 05:07:45 +13:00
2021-01-30 10:22:14 +13:00
roomHandler := room.New(api.sessions, api.desktop, api.capture)
r.Route("/room", roomHandler.Route)
for path, router := range api.routers {
r.Route(path, router)
}
})
2020-10-31 22:48:24 +13:00
}
2021-09-17 10:58:50 +12:00
func (api *ApiManagerCtx) Authenticate(w http.ResponseWriter, r *http.Request) (context.Context, error) {
session, err := api.sessions.Authenticate(r)
if err != nil {
if api.sessions.CookieEnabled() {
api.sessions.CookieClearToken(w, r)
}
2021-03-25 22:29:28 +13:00
2021-09-17 10:58:50 +12:00
if errors.Is(err, types.ErrSessionLoginDisabled) {
return nil, utils.HttpForbidden("login is disabled for this session")
}
return nil, utils.HttpUnauthorized().WithInternalErr(err)
}
return auth.SetSession(r, session), nil
2020-10-30 10:23:30 +13:00
}
2020-12-13 05:07:45 +13:00
2021-09-17 10:58:50 +12:00
func (api *ApiManagerCtx) AddRouter(path string, router func(types.Router)) {
2020-12-13 05:07:45 +13:00
api.routers[path] = router
}