Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
a672066
feat(coderd/rbac): compare scopes by permission coverage
BobbyHo Aug 14, 2026
4315706
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 17, 2026
787c461
docs(coderd/rbac): shorten the ScopesCover doc comment
BobbyHo Aug 17, 2026
2d39e04
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 18, 2026
2f6c44e
fix(coderd/rbac): guard allowed-side org and user permissions
BobbyHo Aug 18, 2026
bd40270
refactor(coderd/rbac): drop the unreachable negative skip in coverage
BobbyHo Aug 18, 2026
3139c54
test(coderd/rbac): pin the scope coverage table's weak assertions
BobbyHo Aug 18, 2026
9276bb8
refactor(coderd/rbac): make the coverage guards reachable from tests
BobbyHo Aug 18, 2026
865eb9a
refactor(coderd/rbac): share the alias table and name the canonical c…
BobbyHo Aug 18, 2026
26a6bed
docs(coderd/rbac): document the expansion invariant where it can be b…
BobbyHo Aug 18, 2026
7cca7b3
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 19, 2026
1678a77
test(coderd/rbac): pin the coverage guards at one strength
BobbyHo Aug 19, 2026
aba3c6c
fix(coderd/rbac): name the scope once in expansion errors
BobbyHo Aug 19, 2026
9a08105
docs(coderd/rbac): correct the external scope list contract
BobbyHo Aug 19, 2026
2fcce8b
docs(coderd/rbac): trim the restated coverage invariant
BobbyHo Aug 19, 2026
a775a48
docs(coderd/rbac): correct the negative permission cross-reference
BobbyHo Aug 19, 2026
e0c0d4a
docs(coderd/rbac): name every category IsExternalScope admits
BobbyHo Aug 19, 2026
189740d
test(coderd/rbac): pin the alias list invariants on the alias table
BobbyHo Aug 19, 2026
7d08e49
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 19, 2026
ca4dc52
feat(coderd/oauth2provider): negotiate and persist authorization scope
BobbyHo Aug 14, 2026
f402e1a
feat(site): state the negotiated scope on the OAuth2 consent page
BobbyHo Aug 14, 2026
28ef5ba
fix(coderd/oauth2provider): return invalid_scope to the client's call…
BobbyHo Aug 14, 2026
62950d3
test(coderd/rbac): close two mutation gaps in scope coverage tests
BobbyHo Aug 20, 2026
554a703
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 20, 2026
09bc1b4
refactor(coderd/oauth2provider): rename validateRequestedScope to neg…
BobbyHo Aug 21, 2026
6fe6c71
test(coderd/oauth2provider): bind the wire-level scope reasons to the…
BobbyHo Aug 21, 2026
e296dda
refactor(coderd): log undecidable oauth2 scope coverage instead of re…
BobbyHo Aug 21, 2026
2760db5
fix(coderd/oauth2provider): name the stored allowlist when none of it…
BobbyHo Aug 21, 2026
5b2ca57
refactor(coderd/oauth2provider): reword the oauth2 scope rejection re…
BobbyHo Aug 21, 2026
90a6f83
docs: correct what the oauth2 authorize scope parameter promises
BobbyHo Aug 21, 2026
77654a1
test(coderd/oauth2provider): fold the no-allowlist guarantee into one…
BobbyHo Aug 21, 2026
dfdd33f
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 22, 2026
ba8a123
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 22, 2026
bc40908
test(coderd): close the consent-page and state-echo test gaps
BobbyHo Aug 23, 2026
cb7df2e
fix(site/static): left-align the consent permission list
BobbyHo Aug 23, 2026
41e9f40
fix(coderd/oauth2provider): validate the callback scheme before any r…
BobbyHo Aug 23, 2026
a56cdac
fix(coderd/oauth2provider): replace callback query params instead of …
BobbyHo Aug 23, 2026
009962c
feat: say that the consent permission names are technical
BobbyHo Aug 23, 2026
f983335
fix: state unrestricted OAuth2 consent in its own field
BobbyHo Aug 23, 2026
81bbfb0
docs: trim OAuth2 scope negotiation comments
BobbyHo Aug 23, 2026
b7f88f0
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 23, 2026
2a2e006
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 23, 2026
a11d42c
test(coderd/oauth2provider): pin the consent list semantics and the s…
BobbyHo Aug 23, 2026
eb37c2f
test(coderd/oauth2provider): cover the error redirect in the query-pa…
BobbyHo Aug 23, 2026
a5e3f94
fix(coderd/oauth2provider): answer 500 and log on an unusable callbac…
BobbyHo Aug 23, 2026
60cbe59
fix: refuse to render a consent page for a grant with no permissions
BobbyHo Aug 23, 2026
fdc9532
docs: document invalid_scope rejections on the authorize endpoint
BobbyHo Aug 23, 2026
e63ff3a
docs(coderd): trim the scope test comments
BobbyHo Aug 25, 2026
a9ade4a
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 25, 2026
5589bf6
docs: trim the consent page and error redirect comments
BobbyHo Aug 25, 2026
24b4f46
docs(coderd): drop the godoc-style comments from the scope tests
BobbyHo Aug 25, 2026
b92b7f9
docs(coderd/oauth2provider): trim the authorize.go comments
BobbyHo Aug 25, 2026
e4cc41d
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 25, 2026
20caa9c
docs(coderd): trim the scope reporting test comments
BobbyHo Aug 25, 2026
c7d3d53
docs(site): trim the consent page scope comments
BobbyHo Aug 25, 2026
98545a6
feat(coderd): mint OAuth2 tokens with the negotiated scope
BobbyHo Aug 15, 2026
641e45e
test(coderd/oauth2provider): cover the negotiated scope end to end
BobbyHo Aug 18, 2026
1a30a9c
feat(coderd/oauth2provider): report the granted scope in token responses
BobbyHo Aug 18, 2026
8b46823
refactor(coderd/oauth2provider): clarify the unmintable scope sentinel
BobbyHo Aug 18, 2026
da5b590
test(coderd/oauth2provider): derive the exchange query from authorize…
BobbyHo Aug 18, 2026
fc6eb77
docs: scope the OAuth2 refresh limitation to narrowing
BobbyHo Aug 18, 2026
589b8b8
docs(coderd): trim the negotiated scope comments
BobbyHo Aug 25, 2026
78fbab1
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 26, 2026
f675cf1
fix(site/static): make the whole consent page reachable by scrolling
BobbyHo Aug 27, 2026
6b5f01d
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Aug 27, 2026
90821d8
docs: say the negotiated scope now bounds the issued token
BobbyHo Aug 27, 2026
72b8cb5
docs: annotate the token endpoint with the response it returns
BobbyHo Aug 27, 2026
b6f74c3
feat: re-check the app allowlist at OAuth2 code redemption
BobbyHo Aug 28, 2026
9f3193e
docs(coderd/oauth2provider): trim the allowlist re-check comments
BobbyHo Aug 28, 2026
9e98cef
fix(coderd): make OAuth2 code redemption single-use under concurrency
BobbyHo Aug 28, 2026
48cb5a4
feat: let an OAuth2 refresh narrow the granted scope
BobbyHo Aug 29, 2026
ceb3997
Update coderd/oauth2provider/authorize.go
BobbyHo Aug 31, 2026
2cd9b60
Update coderd/oauth2provider/authorize.go
BobbyHo Aug 31, 2026
d160bda
docs(coderd/oauth2provider): trim the errNoGrantableScope comment
BobbyHo Aug 31, 2026
b60eee6
refactor(coderd/oauth2provider): canonicalize scopes before the catal…
BobbyHo Aug 31, 2026
4732d1f
refactor(coderd/oauth2provider): canonicalize the allowlist in one pass
BobbyHo Aug 31, 2026
03df6a9
test(coderd/rbac): pin the field sets scope coverage reads
BobbyHo Aug 31, 2026
a2d2856
docs(coderd/rbac): trim the TestCoverageModelFields comment
BobbyHo Aug 31, 2026
d83636b
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 31, 2026
037e744
Merge remote-tracking branch 'origin/main' into plat479-3-report-nego…
BobbyHo Aug 31, 2026
cd35b94
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Aug 31, 2026
508d09e
Merge branch 'plat480-1-apply-negotiated-scope' into plat480-2-stale-…
BobbyHo Aug 31, 2026
fc40ac3
Merge branch 'plat480-2-stale-scope-recheck' into plat480-3-single-us…
BobbyHo Aug 31, 2026
0e2bf76
Merge branch 'plat480-3-single-use-code' into plat481-1-narrow-refres…
BobbyHo Aug 31, 2026
433e449
docs(coderd/oauth2provider): trim scope negotiation comments
BobbyHo Aug 31, 2026
5dffb74
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Aug 31, 2026
6aa7510
docs(coderd/oauth2provider): trim scope negotiation comments
BobbyHo Aug 31, 2026
a1eae58
Merge branch 'plat480-1-apply-negotiated-scope' into plat480-2-stale-…
BobbyHo Aug 31, 2026
e1ea400
docs(coderd/oauth2provider): trim scope negotiation comments
BobbyHo Aug 31, 2026
52567a4
Merge branch 'plat480-2-stale-scope-recheck' into plat480-3-single-us…
BobbyHo Aug 31, 2026
8efc783
docs(coderd/oauth2provider): trim the single-use code comment
BobbyHo Aug 31, 2026
124d652
Merge branch 'plat480-3-single-use-code' into plat481-1-narrow-refres…
BobbyHo Aug 31, 2026
5b08b81
docs(coderd/oauth2provider): trim refresh scope comments
BobbyHo Aug 31, 2026
35ce008
Merge branch 'main' into plat479-3-report-negotiated-scope
BobbyHo Aug 31, 2026
48d9c7f
fix(coderd/oauth2provider): return server_error for undecidable scope…
BobbyHo Sep 1, 2026
c8a3754
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Sep 1, 2026
339aa34
Merge branch 'plat480-1-apply-negotiated-scope' into plat480-2-stale-…
BobbyHo Sep 1, 2026
6b29b13
Merge branch 'main' into plat480-1-apply-negotiated-scope
BobbyHo Sep 1, 2026
976335d
fix: answer invalid_grant for an unmintable stored scope
BobbyHo Sep 1, 2026
5ebc706
Merge branch 'plat480-1-apply-negotiated-scope' into plat480-2-stale-…
BobbyHo Sep 1, 2026
4a31118
Merge remote-tracking branch 'origin/main' into plat480-2-stale-scope…
BobbyHo Sep 2, 2026
cf7ddb7
Merge branch 'plat480-2-stale-scope-recheck' into plat480-3-single-us…
BobbyHo Sep 2, 2026
7a251f1
Merge branch 'plat480-3-single-use-code' into plat481-1-narrow-refres…
BobbyHo Sep 2, 2026
c54ccdb
fix(coderd): address the allowlist re-check review
BobbyHo Sep 2, 2026
d5c8a1c
docs(coderd): simplify the scope check comments
BobbyHo Sep 2, 2026
bb574d0
Merge branch 'plat480-2-stale-scope-recheck' into plat480-3-single-us…
BobbyHo Sep 2, 2026
40fda97
Merge branch 'plat480-3-single-use-code' into plat481-1-narrow-refres…
BobbyHo Sep 2, 2026
886ec18
refactor(coderd/oauth2provider): rename the coverage wrapper
BobbyHo Sep 2, 2026
d9d032d
test(coderd): cover replay and post-redemption state for single-use c…
BobbyHo Sep 3, 2026
8acedcd
Merge remote-tracking branch 'origin/main' into plat480-3-single-use-…
BobbyHo Sep 4, 2026
361f669
Merge branch 'plat480-3-single-use-code' into plat481-1-narrow-refres…
BobbyHo Sep 4, 2026
85030f8
docs(coderd/oauth2provider): correct the canonicalization cross-refer…
BobbyHo Sep 4, 2026
ec80733
test(coderd/oauth2provider): assert the winner's token survives a ref…
BobbyHo Sep 4, 2026
98e6191
refactor(coderd): fold the duplicated app code delete into one query
BobbyHo Sep 4, 2026
15cc66f
docs(coderd): say why the code is deleted, and where single use can b…
BobbyHo Sep 4, 2026
b64f111
test(coderd/oauth2provider): enforce the redemption overlap in the test
BobbyHo Sep 4, 2026
270bfb5
Merge branch 'plat480-3-single-use-code' into plat481-1-narrow-refres…
BobbyHo Sep 4, 2026
f644e52
fix(coderd/oauth2provider): bound and sanitize the token endpoint's e…
BobbyHo Sep 4, 2026
663fa85
fix(coderd/oauth2provider): narrow the access token, not the grant
BobbyHo Sep 4, 2026
8f87b2d
fix(coderd/oauth2provider): name the way out when a refresh cannot widen
BobbyHo Sep 5, 2026
f54bbbc
fix(coderd/oauth2provider): canonicalize the ceiling once in narrowAc…
BobbyHo Sep 5, 2026
8d5f168
refactor(coderd/oauth2provider): address the review's nits and notes
BobbyHo Sep 5, 2026
e1b4f06
docs(admin/integrations): file the refresh narrowing under the code i…
BobbyHo Sep 5, 2026
2ba380a
test(coderd/oauth2provider): assert through the endpoint, not around it
BobbyHo Sep 5, 2026
755b5a1
fix(coderd/oauth2provider): honour scope on the exchange, and log sco…
BobbyHo Sep 5, 2026
e1f3f75
docs(coderd/oauth2provider): trim the scope comments
BobbyHo Sep 6, 2026
4243eb4
fix(coderd): make OAuth2 refresh token redemption single-use under co…
BobbyHo Aug 29, 2026
d15dca4
docs(coderd/oauth2provider): trim the single-use refresh comment
BobbyHo Aug 31, 2026
b94b304
test(coderd/oauth2provider): share the single-use race harness with t…
BobbyHo Sep 6, 2026
b6018df
refactor(coderd/oauth2provider): declare the refresh delete error, do…
BobbyHo Sep 6, 2026
1c3f276
test(coderd/oauth2provider): pin the refresh race at the token read
BobbyHo Sep 6, 2026
81bad9e
docs(coderd/oauth2provider): say plainly why the refresh delete is in…
BobbyHo Sep 6, 2026
8fc16f0
fix(coderd/oauth2provider): let the refresh delete alone arbitrate th…
BobbyHo Sep 6, 2026
210409d
test(coderd/oauth2provider): pin invalid_grant for a refresh whose ke…
BobbyHo Aug 29, 2026
9a7fba4
fix(coderd/oauth2provider): stop echoing a scope that names nothing
BobbyHo Sep 5, 2026
c8d07ad
fix(coderd/oauth2provider): pin READ COMMITTED for the single-use del…
BobbyHo Sep 6, 2026
b55edf3
fix(coderd/oauth2provider): log a refused reuse of a code or refresh …
BobbyHo Sep 6, 2026
17cc715
docs(coderd/database): give the returning-row key delete the same doc…
BobbyHo Sep 6, 2026
7abfb3f
test(coderd/database/dbauthz): pin that a missed fetch still matches …
BobbyHo Sep 6, 2026
9510422
test(coderd/oauth2provider): bound the race barrier and check the acc…
BobbyHo Sep 6, 2026
8d1c4f8
Merge remote-tracking branch 'origin/main' into plat481-1-narrow-refr…
BobbyHo Sep 9, 2026
283cad4
Merge branch 'plat481-1-narrow-refresh-scope' into plat481-2-single-u…
BobbyHo Sep 9, 2026
017c725
Merge remote-tracking branch 'origin/main' into plat481-2-single-use-…
BobbyHo Sep 9, 2026
dbdff37
Merge branch 'main' into plat481-2-single-use-refresh
BobbyHo Sep 9, 2026
fefddb5
Merge branch 'main' into plat481-2-single-use-refresh
BobbyHo Sep 9, 2026
2bacd8c
Merge branch 'main' into plat481-2-single-use-refresh
BobbyHo Sep 10, 2026
1cebdf7
Merge branch 'main' into plat481-2-single-use-refresh
BobbyHo Sep 10, 2026
ea5e03f
refactor(coderd/oauth2provider): extract OAuth2 client authentication…
BobbyHo Sep 11, 2026
097e59a
test(coderd/oauth2provider): create the test context inside each para…
BobbyHo Sep 11, 2026
49e9f5d
docs(coderd/oauth2provider): shorten the mergeBasicClientAuth comment
BobbyHo Sep 11, 2026
6bbace7
docs(coderd/oauth2provider): shorten the authenticateClient comments
BobbyHo Sep 11, 2026
12e30c2
Merge branch 'main' into plat481-2-single-use-refresh
BobbyHo Sep 11, 2026
5b571d1
Merge branch 'plat481-2-single-use-refresh' into coder-sec-348-authen…
BobbyHo Sep 11, 2026
af19d96
docs(coderd/oauth2provider): explain the app binding in authenticateC…
BobbyHo Sep 11, 2026
314e184
chore(coderd/oauth2provider): address review of the client auth helpers
BobbyHo Sep 11, 2026
4876e0c
Merge branch 'main' into coder-sec-348-authenticate-client
BobbyHo Sep 11, 2026
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
88 changes: 55 additions & 33 deletions coderd/oauth2provider/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,9 @@ func extractTokenRequest(r *http.Request, logger slog.Logger, primary *url.URL,
Scope: p.String(vals, "", "scope"),
}

// RFC 6749 §2.3.1: confidential clients may authenticate via HTTP Basic.
if user, pass, ok := r.BasicAuth(); ok && user != "" {
if req.ClientID != "" && req.ClientID != user {
return codersdk.OAuth2TokenRequest{}, nil, errConflictingClientAuth
}
if req.ClientSecret != "" && req.ClientSecret != pass {
return codersdk.OAuth2TokenRequest{}, nil, errConflictingClientAuth
}

req.ClientID = user
req.ClientSecret = pass
req.ClientID, req.ClientSecret, err = mergeBasicClientAuth(r, req.ClientID, req.ClientSecret)
if err != nil {
return codersdk.OAuth2TokenRequest{}, nil, err
}

// Grant-specific required checks that can be satisfied via HTTP Basic.
Expand Down Expand Up @@ -255,6 +247,57 @@ func extractTokenRequest(r *http.Request, logger slog.Logger, primary *url.URL,
return req, nil, nil
}

// mergeBasicClientAuth combines a confidential client's HTTP Basic
// credentials (RFC 6749 §2.3.1) with the form client_id and client_secret.
// Without a Basic header, or with an empty Basic username, the form values
// pass through unchanged. Otherwise each form field must be empty or equal
// to its header counterpart, or the result is errConflictingClientAuth. An
// empty Basic password still counts as a presented password, so a form
// secret beside it is a conflict.
func mergeBasicClientAuth(r *http.Request, clientID, clientSecret string) (mergedID, mergedSecret string, err error) {
user, pass, ok := r.BasicAuth()
if !ok || user == "" {
return clientID, clientSecret, nil
}
if clientID != "" && clientID != user {
return "", "", errConflictingClientAuth
}
if clientSecret != "" && clientSecret != pass {
return "", "", errConflictingClientAuth
}
return user, pass, nil
}

// authenticateClient checks a client secret and confirms it belongs to the
Comment thread
BobbyHo marked this conversation as resolved.
// app named by client_id. That id arrives unverified, so without the app
// check a valid secret for one app could issue a token for another. It
// returns the matched secret row. Every authentication failure returns
// errBadSecret so the response does not reveal which step failed; a
// datastore failure returns the underlying error. Callers skip it for public
// clients, which have no secret and are bound by PKCE and the token's app id
// instead.
func authenticateClient(ctx context.Context, db database.Store, app database.OAuth2ProviderApp, clientSecret string) (database.OAuth2ProviderAppSecret, error) {
Comment thread
BobbyHo marked this conversation as resolved.
secret, err := ParseFormattedSecret(clientSecret)
if err != nil {
return database.OAuth2ProviderAppSecret{}, errBadSecret
}
//nolint:gocritic // OAuth2 system context, users cannot read secrets
dbSecret, err := db.GetOAuth2ProviderAppSecretByPrefix(dbauthz.AsSystemOAuth2(ctx), []byte(secret.Prefix))
if errors.Is(err, sql.ErrNoRows) {
return database.OAuth2ProviderAppSecret{}, errBadSecret
}
if err != nil {
Comment thread
BobbyHo marked this conversation as resolved.
return database.OAuth2ProviderAppSecret{}, err
}
if !apikey.ValidateHash(dbSecret.HashedSecret, secret.Secret) {
return database.OAuth2ProviderAppSecret{}, errBadSecret
}
if dbSecret.AppID != app.ID {
Comment thread
BobbyHo marked this conversation as resolved.
return database.OAuth2ProviderAppSecret{}, errBadSecret
}
return dbSecret, nil
}

// writeTokenError renders an RFC 6749 §5.2 error body. Descriptions can quote
// what the client sent, so they are confined and capped here rather than at each
// call site, leaving the guarantee with the endpoint.
Expand Down Expand Up @@ -427,31 +470,10 @@ func authorizationCodeGrant(ctx context.Context, db database.Store, logger slog.
// client instead.
var appSecretID uuid.NullUUID
if !app.IsPublic() {
secret, err := ParseFormattedSecret(req.ClientSecret)
if err != nil {
return codersdk.OAuth2TokenResponse{}, errBadSecret
}
//nolint:gocritic // OAuth2 system context, users cannot read secrets
dbSecret, err := db.GetOAuth2ProviderAppSecretByPrefix(dbauthz.AsSystemOAuth2(ctx), []byte(secret.Prefix))
if errors.Is(err, sql.ErrNoRows) {
return codersdk.OAuth2TokenResponse{}, errBadSecret
}
dbSecret, err := authenticateClient(ctx, db, app, req.ClientSecret)
if err != nil {
return codersdk.OAuth2TokenResponse{}, err
}

equalSecret := apikey.ValidateHash(dbSecret.HashedSecret, secret.Secret)
if !equalSecret {
return codersdk.OAuth2TokenResponse{}, errBadSecret
}

// The secret must belong to the app named by client_id, which arrives
// unverified in the request. Otherwise a valid secret for one app
// could issue a token for another.
if dbSecret.AppID != app.ID {
return codersdk.OAuth2TokenResponse{}, errBadSecret
}

appSecretID = uuid.NullUUID{UUID: dbSecret.ID, Valid: true}
}

Expand Down
103 changes: 103 additions & 0 deletions coderd/oauth2provider/tokens_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"cdr.dev/slog/v3/sloggers/slogjson"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)

// parseScopes parses a space-delimited scope string into a slice of scopes
Expand Down Expand Up @@ -914,6 +917,106 @@ func TestExtractTokenRequest_UnrecognizedParametersLogged(t *testing.T) {
}
}

// Every failure is errBadSecret so the caller cannot tell a malformed secret
// from a valid one for the wrong app (RFC 6749 §5.2). The OtherAppsSecret row
// covers the belongs-to-app check, the step a retyped copy of the check would
// be most likely to lose.
func TestAuthenticateClient(t *testing.T) {
Comment thread
BobbyHo marked this conversation as resolved.
t.Parallel()

db, _ := dbtestutil.NewDB(t)

seed := func() (database.OAuth2ProviderApp, database.OAuth2ProviderAppSecret, string) {
app := dbgen.OAuth2ProviderApp(t, db, database.OAuth2ProviderApp{})
secret, err := GenerateSecret()
require.NoError(t, err)
dbSecret := dbgen.OAuth2ProviderAppSecret(t, db, database.OAuth2ProviderAppSecret{
AppID: app.ID,
SecretPrefix: []byte(secret.Prefix),
HashedSecret: secret.Hashed,
})
return app, dbSecret, secret.Formatted
}
app, dbSecret, formatted := seed()
_, _, otherFormatted := seed()

unknown, err := GenerateSecret()
require.NoError(t, err)
parsed, err := ParseFormattedSecret(formatted)
require.NoError(t, err)

tests := []struct {
name string
secret string
want error
}{
{name: "Empty", secret: "", want: errBadSecret},
{name: "Malformed", secret: "not-a-secret", want: errBadSecret},
{name: "UnknownPrefix", secret: unknown.Formatted, want: errBadSecret},
{name: "WrongHash", secret: SecretIdentifier + "_" + parsed.Prefix + "_" + unknown.Secret, want: errBadSecret},
{name: "OtherAppsSecret", secret: otherFormatted, want: errBadSecret},
{name: "OwnSecret", secret: formatted},
}
Comment thread
BobbyHo marked this conversation as resolved.
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)

got, err := authenticateClient(ctx, db, app, test.secret)
if test.want != nil {
require.ErrorIs(t, err, test.want)
return
}
require.NoError(t, err)
require.Equal(t, dbSecret.ID, got.ID)
})
}
}

func TestMergeBasicClientAuth(t *testing.T) {
t.Parallel()

tests := []struct {
name string
basicUser string
basicPass string
bodyID string
bodySecret string
wantID string
wantSecret string
wantErr error
}{
{name: "NoHeader", bodyID: "id", bodySecret: "s", wantID: "id", wantSecret: "s"},
{name: "HeaderOnly", basicUser: "id", basicPass: "s", wantID: "id", wantSecret: "s"},
{name: "HeaderAndMatchingBody", basicUser: "id", basicPass: "s", bodyID: "id", bodySecret: "s", wantID: "id", wantSecret: "s"},
// A header with an empty username is treated as no header at all.
{name: "HeaderEmptyUser", basicPass: "pass", bodyID: "id", bodySecret: "s", wantID: "id", wantSecret: "s"},
// An empty Basic password is still a presented password, so a body
// secret beside it is a conflict rather than a fallback.
{name: "HeaderEmptyPasswordBodySecret", basicUser: "id", bodySecret: "s", wantErr: errConflictingClientAuth},
{name: "ConflictingID", basicUser: "id", basicPass: "s", bodyID: "other", wantErr: errConflictingClientAuth},
{name: "ConflictingSecret", basicUser: "id", basicPass: "s", bodySecret: "other", wantErr: errConflictingClientAuth},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

r := &http.Request{Header: http.Header{}}
if test.basicUser != "" || test.basicPass != "" {
r.SetBasicAuth(test.basicUser, test.basicPass)
}
id, secret, err := mergeBasicClientAuth(r, test.bodyID, test.bodySecret)
if test.wantErr != nil {
require.ErrorIs(t, err, test.wantErr)
return
}
require.NoError(t, err)
require.Equal(t, test.wantID, id)
require.Equal(t, test.wantSecret, secret)
})
}
}

// TestRefreshTokenGrant_Scopes tests that scopes can be requested during refresh
func TestRefreshTokenGrant_Scopes(t *testing.T) {
t.Parallel()
Expand Down
Loading