Skip to content
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
11 changes: 7 additions & 4 deletions pkg/v2/token/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
18 changes: 9 additions & 9 deletions pkg/v2/token/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/v2/token/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -119,18 +119,18 @@ var expectedCreateTokenResponse = &tokenV2.TokenV2{
Status: "active",
ID: "c29e3f63-0711-4772-a415-ad79973bdaef",
Name: "my-token",
CreatedAt: createdAt,
CreatedAt: &createdAt,
Expiration: Exp,
Scope: Scope,
}

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",
}

Expand All @@ -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"`
Expand All @@ -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"`
Expand Down
5 changes: 4 additions & 1 deletion pkg/v2/token/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading