Skip to content

Commit b6bfdf1

Browse files
committed
Fix update loop for oauth token
1 parent f2c0983 commit b6bfdf1

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

coderd/workspaceagents.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -1012,30 +1012,35 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
10121012
return
10131013
}
10141014
defer cancelFunc()
1015-
select {
1016-
case <-r.Context().Done():
1017-
return
1018-
case <-authChan:
1019-
}
1020-
1021-
gitAuthLink, err := api.Database.GetGitAuthLink(ctx, database.GetGitAuthLinkParams{
1022-
ProviderID: gitAuthConfig.ID,
1023-
UserID: workspace.OwnerID,
1024-
})
1025-
if err != nil {
1026-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
1027-
Message: "Failed to get git auth link.",
1028-
Detail: err.Error(),
1015+
ticker := time.NewTicker(time.Second)
1016+
for {
1017+
select {
1018+
case <-r.Context().Done():
1019+
return
1020+
case <-ticker.C:
1021+
case <-authChan:
1022+
}
1023+
gitAuthLink, err := api.Database.GetGitAuthLink(ctx, database.GetGitAuthLinkParams{
1024+
ProviderID: gitAuthConfig.ID,
1025+
UserID: workspace.OwnerID,
10291026
})
1027+
if err != nil {
1028+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
1029+
Message: "Failed to get git auth link.",
1030+
Detail: err.Error(),
1031+
})
1032+
return
1033+
}
1034+
if gitAuthLink.OAuthExpiry.Before(database.Now()) {
1035+
continue
1036+
}
1037+
httpapi.Write(ctx, rw, http.StatusOK, formatGitAuthAccessToken(gitAuthConfig.Type, gitAuthLink.OAuthAccessToken))
10301038
return
10311039
}
1032-
1033-
httpapi.Write(ctx, rw, http.StatusOK, formatGitAuthAccessToken(gitAuthConfig.Type, gitAuthLink.OAuthAccessToken))
1034-
return
10351040
}
10361041

10371042
// This is the URL that will redirect the user with a state token.
1038-
url, err := api.AccessURL.Parse(fmt.Sprintf("/gitauth/%s", gitAuthConfig.ID))
1043+
redirectURL, err := api.AccessURL.Parse(fmt.Sprintf("/gitauth/%s", gitAuthConfig.ID))
10391044
if err != nil {
10401045
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
10411046
Message: "Failed to parse access URL.",
@@ -1058,7 +1063,7 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
10581063
}
10591064

10601065
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentGitAuthResponse{
1061-
URL: url.String(),
1066+
URL: redirectURL.String(),
10621067
})
10631068
return
10641069
}
@@ -1070,7 +1075,7 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
10701075
}).Token()
10711076
if err != nil {
10721077
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentGitAuthResponse{
1073-
URL: url.String(),
1078+
URL: redirectURL.String(),
10741079
})
10751080
return
10761081
}

0 commit comments

Comments
 (0)