feat(auth): add scoped JSON account and session queries for CLI clients
Build and publish container images / images (push) Successful in 18m18s
Build and publish container images / images (push) Successful in 18m18s
This commit is contained in:
1 parent
6f9752ea3a
commit
b650fe50d7
10 files changed
+640
No files matched your search
@@ -0,0 +1,120 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeAuthQueryJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func authQueryRoute(path string) bool {
|
||||
switch path {
|
||||
case "/_/api/v1/auth/whoami", "/_/api/v1/auth/sessions", "/_/api/v1/auth/sessions/history":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) registerAuthQueryRoutes() {
|
||||
for _, path := range []string{"/_/api/v1/auth/whoami", "/_/api/v1/auth/sessions", "/_/api/v1/auth/sessions/history"} {
|
||||
s.mux.HandleFunc(path, s.handleAuthQuery)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleAuthQuery(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", "GET")
|
||||
writeAuthQueryJSON(w, 405, map[string]string{"error": "method_not_allowed"})
|
||||
return
|
||||
}
|
||||
p := s.requestPrincipal(r)
|
||||
if p == nil {
|
||||
writeAuthQueryJSON(w, 401, map[string]string{"error": "authentication_required"})
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
current, err := s.store.CurrentAuthSession(ctx, domain, p.UUID, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
writeAuthQueryJSON(w, 401, map[string]string{"error": "authentication_required"})
|
||||
} else {
|
||||
writeAuthQueryJSON(w, 503, map[string]string{"error": "auth_query_unavailable"})
|
||||
}
|
||||
return
|
||||
}
|
||||
query, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_query"})
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/_/api/v1/auth/whoami" {
|
||||
if len(query) != 0 {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_query"})
|
||||
return
|
||||
}
|
||||
roles, groups, truncated, e := s.store.AuthMembershipNames(ctx, domain, p.UUID)
|
||||
if e != nil {
|
||||
writeAuthQueryJSON(w, 503, map[string]string{"error": "auth_query_unavailable"})
|
||||
return
|
||||
}
|
||||
writeAuthQueryJSON(w, 200, map[string]any{
|
||||
"principal_uuid": p.UUID, "username": p.Username, "display_name": p.DisplayName, "email": p.Email, "state": p.State,
|
||||
"roles": roles, "groups": groups, "memberships_truncated": truncated,
|
||||
"session": map[string]any{"uuid": current.UUID, "auth_time_ms": current.AuthTimeMS, "last_seen_at_ms": current.LastSeenAtMS, "expires_at_ms": current.ExpiresAtMS, "mfa_level": current.MFALevel},
|
||||
})
|
||||
return
|
||||
}
|
||||
for key, values := range query {
|
||||
if (key != "scope" && key != "limit") || len(values) != 1 {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_query"})
|
||||
return
|
||||
}
|
||||
}
|
||||
scope := query.Get("scope")
|
||||
principal := p.UUID
|
||||
switch scope {
|
||||
case "", "self":
|
||||
case "domain":
|
||||
if !s.store.IsBuiltinGroupAdmin(domain, p.UUID, "domain_admin") {
|
||||
writeAuthQueryJSON(w, 403, map[string]string{"error": "domain_admin_required"})
|
||||
return
|
||||
}
|
||||
principal = ""
|
||||
default:
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_scope"})
|
||||
return
|
||||
}
|
||||
history := r.URL.Path == "/_/api/v1/auth/sessions/history"
|
||||
limit := 100
|
||||
if history {
|
||||
limit = 20
|
||||
}
|
||||
if values, ok := query["limit"]; ok {
|
||||
limit, err = strconv.Atoi(values[0])
|
||||
if err != nil || limit < 1 || limit > 100 {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_limit"})
|
||||
return
|
||||
}
|
||||
}
|
||||
sessions, truncated, err := s.store.QueryAuthSessions(ctx, domain, principal, current.UUID, history, limit)
|
||||
if err != nil {
|
||||
writeAuthQueryJSON(w, 503, map[string]string{"error": "auth_query_unavailable"})
|
||||
return
|
||||
}
|
||||
writeAuthQueryJSON(w, 200, map[string]any{"sessions": sessions, "truncated": truncated})
|
||||
}
|
||||
Reference in new issue
Block a user