diff --git a/api/client.go b/api/client.go index a45f65c..e4da693 100644 --- a/api/client.go +++ b/api/client.go @@ -37,7 +37,7 @@ type Client struct { metadataKeySettings MetadataKeySettings // Server Settings for password expiry - passwordExpirySettings PasswordExpirySettings + PasswordExpirySettings PasswordExpirySettings // used for solving MFA challenges. You can block this to for example wait for user input. // You shouden't run any unrelated API Calls while you are in this callback. @@ -249,10 +249,10 @@ func (c *Client) setPasswordExpirySettings(ctx context.Context, settings *Server } c.log("passwordExpirySettings: %+v", passwordExpirySettings) - c.passwordExpirySettings = *passwordExpirySettings + c.PasswordExpirySettings = *passwordExpirySettings } else { c.log("Server has password expiry plugin disabled or not installed.") - c.passwordExpirySettings = getDefaultPasswordExpirySettings() + c.PasswordExpirySettings = getDefaultPasswordExpirySettings() } return nil diff --git a/api/resources.go b/api/resources.go index 3523cb8..3910ed4 100644 --- a/api/resources.go +++ b/api/resources.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "time" ) // Resource is a Resource. @@ -87,10 +86,6 @@ func (c *Client) GetResources(ctx context.Context, opts *GetResourcesOptions) ([ // CreateResource Creates a new Passbolt Resource func (c *Client) CreateResource(ctx context.Context, resource Resource) (*Resource, error) { - if c.passwordExpirySettings.DefaultExpiryPeriod != 0 { - expiry := time.Now().Add(time.Hour * 24 * time.Duration(c.passwordExpirySettings.DefaultExpiryPeriod)) - resource.Expired = &Time{expiry} - } msg, err := c.DoCustomRequest(ctx, "POST", "/resources.json", "v2", resource, nil) if err != nil { return nil, err @@ -129,10 +124,6 @@ func (c *Client) UpdateResource(ctx context.Context, resourceID string, resource return nil, fmt.Errorf("Checking ID format: %w", err) } - if resource.Expired != nil && c.passwordExpirySettings.AutomaticUpdate { - expiry := time.Now().Add(time.Hour * 24 * time.Duration(c.passwordExpirySettings.DefaultExpiryPeriod)) - resource.Expired = &Time{expiry} - } msg, err := c.DoCustomRequest(ctx, "PUT", "/resources/"+resourceID+".json", "v2", resource, nil) if err != nil { return nil, err diff --git a/helper/resource_create.go b/helper/resource_create.go index b3bc70b..88fffbb 100644 --- a/helper/resource_create.go +++ b/helper/resource_create.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "time" "github.com/passbolt/go-passbolt/api" ) @@ -99,6 +100,11 @@ func CreateResourceV5(ctx context.Context, c *api.Client, folderParentID, name, } resource.Secrets = []api.Secret{{Data: encSecretData}} + if c.PasswordExpirySettings.DefaultExpiryPeriod != 0 { + expiry := time.Now().Add(time.Hour * 24 * time.Duration(c.PasswordExpirySettings.DefaultExpiryPeriod)) + resource.Expired = &api.Time{Time: expiry} + } + newresource, err := c.CreateResource(ctx, resource) if err != nil { return "", fmt.Errorf("Creating Resource: %w", err) @@ -154,6 +160,11 @@ func CreateResourceV4(ctx context.Context, c *api.Client, folderParentID, name, } resource.Secrets = []api.Secret{{Data: encSecretData}} + if c.PasswordExpirySettings.DefaultExpiryPeriod != 0 { + expiry := time.Now().Add(time.Hour * 24 * time.Duration(c.PasswordExpirySettings.DefaultExpiryPeriod)) + resource.Expired = &api.Time{Time: expiry} + } + newresource, err := c.CreateResource(ctx, resource) if err != nil { return "", fmt.Errorf("Creating Resource: %w", err) diff --git a/helper/resource_update.go b/helper/resource_update.go index a3d3e16..d91bb0f 100644 --- a/helper/resource_update.go +++ b/helper/resource_update.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "time" "github.com/ProtonMail/gopenpgp/v3/crypto" "github.com/passbolt/go-passbolt/api" @@ -376,6 +377,11 @@ func UpdateResource(ctx context.Context, c *api.Client, resourceID, name, userna }) } + if resource.Expired != nil && c.PasswordExpirySettings.AutomaticUpdate { + expiry := time.Now().Add(time.Hour * 24 * time.Duration(c.PasswordExpirySettings.DefaultExpiryPeriod)) + newResource.Expired = &api.Time{expiry} + } + _, err = c.UpdateResource(ctx, resourceID, newResource) if err != nil { return fmt.Errorf("Updating Resource: %w", err)