Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Can fetch a token based on the device code #323

Merged
merged 1 commit into from
Jun 18, 2024
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return the Return part of the mock, not the method's input.

}

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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an int.

"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)
}
Loading