Skip to content

Commit

Permalink
[CAP-5226] Handle API updates to make device flow RFC compliant (#132)
Browse files Browse the repository at this point in the history
* Match RFC compliance updates

* Change to URL encoded
  • Loading branch information
mdbenjam authored Nov 21, 2024
1 parent fc1ba6a commit 3be9b13
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
30 changes: 19 additions & 11 deletions pkg/client/devicegrant/devicegrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ const authorizationPendingAPIMsg = "authorization_pending"
var ErrAuthorizationPending = errors.New("authorization pending")

type DeviceGrant struct {
DeviceCode string `json:"deviceCode"`
UserCode string `json:"userCode"`
VerificationUri string `json:"verificationUri"`
ExpiresIn int `json:"expiresIn"`
Interval int `json:"interval"`
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationUri string `json:"verification_uri"`
VerificationUriComplete string `json:"verification_uri_complete"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}

type GrantRequestBody struct {
ClientID string `json:"clientId"`
ClientID string `json:"client_id"`
}

type DeviceToken struct {
DeviceToken string `json:"deviceToken"`
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}

type TokenRequestBody struct {
ClientID string `json:"clientId"`
DeviceCode string `json:"deviceCode"`
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
DeviceCode string `json:"device_code"`
}

type ErrorResponse struct {
Expand Down Expand Up @@ -73,7 +77,10 @@ func (c *Client) CreateGrant(ctx context.Context) (*DeviceGrant, error) {
}

func (c *Client) GetDeviceToken(ctx context.Context, dg *DeviceGrant) (string, error) {
body := &TokenRequestBody{ClientID: cliOauthClientID, DeviceCode: dg.DeviceCode}
body := &TokenRequestBody{
ClientID: cliOauthClientID, DeviceCode: dg.DeviceCode,
GrantType: "urn:ietf:params:oauth:grant-type:device_code",
}

var token DeviceToken
err := c.postFor(ctx, "/device-token", body, &token)
Expand All @@ -85,7 +92,7 @@ func (c *Client) GetDeviceToken(ctx context.Context, dg *DeviceGrant) (string, e
return "", err
}

return token.DeviceToken, nil
return token.AccessToken, nil
}

func (c *Client) postFor(ctx context.Context, path string, body any, v any) error {
Expand All @@ -101,6 +108,7 @@ func (c *Client) postFor(ctx context.Context, path string, body any, v any) erro
}

req = req.WithContext(ctx)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.Do(req)
if err != nil {
return err
Expand Down
22 changes: 12 additions & 10 deletions pkg/client/devicegrant/devicegrant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func TestClient_CreateGrant(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, &devicegrant.DeviceGrant{
DeviceCode: "some device code",
UserCode: "some user code",
VerificationUri: "some verification uri",
ExpiresIn: 1,
Interval: 2,
DeviceCode: "some device code",
UserCode: "some user code",
VerificationUri: "some verification uri",
VerificationUriComplete: "some complete verification uri",
ExpiresIn: 1,
Interval: 2,
}, dg)
}

Expand Down Expand Up @@ -79,11 +80,12 @@ func TestClient_GetDeviceToken(t *testing.T) {
}

const deviceGrantResp = `{
"deviceCode": "some device code",
"userCode": "some user code",
"verificationUri": "some verification uri",
"expiresIn": 1,
"device_code": "some device code",
"user_code": "some user code",
"verification_uri": "some verification uri",
"verification_uri_complete": "some complete verification uri",
"expires_in": 1,
"interval": 2
}`

const deviceTokenResp = `{"deviceToken": "some device token"}`
const deviceTokenResp = `{"access_token": "some device token"}`
2 changes: 1 addition & 1 deletion pkg/tui/views/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func isAlreadyLoggedIn(ctx context.Context) bool {
}

func dashboardAuthURL(dg *devicegrant.DeviceGrant) (*url.URL, error) {
u, err := url.Parse(dg.VerificationUri)
u, err := url.Parse(dg.VerificationUriComplete)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3be9b13

Please sign in to comment.