Skip to content

Commit 8c5579a

Browse files
committed
fix: use expires_in field for git device refresh
This was causing git auth to never refresh after the token became expired after 8hrs.
1 parent d896b74 commit 8c5579a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

coderd/gitauth/oauth.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"net/http"
77
"net/url"
88
"regexp"
9+
"time"
910

1011
"golang.org/x/oauth2"
1112
"golang.org/x/oauth2/github"
1213
"golang.org/x/xerrors"
1314

15+
"github.com/coder/coder/coderd/database"
1416
"github.com/coder/coder/codersdk"
1517
)
1618

@@ -138,7 +140,9 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.GitAuthDevi
138140
}
139141

140142
type ExchangeDeviceCodeResponse struct {
141-
*oauth2.Token
143+
AccessToken string `json:"access_token"`
144+
RefreshToken string `json:"refresh_token"`
145+
ExpiresIn int `json:"expires_in"`
142146
Error string `json:"error"`
143147
ErrorDescription string `json:"error_description"`
144148
}
@@ -175,7 +179,11 @@ func (c *DeviceAuth) ExchangeDeviceCode(ctx context.Context, deviceCode string)
175179
if body.Error != "" {
176180
return nil, xerrors.New(body.Error)
177181
}
178-
return body.Token, nil
182+
return &oauth2.Token{
183+
AccessToken: body.AccessToken,
184+
RefreshToken: body.RefreshToken,
185+
Expiry: database.Now().Add(time.Duration(body.ExpiresIn) * time.Second),
186+
}, nil
179187
}
180188

181189
func (c *DeviceAuth) formatDeviceTokenURL(deviceCode string) (string, error) {

coderd/gitauth_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ func TestGitAuthDevice(t *testing.T) {
200200
require.Equal(t, "authorization_pending", sdkErr.Detail)
201201

202202
resp = gitauth.ExchangeDeviceCodeResponse{
203-
Token: &oauth2.Token{
204-
AccessToken: "hey",
205-
},
203+
AccessToken: "hey",
206204
}
207205

208206
err = client.GitAuthDeviceExchange(context.Background(), "test", codersdk.GitAuthDeviceExchange{

0 commit comments

Comments
 (0)