Publish curated ITWorx-Pulse source from 0e0633c8ac91cb71312a26837f791035a78d81b8
Public source validation / validate (push) Failing after 2m10s
Public source validation / validate (push) Failing after 2m10s
This commit is contained in:
1 parent
a4956c2af9
commit
db8093b608
88 files changed
+3685
-469
No files matched your search
@@ -0,0 +1,53 @@
|
||||
package sessionapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
)
|
||||
|
||||
type Permissions struct {
|
||||
View bool `json:"view"`
|
||||
Operate bool `json:"operate"`
|
||||
Edit bool `json:"edit"`
|
||||
Admin bool `json:"admin"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Subject string `json:"subject"`
|
||||
Role auth.Role `json:"role"`
|
||||
Environment string `json:"environment"`
|
||||
Permissions Permissions `json:"permissions"`
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
Environment string
|
||||
}
|
||||
|
||||
func (handler Handler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||
response.Header().Set("Cache-Control", "private, no-store")
|
||||
if request.Method != http.MethodGet {
|
||||
problem.Write(response, request, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Method not allowed", "Only GET is supported for the session capability view.", nil)
|
||||
return
|
||||
}
|
||||
principal, ok := auth.PrincipalFromContext(request.Context())
|
||||
if !ok {
|
||||
problem.Write(response, request, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "An authenticated Pulse session is required.", nil)
|
||||
return
|
||||
}
|
||||
payload := Response{
|
||||
Subject: principal.Subject,
|
||||
Role: principal.Role,
|
||||
Environment: handler.Environment,
|
||||
Permissions: Permissions{
|
||||
View: auth.Allows(principal.Role, auth.PermissionView),
|
||||
Operate: auth.Allows(principal.Role, auth.PermissionOperate),
|
||||
Edit: auth.Allows(principal.Role, auth.PermissionEdit),
|
||||
Admin: auth.Allows(principal.Role, auth.PermissionAdmin),
|
||||
},
|
||||
}
|
||||
response.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(response).Encode(payload)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package sessionapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
)
|
||||
|
||||
func TestHandlerReturnsCurrentCapabilities(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/session", nil)
|
||||
request = request.WithContext(auth.WithPrincipal(context.Background(), auth.Principal{Subject: "operator-42", Role: auth.RoleEditor}))
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
Handler{Environment: "development"}.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusOK)
|
||||
}
|
||||
if got := response.Header().Get("Cache-Control"); got != "private, no-store" {
|
||||
t.Fatalf("Cache-Control = %q", got)
|
||||
}
|
||||
var payload Response
|
||||
if err := json.NewDecoder(response.Body).Decode(&payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Subject != "operator-42" || payload.Role != auth.RoleEditor {
|
||||
t.Fatalf("unexpected identity: %#v", payload)
|
||||
}
|
||||
if payload.Environment != "development" {
|
||||
t.Fatalf("environment = %q, want development", payload.Environment)
|
||||
}
|
||||
if !payload.Permissions.View || !payload.Permissions.Operate || !payload.Permissions.Edit || payload.Permissions.Admin {
|
||||
t.Fatalf("unexpected permissions: %#v", payload.Permissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerFailsClosedWithoutPrincipal(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/session", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
Handler{}.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRejectsMutationMethods(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/session", nil)
|
||||
request = request.WithContext(auth.WithPrincipal(context.Background(), auth.Principal{Subject: "admin-1", Role: auth.RoleAdministrator}))
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
Handler{}.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusMethodNotAllowed {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user