From dcd96618b9e9aa3fe76a772536e014f89fe3234e Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Tue, 18 Jun 2024 11:24:38 -0700 Subject: [PATCH] Can fetch a token based on the device code --- internal/login/login.go | 30 +++++++++++++++++++++++++++ internal/login/login_test.go | 39 +++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/internal/login/login.go b/internal/login/login.go index 027f304d..4b135411 100644 --- a/internal/login/login.go +++ b/internal/login/login.go @@ -20,6 +20,10 @@ type DeviceAuthorization struct { VerificationURI string `json:"verificationUri"` } +type DeviceAuthorizationToken struct { + AccessToken string `json:"accessToken"` +} + type UnauthenticatedClient interface { MakeRequest( method string, @@ -96,6 +100,32 @@ func FetchDeviceAuthorization( return deviceAuthorization, nil } +func FetchToken( + client UnauthenticatedClient, + deviceCode string, + baseURI string, +) (DeviceAuthorizationToken, error) { + path := fmt.Sprintf("%s/internal/device-authorization/token", baseURI) + body := fmt.Sprintf( + `{ + "deviceCode": %q + }`, + deviceCode, + ) + res, err := client.MakeRequest("POST", path, []byte(body)) + if err != nil { + return DeviceAuthorizationToken{}, err + } + + var deviceAuthorizationToken DeviceAuthorizationToken + err = json.Unmarshal(res, &deviceAuthorizationToken) + if err != nil { + return DeviceAuthorizationToken{}, err + } + + return deviceAuthorizationToken, nil +} + func GetDeviceName() string { deviceName, err := os.Hostname() if err != nil { diff --git a/internal/login/login_test.go b/internal/login/login_test.go index b1da44ca..a258453a 100644 --- a/internal/login/login_test.go +++ b/internal/login/login_test.go @@ -22,7 +22,7 @@ func (c *mockClient) MakeRequest( ) ([]byte, error) { args := c.Called(method, path, data) - return data, args.Error(1) + return args.Get(0).([]byte), args.Error(1) } func TestFetchDeviceAuthorization(t *testing.T) { @@ -38,11 +38,17 @@ func TestFetchDeviceAuthorization(t *testing.T) { }`), ).Return([]byte(`{ "deviceCode": "test-device-code", - "expiresIn": 0000000001, + "expiresIn": 1, "userCode": "0001", "verificationUri": "/confirm-auth/test-device-code" }`), nil) - expected := login.DeviceAuthorization{} + expected := login.DeviceAuthorization{ + DeviceCode: "test-device-code", + ExpiresIn: 1, + UserCode: "0001", + VerificationURI: "/confirm-auth/test-device-code", + } + result, err := login.FetchDeviceAuthorization( &mockClient, "test-client-id", @@ -53,3 +59,30 @@ func TestFetchDeviceAuthorization(t *testing.T) { require.NoError(t, err) assert.Equal(t, expected, result) } + +func TestFetchToken(t *testing.T) { + baseURI := "http://test.com" + mockClient := mockClient{} + mockClient.On( + "MakeRequest", + "POST", + "http://test.com/internal/device-authorization/token", + []byte(`{ + "deviceCode": "test-device-code" + }`), + ).Return([]byte(`{ + "accessToken": "test-access-token" + }`), nil) + expected := login.DeviceAuthorizationToken{ + AccessToken: "test-access-token", + } + + result, err := login.FetchToken( + &mockClient, + "test-device-code", + baseURI, + ) + + require.NoError(t, err) + assert.Equal(t, expected, result) +}