fix(webui): explain personal token availability
This commit is contained in:
4 files changed
+114
-69
No files matched your search
@@ -3,6 +3,7 @@ package webui
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
@@ -10,7 +11,11 @@ import (
|
||||
// on the principal the request carries, so these handlers name no one.
|
||||
|
||||
func (s *Server) handlePersonalTokensList(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.personalTokensReady(w) {
|
||||
if !personalTokenOwner(r) {
|
||||
writeJSON(w, map[string]any{"tokens": []identity.PersonalToken{}, "available": false})
|
||||
return
|
||||
}
|
||||
if !s.personalTokensReady(w, r) {
|
||||
return
|
||||
}
|
||||
tokens, err := s.PersonalTokens.ListPersonalTokens(r.Context())
|
||||
@@ -21,13 +26,13 @@ func (s *Server) handlePersonalTokensList(w http.ResponseWriter, r *http.Request
|
||||
if tokens == nil {
|
||||
tokens = []identity.PersonalToken{}
|
||||
}
|
||||
writeJSON(w, map[string]any{"tokens": tokens})
|
||||
writeJSON(w, map[string]any{"tokens": tokens, "available": true})
|
||||
}
|
||||
|
||||
// handlePersonalTokenCreate makes a token here and sends only its hash to the
|
||||
// State Store; the response is the one time the token is shown.
|
||||
func (s *Server) handlePersonalTokenCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.personalTokensReady(w) {
|
||||
if !s.personalTokensReady(w, r) {
|
||||
return
|
||||
}
|
||||
raw, subject, err := identity.NewPersonalToken()
|
||||
@@ -45,7 +50,7 @@ func (s *Server) handlePersonalTokenCreate(w http.ResponseWriter, r *http.Reques
|
||||
}
|
||||
|
||||
func (s *Server) handlePersonalTokenRevoke(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.personalTokensReady(w) {
|
||||
if !s.personalTokensReady(w, r) {
|
||||
return
|
||||
}
|
||||
if err := s.PersonalTokens.RevokePersonalToken(r.Context(), r.PathValue("id")); err != nil {
|
||||
@@ -55,7 +60,17 @@ func (s *Server) handlePersonalTokenRevoke(w http.ResponseWriter, r *http.Reques
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) personalTokensReady(w http.ResponseWriter) bool {
|
||||
func personalTokenOwner(r *http.Request) bool {
|
||||
p, ok := access.PrincipalFromContext(r.Context())
|
||||
return ok && p.IdentityID != "" && p.IdentityID != identity.SystemID
|
||||
}
|
||||
|
||||
func (s *Server) personalTokensReady(w http.ResponseWriter, r *http.Request) bool {
|
||||
if !personalTokenOwner(r) {
|
||||
http.Error(w, "Personal tokens need a signed-in person.", http.StatusForbidden)
|
||||
return false
|
||||
}
|
||||
|
||||
if s.PersonalTokens == nil {
|
||||
http.Error(w, "personal tokens unavailable", http.StatusServiceUnavailable)
|
||||
return false
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
@@ -145,3 +146,28 @@ func TestAdministrationRoutes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPersonalTokensWithoutPerson(t *testing.T) {
|
||||
for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodDelete} {
|
||||
t.Run(method, func(t *testing.T) {
|
||||
s := &Server{PersonalTokens: &recordingTokens{}}
|
||||
req := httptest.NewRequestWithContext(access.WithPrincipal(t.Context(), access.SharedTokenOwner()), method, "/api/tokens", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
s.handlePersonalTokensList(rec, req)
|
||||
case http.MethodPost:
|
||||
s.handlePersonalTokenCreate(rec, req)
|
||||
case http.MethodDelete:
|
||||
s.handlePersonalTokenRevoke(rec, req)
|
||||
}
|
||||
if method == http.MethodGet {
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"available":false`) {
|
||||
t.Fatalf("list without person = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
} else if rec.Code != http.StatusForbidden || strings.TrimSpace(rec.Body.String()) != "Personal tokens need a signed-in person." {
|
||||
t.Fatalf("mutation without person = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user