authn: use a single principal model instead of splitting users and clients by grant_type
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 15/100
- Loại issue
- Tái cấu trúc
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- go
- Lĩnh vực
- authentication
Hướng nghiên cứu
Start with the grant_type dispatch in authn/token_parser.go (around line 262) and the user/client split in the principal, AuthContext and Config types. Then check the GetOrCreateUser call in authn/middleware.go (around line 74) and the UserProfileService parameter. Done means one Principal type, one per-claim list that applies to every token, UserProfileService removed, authn/README.md updated with the provisioning-middleware pattern, and the tests passing. This is a breaking API change that needs maintainer agreement on the design first.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Current Limitation
authn splits every authenticated caller into a user or a client principal based on the token's grant_type claim (authn/token_parser.go:262): authorization_code gives a user, client_credentials gives a client, and anything else is rejected. That one decision is copied across the whole package:
UserPrincipal/ClientPrincipalUserContext/ClientContextonAuthContextConfig.UserClaims/Config.ClientClaims,WithUserClaims/WithClientClaimsPrincipalTypeandAuthContext.Type()
Problems with this:
- Permissions shouldn't depend on how the token was obtained. A system integrating directly via client credentials should be able to do exactly what a person can do, as long as it holds the same roles and scopes. Treating them as different kinds of principal invites authorization logic that branches on token type instead of on permissions.
- It adds no value. Nothing outside
authnbranches on the principal type. Withinauthnthe only behavioural difference is thatUserProfileService.GetOrCreateUserruns for user principals only (authn/middleware.go:74). Everything else is the same data (subject/ID, roles, scopes, extra claims) written out twice.AuthContextthen flattens it again throughSubject()/Roles()/Scopes()/ExtraClaims(), whose own doc comment says authz should use them "rather than … branching on principal type". - It ties the library to one vendor.
grant_typeis not a standard access-token claim (it's not in RFC 9068). IdPs that don't emit it have every token rejected asunsupported grant type. - The claim declarations are split for no reason. You declare the same
Required/Optionalclaims once per principal kind, and validate them twice.
Suggested Improvement
Have one principal model, and one claim declaration that applies to every token.
-
Validation: a token is accepted when its signature,
iss,audandexpcheck out. Dropgrant_type-based dispatch and theunsupported grant typerejection. -
Claims: replace
UserClaims/ClientClaims(andWithUserClaims/WithClientClaims, and theClaimSpectwo-list shape) with a single list of per-claim declarations that applies to every token:claims: - name: email required: true - name: ouId # required defaults to false - name: given_nametype Claim struct { Name string `yaml:"name"` Required bool `yaml:"required"` } type Config struct { // ... Claims []Claim `yaml:"claims"` } func WithClaims(claims ...Claim) Option- One entry per claim. Everything about a claim is declared in one place, and the contradictory "listed in both
OptionalandRequired" state (currently resolved silently as required) can't exist. requiredis a boolean, defaulting tofalse(optional).- Duplicate names are rejected.
Validateerrors on a name declared twice, and so do repeatedWithClaimscalls; there is no "required wins" merge. - Extensible. Future per-claim settings (e.g. a nested path, an expected type) become fields on
Claiminstead of more parallel lists. - Applies to every token. A
requiredclaim missing from any token rejects it. Requirements that only make sense for some callers (e.g. "a human must haveemail") should be declared optional and enforced in the handler or authz. - Declared claims are extracted from every token that carries them; previously a name declared only for one principal type was never extracted from the other.
- Required/optional value semantics, exact (non-nested) name matching, and the
ExtraClaimsaccessors are unchanged. - Reserved names:
grant_typeis no longer reserved, since nothing dispatches on it.client_id(andazp),scope, the roles claim, and the JWT registered claims stay reserved.
- One entry per claim. Everything about a claim is declared in one place, and the contradictory "listed in both
-
Principal: a single
PrincipalwithSubject(sub),ClientID(client_id, falling back toazp),Roles,Scopes,ExtraClaims. -
AuthContext: a single shape carrying the principal's fields. Remove
PrincipalType,Type(), and theUser/Clientbranches. -
Remove
UserProfileServiceentirely, with no replacement hook. User provisioning is the only behaviour that currently depends on "is this a human?", and it isn't authentication.RequireAuth()already returns a plainfunc(http.Handler) http.Handler, so an application that wants to provision users (or do anything else with the validated principal) chains its own middleware after it:// in the application, not in authn func ProvisionUser(users *user.Service) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { p := authn.GetAuthContext(r.Context()) u, err := users.GetOrCreate(r.Context(), p.Subject(), p.ExtraClaims()) if err != nil { http.Error(w, "user provisioning failed", http.StatusInternalServerError) return } next.ServeHTTP(w, r.WithContext(user.NewContext(r.Context(), u))) }) } } // wrap all protected routes once, not per route api := http.NewServeMux() api.HandleFunc("POST /api/x", handlerX) root := http.NewServeMux() root.Handle("/api/", authManager.RequireAuth()(ProvisionUser(users)(api)))Why not a library-provided hook instead:
- Middleware is the standard Go extension point, and
authnalready produces one. A hook would be a second, library-specific way to do the same thing. - The application owns the policy: who gets a record, what to attach to the context, and whether a failure rejects the request (today
GetOrCreateUsererrors are logged and the request proceeds with a half-populated context). - No extra public API to document and keep stable, and
authnstops knowing about users at all.AuthContextno longer carries a persisted user ID field.
Document the provisioning middleware and the sub-mux wiring in
authn/README.mdas the migration path. - Middleware is the standard Go extension point, and
-
Update
authn/README.mdand the tests.
This is a breaking change to Config, the extractor options, Principal, AuthContext, and NewManager / Middleware / RequireAuth (the UserProfileService parameter and interface are removed).
Version
9668a52
Additional Context
Related: #289 (remove the ClientIDs allowlist). The two can be done independently. Together they bring authn down to standard JWT access-token validation (signature, issuer, audience, expiry) plus extraction of the claims the consumer declares, and leave authorization decisions to authz.
- Ngôn ngữ chính
- Go
- Star
- 6
- Fork
- 8
- Merge trung bình
- 21 giờ 36 phút
- Pull request đã merge (30 ngày)
- 42
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của OpenNSW/core
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
payment: test fixtures hardcode real gateway names (govpay/lankapay) instead of generic placeholdersĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
nsw-sl cleanup Type/Improvement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
Maintainer thường phản hồi trong vòng 1 ngày
-
remote: let the host inject a logger, and log with one consistent policyCó thể đã có người làm @mushrafmim đã nhận 5 ngày trước. Đang mởType/Improvement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 22/100
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
agent-research-finding chore
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
jordansmall/spindrift#7370 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
☢️ Bug v3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
The repo's gates check is clean and runs need no swapsCó thể đã có người làm @turbot-agent-fleet đã nhận hôm nay. Đang mởagent:ready
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
turbot/tailpipe#677 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-kind needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
crossplane-contrib/provider-keycloak#806 ·
Maintainer thường phản hồi trong vòng 1 ngày