Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ release builds resolve the SDK from a published module tag.
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/convert` — type conversions.
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/events` — the host event catalog: event names, access classes, and typed payloads (see [docs/events.md](docs/events.md)).
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/httpclient` — credentialed JSON-over-HTTP client with bounded responses and typed status errors.
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/httproute` — reads the signed-in viewer (account, role, verified profile) the host passes to an `http_routes.v1` handler.
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/imagevariant` — canonical image-size variant strings.
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/manifest` — manifest loading/rendering.
- `github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/runtime` — `manifest` subcommand + `Runtime` server scaffolding, including `ServeManifestWithOptions` options such as `WithConfigure`, `WithAuthProviderChecks` and `WithConfigurationChecks`.
Expand Down
79 changes: 79 additions & 0 deletions pkg/pluginsdk/httproute/viewer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Package httproute reads what the host tells an http_routes.v1 handler about
// the person making a request.
package httproute

import (
"strconv"
"strings"

pluginv1 "github.com/Silo-Server/silo-plugin-sdk/pkg/pluginproto/silo/plugin/v1"
)

// Headers the host sets when it proxies a browser's or app's request to a
// plugin route. The host builds them from its own session and drops any
// X-Silo- header the client sent, so a handler can trust them on those
// requests.
const (
HeaderUserID = "X-Silo-User-Id"
HeaderUserRole = "X-Silo-User-Role"
HeaderUserName = "X-Silo-User-Name"
HeaderProfileID = "X-Silo-Profile-Id"
HeaderProfileName = "X-Silo-Profile-Name"
HeaderProfilePrimary = "X-Silo-Profile-Primary"
HeaderTheme = "X-Silo-Theme"
)

// Viewer is the signed-in person a request comes from.
type Viewer struct {
// UserID is the account. Profile IDs repeat across accounts, so key
// per-profile data by UserID and ProfileID together.
UserID int
// Admin is true for an account with the server admin role. It is not
// ProfilePrimary.
Admin bool
// UserName is the account's username, when the host knows it.
UserName string
// ProfileID is the household profile the host verified for the request,
// or empty when none was selected.
ProfileID string
// ProfileName is the profile's display name, when the host knows it.
ProfileName string
// ProfilePrimary is true for the account's primary (household parent)
// profile.
ProfilePrimary bool
// Theme is the viewer's UI theme, such as "dark", when set.
Theme string
}

// ViewerOf returns the signed-in viewer of a request. It reports false when
// the request has no signed-in account, as on a public route.
func ViewerOf(req *pluginv1.HandleHTTPRequest) (Viewer, bool) {
userID, err := strconv.Atoi(Header(req, HeaderUserID))
if err != nil || userID <= 0 {
return Viewer{}, false
}
return Viewer{
UserID: userID,
Admin: Header(req, HeaderUserRole) == "admin",
UserName: Header(req, HeaderUserName),
ProfileID: Header(req, HeaderProfileID),
ProfileName: Header(req, HeaderProfileName),
ProfilePrimary: Header(req, HeaderProfilePrimary) == "true",
Theme: Header(req, HeaderTheme),
}, true
}

// Header returns the request header name, matched case-insensitively, with
// surrounding space trimmed. It returns "" when the header is absent.
func Header(req *pluginv1.HandleHTTPRequest, name string) string {
headers := req.GetHeaders()
if value, ok := headers[name]; ok {
return strings.TrimSpace(value)
}
for key, value := range headers {
if strings.EqualFold(key, name) {
return strings.TrimSpace(value)
Comment thread
Quick104 marked this conversation as resolved.
Resolved
}
}
return ""
}
45 changes: 45 additions & 0 deletions pkg/pluginsdk/httproute/viewer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package httproute_test

import (
"testing"

pluginv1 "github.com/Silo-Server/silo-plugin-sdk/pkg/pluginproto/silo/plugin/v1"
"github.com/Silo-Server/silo-plugin-sdk/pkg/pluginsdk/httproute"
)

func TestViewerOf(t *testing.T) {
for _, tc := range []struct {
name string
headers map[string]string
want httproute.Viewer
wantOK bool
}{
{name: "public route", headers: map[string]string{"Accept": "text/html"}},
{name: "malformed user id", headers: map[string]string{"X-Silo-User-Id": "seven"}},
{name: "zero user id", headers: map[string]string{"X-Silo-User-Id": "0"}},
{
name: "admin on a primary profile",
headers: map[string]string{
"X-Silo-User-Id": "7", "X-Silo-User-Role": "admin", "X-Silo-User-Name": "josh",
"X-Silo-Profile-Id": "p1", "X-Silo-Profile-Name": "Josh", "X-Silo-Profile-Primary": "true",
"X-Silo-Theme": "dark",
},
want: httproute.Viewer{UserID: 7, Admin: true, UserName: "josh", ProfileID: "p1", ProfileName: "Josh", ProfilePrimary: true, Theme: "dark"},
wantOK: true,
},
{
// A proxy or test client may change the case of header names.
name: "user without a profile, lowercase names",
headers: map[string]string{"x-silo-user-id": " 12 ", "x-silo-user-role": "user"},
want: httproute.Viewer{UserID: 12},
wantOK: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
got, ok := httproute.ViewerOf(&pluginv1.HandleHTTPRequest{Headers: tc.headers})
if ok != tc.wantOK || got != tc.want {
t.Fatalf("ViewerOf() = %+v, %v; want %+v, %v", got, ok, tc.want, tc.wantOK)
}
})
}
}
Loading