feat(auth.m2m): per-client custom claims in client_credentials tokens#236
Merged
feat(auth.m2m): per-client custom claims in client_credentials tokens#236
Conversation
Co-authored-by: intel352 <77607+intel352@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add tenant identity to auth.m2m tokens
feat(auth.m2m): per-client custom claims in client_credentials tokens
Mar 3, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for embedding per-client custom claims into auth.m2m client_credentials JWTs so downstream workflows can rely on signed tenant/affiliate identity instead of untrusted headers.
Changes:
- Extends
M2MClientwith aClaims map[string]anyfield and includes it during token issuance. - Updates the
auth.m2mmodule factory to parseclaimsfrom client config entries. - Adds tests for custom-claim inclusion and protected-claim non-overridability; updates schema text to document
claims.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| plugins/auth/plugin.go | Parses per-client claims from config and wires them into registered M2M clients; updates schema description. |
| plugins/auth/plugin_test.go | Adds a factory-level test constructing auth.m2m with client claims. |
| module/auth_m2m.go | Adds Claims to M2MClient and passes them into issueToken for client_credentials. |
| module/auth_m2m_test.go | Adds unit tests validating custom claims appear in issued tokens and cannot override protected standard claims. |
Comments suppressed due to low confidence (1)
plugins/auth/plugin_test.go:170
TestModuleFactoryM2MWithClaimsonly asserts a 200 response from the token endpoint, which would still pass even if theclaimsconfig isn’t parsed/applied. To actually cover the new behavior in the plugin factory, decode the JSON response, authenticate/parse the returned access token, and assert the expected custom claim (e.g.tenant_id) is present (and optionally that protected claims still can’t be overridden).
func TestModuleFactoryM2MWithClaims(t *testing.T) {
p := New()
factories := p.ModuleFactories()
mod := factories["auth.m2m"]("m2m-test", map[string]any{
"algorithm": "HS256",
"secret": "this-is-a-valid-secret-32-bytes!",
"clients": []any{
map[string]any{
"clientId": "org-alpha",
"clientSecret": "secret-alpha",
"scopes": []any{"read"},
"claims": map[string]any{
"tenant_id": "alpha",
},
},
},
})
if mod == nil {
t.Fatal("auth.m2m factory returned nil")
}
m2mMod, ok := mod.(*module.M2MAuthModule)
if !ok {
t.Fatal("expected *module.M2MAuthModule")
}
// Issue a token via the Handle method.
params := url.Values{
"grant_type": {"client_credentials"},
"client_id": {"org-alpha"},
"client_secret": {"secret-alpha"},
}
req := httptest.NewRequest(http.MethodPost, "/oauth/token", strings.NewReader(params.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
m2mMod.Handle(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d; body: %s", w.Code, w.Body.String())
}
}
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
auth.m2mtokens carried only standard JWT claims, forcing downstream services to trust untrusted request headers (e.g.X-Tenant-Id) for tenant identity. This adds a per-clientclaimsmap that is cryptographically embedded in issued tokens.Changes
M2MClient— addsClaims map[string]anyfieldhandleClientCredentials— passesclient.ClaimstoissueToken; existing standard-claim protection (iss,sub,iat,exp,scope) blocks any override attemptsauth.m2mfactory — parsesclaims: map[string]anyfrom each client config entryclientsfield description to documentclaimsExample
Issued tokens will include
tenant_idalongside standard claims.step.auth_validatealready extracts all JWT claims into pipeline context, so downstream steps gettenant_idautomatically without trusting headers.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.