@@ -1012,30 +1012,35 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
1012
1012
return
1013
1013
}
1014
1014
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 ,
1029
1026
})
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 ))
1030
1038
return
1031
1039
}
1032
-
1033
- httpapi .Write (ctx , rw , http .StatusOK , formatGitAuthAccessToken (gitAuthConfig .Type , gitAuthLink .OAuthAccessToken ))
1034
- return
1035
1040
}
1036
1041
1037
1042
// 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 ))
1039
1044
if err != nil {
1040
1045
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
1041
1046
Message : "Failed to parse access URL." ,
@@ -1058,7 +1063,7 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
1058
1063
}
1059
1064
1060
1065
httpapi .Write (ctx , rw , http .StatusOK , codersdk.WorkspaceAgentGitAuthResponse {
1061
- URL : url .String (),
1066
+ URL : redirectURL .String (),
1062
1067
})
1063
1068
return
1064
1069
}
@@ -1070,7 +1075,7 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
1070
1075
}).Token ()
1071
1076
if err != nil {
1072
1077
httpapi .Write (ctx , rw , http .StatusOK , codersdk.WorkspaceAgentGitAuthResponse {
1073
- URL : url .String (),
1078
+ URL : redirectURL .String (),
1074
1079
})
1075
1080
return
1076
1081
}
0 commit comments