Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions coderd/gitauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"net/http"
"net/url"
"regexp"
"time"

"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"
)

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

type ExchangeDeviceCodeResponse struct {
*oauth2.Token
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
Expand Down Expand Up @@ -175,7 +179,11 @@ func (c *DeviceAuth) ExchangeDeviceCode(ctx context.Context, deviceCode string)
if 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) {
Expand Down
4 changes: 1 addition & 3 deletions coderd/gitauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ func TestGitAuthDevice(t *testing.T) {
require.Equal(t, "authorization_pending", sdkErr.Detail)

resp = gitauth.ExchangeDeviceCodeResponse{
Token: &oauth2.Token{
AccessToken: "hey",
},
AccessToken: "hey",
}

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