feat(server): add /api/v1/session endpoint
All checks were successful
arcad/emissary/pipeline/head This commit looks good

This commit is contained in:
2024-03-03 18:40:56 +01:00
parent 8f2131338d
commit 76718722cc
5 changed files with 75 additions and 7 deletions

View File

@ -0,0 +1,35 @@
package api
import (
"net/http"
"forge.cadoles.com/Cadoles/emissary/internal/auth"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
)
func (m *Mount) getSession(w http.ResponseWriter, r *http.Request) {
user, ok := assertRequestUser(w, r)
if !ok {
return
}
ctx := r.Context()
tenant, err := m.tenantRepo.Get(ctx, user.Tenant())
if err != nil && !errors.Is(err, datastore.ErrNotFound) {
err = errors.WithStack(err)
logger.Error(ctx, "could not retrieve user tenant", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
}
api.DataResponse(w, http.StatusOK, struct {
User auth.User `json:"user"`
Tenant *datastore.Tenant `json:"tenant"`
}{
User: user,
Tenant: tenant,
})
}

View File

@ -23,6 +23,8 @@ func (m *Mount) Mount(r chi.Router) {
r.Group(func(r chi.Router) {
r.Use(auth.Middleware(m.authenticators...))
r.Get("/session", m.getSession)
r.Route("/agents", func(r chi.Router) {
r.With(assertUserWithWriteAccess).Post("/claim", m.claimAgent)