mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix: use expires_in
field for git device refresh (#8411)
This was causing git auth to never refresh after the token became expired after 8hrs.
This commit is contained in:
@ -6,11 +6,13 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/github"
|
"golang.org/x/oauth2/github"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
|
"github.com/coder/coder/coderd/database"
|
||||||
"github.com/coder/coder/codersdk"
|
"github.com/coder/coder/codersdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -138,7 +140,9 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.GitAuthDevi
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ExchangeDeviceCodeResponse struct {
|
type ExchangeDeviceCodeResponse struct {
|
||||||
*oauth2.Token
|
AccessToken string `json:"access_token"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
ExpiresIn int `json:"expires_in"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
ErrorDescription string `json:"error_description"`
|
ErrorDescription string `json:"error_description"`
|
||||||
}
|
}
|
||||||
@ -175,7 +179,11 @@ func (c *DeviceAuth) ExchangeDeviceCode(ctx context.Context, deviceCode string)
|
|||||||
if body.Error != "" {
|
if body.Error != "" {
|
||||||
return nil, xerrors.New(body.Error)
|
return nil, xerrors.New(body.Error)
|
||||||
}
|
}
|
||||||
return body.Token, nil
|
return &oauth2.Token{
|
||||||
|
AccessToken: body.AccessToken,
|
||||||
|
RefreshToken: body.RefreshToken,
|
||||||
|
Expiry: database.Now().Add(time.Duration(body.ExpiresIn) * time.Second),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DeviceAuth) formatDeviceTokenURL(deviceCode string) (string, error) {
|
func (c *DeviceAuth) formatDeviceTokenURL(deviceCode string) (string, error) {
|
||||||
|
@ -200,9 +200,7 @@ func TestGitAuthDevice(t *testing.T) {
|
|||||||
require.Equal(t, "authorization_pending", sdkErr.Detail)
|
require.Equal(t, "authorization_pending", sdkErr.Detail)
|
||||||
|
|
||||||
resp = gitauth.ExchangeDeviceCodeResponse{
|
resp = gitauth.ExchangeDeviceCodeResponse{
|
||||||
Token: &oauth2.Token{
|
|
||||||
AccessToken: "hey",
|
AccessToken: "hey",
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = client.GitAuthDeviceExchange(context.Background(), "test", codersdk.GitAuthDeviceExchange{
|
err = client.GitAuthDeviceExchange(context.Background(), "test", codersdk.GitAuthDeviceExchange{
|
||||||
|
Reference in New Issue
Block a user