-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
DeviceCredentialsManager
to manage device credentials (#369)
- Loading branch information
Showing
8 changed files
with
628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package management | ||
|
||
import "context" | ||
|
||
// DeviceCredential is a device credential. | ||
type DeviceCredential struct { | ||
// ID of this device credential | ||
ID *string `json:"id,omitempty"` | ||
|
||
// The id of the client. | ||
ClientID *string `json:"client_id,omitempty"` | ||
|
||
// The id of the user. | ||
UserID *string `json:"user_id,omitempty"` | ||
|
||
// User agent for this device | ||
DeviceName *string `json:"device_name,omitempty"` | ||
|
||
// Unique identifier for the device. NOTE: This field is generally not populated for refresh_tokens and rotating_refresh_tokens | ||
DeviceID *string `json:"device_id,omitempty"` | ||
|
||
// Type of credential. Can be public_key, refresh_token, or rotating_refresh_token | ||
Type *string `json:"type,omitempty"` | ||
|
||
// Base64 encoded string containing the credential | ||
Value *string `json:"value,omitempty"` | ||
} | ||
|
||
// DeviceCredentialList is a list of DeviceCredentials. | ||
type DeviceCredentialList struct { | ||
List | ||
DeviceCredentials []*DeviceCredential `json:"device_credentials"` | ||
} | ||
|
||
// DeviceCredentialsManager manages Auth0 device-credentials resources. | ||
type DeviceCredentialsManager manager | ||
|
||
// Create a device credential public key to manage refresh token rotation for a given user_id | ||
// Type of credential must be "public_key". | ||
// | ||
// See: https://auth0.com/docs/api/management/v2/device-credentials/post-device-credentials | ||
func (m *DeviceCredentialsManager) Create(ctx context.Context, d *DeviceCredential, opts ...RequestOption) error { | ||
return m.management.Request(ctx, "POST", m.management.URI("device-credentials"), d, opts...) | ||
} | ||
|
||
// List device credential information (public_key, refresh_token, or rotating_refresh_token) associated with a specific user. | ||
// | ||
// For information on how to paginate using this function see https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination | ||
// | ||
// See: https://auth0.com/docs/api/management/v2/device-credentials/get-device-credentials | ||
func (m *DeviceCredentialsManager) List(ctx context.Context, opts ...RequestOption) (d *DeviceCredentialList, err error) { | ||
err = m.management.Request(ctx, "GET", m.management.URI("device-credentials"), &d, applyListDefaults(opts)) | ||
return | ||
} | ||
|
||
// Delete a device credential (such as a refresh token or public key) with the given ID. | ||
// | ||
// See: https://auth0.com/docs/api/management/v2/device-credentials/delete-device-credentials-by-id | ||
func (m *DeviceCredentialsManager) Delete(ctx context.Context, id string, opts ...RequestOption) error { | ||
return m.management.Request(ctx, "DELETE", m.management.URI("device-credentials", id), nil, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package management | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/auth0/go-auth0" | ||
) | ||
|
||
func TestDeviceCredentials_Create(t *testing.T) { | ||
configureHTTPTestRecordings(t) | ||
|
||
expectedDeviceCredential := &DeviceCredential{ | ||
DeviceName: auth0.String("TestDevice"), | ||
Type: auth0.String("public_key"), | ||
Value: auth0.String("ABCD"), | ||
DeviceID: auth0.String("test_device"), | ||
ClientID: auth0.String("test_client"), | ||
} | ||
|
||
err := api.DeviceCredentials.Create(context.Background(), expectedDeviceCredential) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, expectedDeviceCredential.GetID()) | ||
|
||
t.Cleanup(func() { | ||
cleanupDeviceCredential(t, expectedDeviceCredential.GetID()) | ||
}) | ||
} | ||
|
||
func TestDeviceCredentials_List(t *testing.T) { | ||
configureHTTPTestRecordings(t) | ||
|
||
expectedDeviceCredential := givenADeviceCredential(t) | ||
|
||
deviceCredentialList, err := api.DeviceCredentials.List(context.Background(), IncludeFields("id")) | ||
|
||
assert.NoError(t, err) | ||
assert.Contains(t, deviceCredentialList.DeviceCredentials, &DeviceCredential{ID: expectedDeviceCredential.ID}) | ||
} | ||
|
||
func TestDeviceCredentials_Delete(t *testing.T) { | ||
configureHTTPTestRecordings(t) | ||
|
||
expectedDeviceCredential := givenADeviceCredential(t) | ||
|
||
err := api.DeviceCredentials.Delete(context.Background(), expectedDeviceCredential.GetID()) | ||
assert.NoError(t, err) | ||
|
||
actualDeviceCredentials, err := api.DeviceCredentials.List(context.Background()) | ||
assert.NoError(t, err) | ||
assert.Empty(t, actualDeviceCredentials.DeviceCredentials) | ||
} | ||
|
||
func givenADeviceCredential(t *testing.T) *DeviceCredential { | ||
t.Helper() | ||
|
||
deviceCredential := &DeviceCredential{ | ||
DeviceName: auth0.String("TestDevice"), | ||
Type: auth0.String("refresh_token"), | ||
Value: auth0.String("ABCD"), | ||
DeviceID: auth0.String("test_device"), | ||
ClientID: auth0.String("test_client"), | ||
} | ||
err := api.DeviceCredentials.Create(context.Background(), deviceCredential) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
cleanupDeviceCredential(t, deviceCredential.GetID()) | ||
}) | ||
|
||
return deviceCredential | ||
} | ||
|
||
func cleanupDeviceCredential(t *testing.T, id string) { | ||
t.Helper() | ||
|
||
err := api.DeviceCredentials.Delete(context.Background(), id) | ||
require.NoError(t, err) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.