Skip to content

Commit

Permalink
feat: Can fetch a token based on the device code (#323)
Browse files Browse the repository at this point in the history
Can fetch a token based on the device code
  • Loading branch information
dbolson authored Jun 18, 2024
1 parent edb3d2d commit 769b925
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
30 changes: 30 additions & 0 deletions internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type DeviceAuthorization struct {
VerificationURI string `json:"verificationUri"`
}

type DeviceAuthorizationToken struct {
AccessToken string `json:"accessToken"`
}

type UnauthenticatedClient interface {
MakeRequest(
method string,
Expand Down Expand Up @@ -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 {
Expand Down
39 changes: 36 additions & 3 deletions internal/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand All @@ -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)
}

0 comments on commit 769b925

Please sign in to comment.