This commit is contained in:
1 parent
1d99f0272d
commit
b1412a9016
7 files changed
+476
-51
No files matched your search
@@ -63,8 +63,20 @@ type Client struct {
|
||||
// when Middleware has no Authorization header to use. Defaults to
|
||||
// "auth_session", matching AuthServer's SessionCookieName.
|
||||
cookieName string
|
||||
// projectKey identifies the calling project to AuthServer (sent as the
|
||||
// ProjectKeyHeader on every request) so per-user project-access
|
||||
// restrictions can be enforced. Empty means "don't identify" - the
|
||||
// caller is treated as unrestricted; see AuthServer's project-scoping
|
||||
// feature and WithProjectKey.
|
||||
projectKey string
|
||||
}
|
||||
|
||||
// ProjectKeyHeader is the header Client attaches to every request identifying
|
||||
// which project is calling, so AuthServer can enforce per-user project-access
|
||||
// restriction (see WithProjectKey). AuthServer treats a missing/empty header
|
||||
// the same as "this caller has no project identity": never restricted.
|
||||
const ProjectKeyHeader = "X-Project-Key"
|
||||
|
||||
// Option configures optional Client behavior.
|
||||
type Option func(*Client)
|
||||
|
||||
@@ -81,6 +93,16 @@ func WithCookieName(name string) Option {
|
||||
return func(c *Client) { c.cookieName = name }
|
||||
}
|
||||
|
||||
// WithProjectKey identifies this Client's project to AuthServer (see
|
||||
// ProjectKeyHeader), enabling per-user project-access restriction. key is
|
||||
// normally a project's own lowercase identifier, matching the same key
|
||||
// ProjectConfigurator's Projects workspace registers it under (e.g.
|
||||
// "carriernode", "gamestream", "omnilogger") - see AuthServer's project-scope
|
||||
// admin UI, which assigns users access using these same keys.
|
||||
func WithProjectKey(key string) Option {
|
||||
return func(c *Client) { c.projectKey = strings.ToLower(strings.TrimSpace(key)) }
|
||||
}
|
||||
|
||||
// New creates a Client for the AuthServer instance at baseURL. baseURL may
|
||||
// be given without a scheme (e.g. "auth.example.com" or "192.168.1.50:8080")
|
||||
// - the scheme is then inferred from the host: a loopback, RFC1918/RFC4193
|
||||
@@ -124,6 +146,15 @@ func (c *Client) getBaseURL() string {
|
||||
return c.baseURL
|
||||
}
|
||||
|
||||
// setProjectKeyHeader attaches ProjectKeyHeader when this Client was built
|
||||
// with WithProjectKey; a no-op otherwise, so an unconfigured Client behaves
|
||||
// exactly as it did before project scoping existed.
|
||||
func (c *Client) setProjectKeyHeader(req *http.Request) {
|
||||
if c.projectKey != "" {
|
||||
req.Header.Set(ProjectKeyHeader, c.projectKey)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeBaseURL(baseURL string) string {
|
||||
trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
if trimmed == "" {
|
||||
@@ -186,6 +217,7 @@ func (c *Client) Validate(ctx context.Context, token string) (*User, error) {
|
||||
return nil, fmt.Errorf("authclient: build request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
c.setProjectKeyHeader(req)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -418,6 +450,7 @@ func (c *Client) call(ctx context.Context, method, path, token string, body, out
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
c.setProjectKeyHeader(req)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user