Skip to content

Commit 863c2e7

Browse files
authored
feat: allow storing extra oauth token properties in the database (#10152)
1 parent 35538e1 commit 863c2e7

File tree

25 files changed

+223
-60
lines changed

25 files changed

+223
-60
lines changed

cli/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,8 @@ func parseExternalAuthProvidersFromEnv(prefix string, environ []string) ([]coder
22512251
provider.NoRefresh = b
22522252
case "SCOPES":
22532253
provider.Scopes = strings.Split(v.Value, " ")
2254+
case "EXTRA_TOKEN_KEYS":
2255+
provider.ExtraTokenKeys = strings.Split(v.Value, " ")
22542256
case "APP_INSTALL_URL":
22552257
provider.AppInstallURL = v.Value
22562258
case "APP_INSTALLATIONS_URL":

coderd/apidoc/docs.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderdtest/oidctest/idp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type FakeIDP struct {
6868
// "Authorized Redirect URLs". This can be used to emulate that.
6969
hookValidRedirectURL func(redirectURL string) error
7070
hookUserInfo func(email string) (jwt.MapClaims, error)
71+
hookMutateToken func(token map[string]interface{})
7172
fakeCoderd func(req *http.Request) (*http.Response, error)
7273
hookOnRefresh func(email string) error
7374
// Custom authentication for the client. This is useful if you want
@@ -112,6 +113,14 @@ func WithRefresh(hook func(email string) error) func(*FakeIDP) {
112113
}
113114
}
114115

116+
// WithExtra returns extra fields that be accessed on the returned Oauth Token.
117+
// These extra fields can override the default fields (id_token, access_token, etc).
118+
func WithMutateToken(mutateToken func(token map[string]interface{})) func(*FakeIDP) {
119+
return func(f *FakeIDP) {
120+
f.hookMutateToken = mutateToken
121+
}
122+
}
123+
115124
func WithCustomClientAuth(hook func(t testing.TB, req *http.Request) (url.Values, error)) func(*FakeIDP) {
116125
return func(f *FakeIDP) {
117126
f.hookAuthenticateClient = hook
@@ -621,6 +630,9 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
621630
"expires_in": int64((time.Minute * 5).Seconds()),
622631
"id_token": f.encodeClaims(t, claims),
623632
}
633+
if f.hookMutateToken != nil {
634+
f.hookMutateToken(token)
635+
}
624636
// Store the claims for the next refresh
625637
f.refreshIDTokenClaims.Store(refreshToken, claims)
626638

coderd/database/dbfake/dbfake.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,6 +4246,7 @@ func (q *FakeQuerier) InsertExternalAuthLink(_ context.Context, arg database.Ins
42464246
OAuthRefreshToken: arg.OAuthRefreshToken,
42474247
OAuthRefreshTokenKeyID: arg.OAuthRefreshTokenKeyID,
42484248
OAuthExpiry: arg.OAuthExpiry,
4249+
OAuthExtra: arg.OAuthExtra,
42494250
}
42504251
q.externalAuthLinks = append(q.externalAuthLinks, gitAuthLink)
42514252
return gitAuthLink, nil
@@ -5301,6 +5302,7 @@ func (q *FakeQuerier) UpdateExternalAuthLink(_ context.Context, arg database.Upd
53015302
gitAuthLink.OAuthRefreshToken = arg.OAuthRefreshToken
53025303
gitAuthLink.OAuthRefreshTokenKeyID = arg.OAuthRefreshTokenKeyID
53035304
gitAuthLink.OAuthExpiry = arg.OAuthExpiry
5305+
gitAuthLink.OAuthExtra = arg.OAuthExtra
53045306
q.externalAuthLinks[index] = gitAuthLink
53055307

53065308
return gitAuthLink, nil

coderd/database/dbgen/dbgen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ func UserLink(t testing.TB, db database.Store, orig database.UserLink) database.
514514
}
515515

516516
func ExternalAuthLink(t testing.TB, db database.Store, orig database.ExternalAuthLink) database.ExternalAuthLink {
517+
msg := takeFirst(&orig.OAuthExtra, &pqtype.NullRawMessage{})
517518
link, err := db.InsertExternalAuthLink(genCtx, database.InsertExternalAuthLinkParams{
518519
ProviderID: takeFirst(orig.ProviderID, uuid.New().String()),
519520
UserID: takeFirst(orig.UserID, uuid.New()),
@@ -524,6 +525,7 @@ func ExternalAuthLink(t testing.TB, db database.Store, orig database.ExternalAut
524525
OAuthExpiry: takeFirst(orig.OAuthExpiry, dbtime.Now().Add(time.Hour*24)),
525526
CreatedAt: takeFirst(orig.CreatedAt, dbtime.Now()),
526527
UpdatedAt: takeFirst(orig.UpdatedAt, dbtime.Now()),
528+
OAuthExtra: *msg,
527529
})
528530

529531
require.NoError(t, err, "insert external auth link")

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE external_auth_links DROP COLUMN "oauth_extra";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE external_auth_links ADD COLUMN "oauth_extra" jsonb;

coderd/database/models.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)