Skip to content

Commit 380022f

Browse files
authored
fix: update oauth token on each login (#3542)
1 parent c3eea98 commit 380022f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

coderd/features_internal_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"net/http/httptest"
77
"testing"
88

9-
"github.com/coder/coder/codersdk"
109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
11+
12+
"github.com/coder/coder/codersdk"
1213
)
1314

1415
func TestEntitlements(t *testing.T) {

coderd/userauth.go

+34
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,23 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
241241
}
242242
}
243243

244+
if link.UserID != uuid.Nil {
245+
link, err = api.Database.UpdateUserLink(ctx, database.UpdateUserLinkParams{
246+
UserID: user.ID,
247+
LoginType: database.LoginTypeGithub,
248+
OAuthAccessToken: state.Token.AccessToken,
249+
OAuthRefreshToken: state.Token.RefreshToken,
250+
OAuthExpiry: state.Token.Expiry,
251+
})
252+
if err != nil {
253+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
254+
Message: "A database error occurred.",
255+
Detail: fmt.Sprintf("update user link: %s", err.Error()),
256+
})
257+
return
258+
}
259+
}
260+
244261
_, created := api.createAPIKey(rw, r, createAPIKeyParams{
245262
UserID: user.ID,
246263
LoginType: database.LoginTypeGithub,
@@ -432,6 +449,23 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
432449
}
433450
}
434451

452+
if link.UserID != uuid.Nil {
453+
link, err = api.Database.UpdateUserLink(ctx, database.UpdateUserLinkParams{
454+
UserID: user.ID,
455+
LoginType: database.LoginTypeOIDC,
456+
OAuthAccessToken: state.Token.AccessToken,
457+
OAuthRefreshToken: state.Token.RefreshToken,
458+
OAuthExpiry: state.Token.Expiry,
459+
})
460+
if err != nil {
461+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
462+
Message: "A database error occurred.",
463+
Detail: fmt.Sprintf("update user link: %s", err.Error()),
464+
})
465+
return
466+
}
467+
}
468+
435469
// If the upstream email or username has changed we should mirror
436470
// that in Coder. Many enterprises use a user's email/username as
437471
// security auditing fields so they need to stay synced.

0 commit comments

Comments
 (0)