diff --git a/pkg/v2/token/request.go b/pkg/v2/token/request.go index 7b09854..e94ee13 100644 --- a/pkg/v2/token/request.go +++ b/pkg/v2/token/request.go @@ -81,7 +81,6 @@ func GetByID(ctx context.Context, client *client.ServiceClient, tokenID string) if err != nil { return nil, responseResult, err } - token.Token = tokenID return &token, responseResult, nil } @@ -101,7 +100,7 @@ func Revoke(ctx context.Context, client *client.ServiceClient, tokenID string) ( } // Refresh refresh a token by its ID. -func Refresh(ctx context.Context, client *client.ServiceClient, tokenID string, exp Exp) (*TokenV2, *svc.ResponseResult, error) { +func Refresh(ctx context.Context, client *client.ServiceClient, tokenID string, exp Expiration) (*TokenV2, *svc.ResponseResult, error) { url := strings.Join([]string{client.Endpoint(), v2.ResourceURLToken, tokenID, v2.ResourceURLRefresh}, "/") reqBody, err := json.Marshal(exp) if err != nil { @@ -125,7 +124,7 @@ func Refresh(ctx context.Context, client *client.ServiceClient, tokenID string, } // Regenerate regenerate a token by its ID. -func Regenerate(ctx context.Context, client *client.ServiceClient, tokenID string, exp Exp) (*TokenV2, *svc.ResponseResult, error) { +func Regenerate(ctx context.Context, client *client.ServiceClient, tokenID string, exp Expiration) (*TokenV2, *svc.ResponseResult, error) { url := strings.Join([]string{client.Endpoint(), v2.ResourceURLToken, tokenID, v2.ResourceURLRegenerate}, "/") reqBody, err := json.Marshal(exp) if err != nil { @@ -163,12 +162,13 @@ func Delete(ctx context.Context, client *client.ServiceClient, tokenID string) ( } // Patch patch a token by its ID. -func Patch(ctx context.Context, client *client.ServiceClient, tokenID string, name string, sc Scope) (*TokenV2, *svc.ResponseResult, error) { +func Patch(ctx context.Context, client *client.ServiceClient, tokenID string, name string, sc Scope, exp Expiration) (*TokenV2, *svc.ResponseResult, error) { var token TokenV2 if name != "" { token.Name = name } token.Scope = sc + token.Expiration = exp reqBody, err := json.Marshal(token) if err != nil { return nil, nil, err @@ -188,6 +188,9 @@ func Patch(ctx context.Context, client *client.ServiceClient, tokenID string, na if err != nil { return nil, responseResult, err } + if responseResult.Err != nil { + return nil, responseResult, responseResult.Err + } return &tokenResult, responseResult, nil } diff --git a/pkg/v2/token/schemas.go b/pkg/v2/token/schemas.go index 16d60ec..c72dd47 100644 --- a/pkg/v2/token/schemas.go +++ b/pkg/v2/token/schemas.go @@ -3,14 +3,14 @@ package tokenv2 import "time" type TokenV2 struct { - ID string `json:"id,omitempty"` - Name string `json:"name,omitempty"` - CreatedAt time.Time `json:"createdAt,omitempty"` - Expiration Exp `json:"expiration"` - Scope Scope `json:"scope"` - Status string `json:"status,omitempty"` - Token string `json:"token,omitempty"` - LastUsedAt time.Time `json:"lastUsedAt,omitempty"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + CreatedAt *time.Time `json:"createdAt,omitempty"` + Expiration Expiration `json:"expiration"` + Scope Scope `json:"scope"` + Status string `json:"status,omitempty"` + Token string `json:"token,omitempty"` + LastUsedAt *time.Time `json:"lastUsedAt,omitempty"` } type Scope struct { @@ -19,7 +19,7 @@ type Scope struct { RegistryIDs []string `json:"registryIds,omitempty"` } -type Exp struct { +type Expiration struct { IsSet bool `json:"isSet"` ExpiresAt time.Time `json:"expiresAt,omitempty"` } diff --git a/pkg/v2/token/testing/fixtures.go b/pkg/v2/token/testing/fixtures.go index fa37cef..2ebf413 100644 --- a/pkg/v2/token/testing/fixtures.go +++ b/pkg/v2/token/testing/fixtures.go @@ -100,7 +100,7 @@ var createdAt, _ = time.Parse("2006-01-02T15:04:05Z", "2023-02-13T15:00:00Z") var expiresAt, _ = time.Parse("2006-01-02T15:04:05Z", "2030-01-01T00:00:00Z") var lastUsedAt, _ = time.Parse("2006-01-02T15:04:05Z", "2023-02-14T15:25:10Z") -var Exp = tokenV2.Exp{ +var Exp = tokenV2.Expiration{ IsSet: true, ExpiresAt: expiresAt, } @@ -119,7 +119,7 @@ var expectedCreateTokenResponse = &tokenV2.TokenV2{ Status: "active", ID: "c29e3f63-0711-4772-a415-ad79973bdaef", Name: "my-token", - CreatedAt: createdAt, + CreatedAt: &createdAt, Expiration: Exp, Scope: Scope, } @@ -127,10 +127,10 @@ var expectedCreateTokenResponse = &tokenV2.TokenV2{ var expectedPatchTokenResponse = &tokenV2.TokenV2{ ID: "c29e3f63-0711-4772-a415-ad79973bdaef", Name: "token", - CreatedAt: createdAt, + CreatedAt: &createdAt, Expiration: Exp, Scope: Scope, - LastUsedAt: lastUsedAt, + LastUsedAt: &lastUsedAt, Status: "active", } @@ -139,10 +139,10 @@ var expectedListTokenResponse = &tokenV2.TokensV2{ Tokens: []tokenV2.TokenV2{ { Status: "active", - LastUsedAt: lastUsedAt, + LastUsedAt: &lastUsedAt, ID: "c29e3f63-0711-4772-a415-ad79973bdaef", Name: "my-token", - CreatedAt: createdAt, + CreatedAt: &createdAt, Expiration: struct { IsSet bool `json:"isSet"` ExpiresAt time.Time `json:"expiresAt,omitempty"` @@ -165,10 +165,10 @@ var expectedListTokenResponse = &tokenV2.TokensV2{ }, { Status: "active", - LastUsedAt: lastUsedAt, + LastUsedAt: &lastUsedAt, ID: "c29e3f63-0711-4772-a415-ad79973bdaef", Name: "my-token", - CreatedAt: createdAt, + CreatedAt: &createdAt, Expiration: struct { IsSet bool `json:"isSet"` ExpiresAt time.Time `json:"expiresAt,omitempty"` diff --git a/pkg/v2/token/testing/requests_test.go b/pkg/v2/token/testing/requests_test.go index 9f68e37..53146fe 100644 --- a/pkg/v2/token/testing/requests_test.go +++ b/pkg/v2/token/testing/requests_test.go @@ -143,7 +143,10 @@ func TestPatchToken(t *testing.T) { RegistryIDs: []string{"888af692-c646-4b76-a234-81ca9b5bcafe", "6303699d-c2cd-40b1-8428-9dcd6cc3d00d"}, AllRegistries: false, } - actual, response, err := tokenV2.Patch(ctx, testClient, testTokenID, "token", scope) + exp := tokenV2.Expiration{ + ExpiresAt: expiresAt, + } + actual, response, err := tokenV2.Patch(ctx, testClient, testTokenID, "token", scope, exp) if err != nil { t.Fatal(err) }