Repository navigation
feat(httproute): read the request's viewer from host headers #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.