Skip to content

Commit 7d6d1c6

Browse files
authored
Merge pull request #53 from Nelwhix/#51-handle-password-expiry
V5 Password Expiry
2 parents ffcbf94 + fb0ce58 commit 7d6d1c6

File tree

6 files changed

+111
-7
lines changed

6 files changed

+111
-7
lines changed

api/auth.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,22 @@ func (c *Client) Login(ctx context.Context) error {
102102

103103
c.userID = user.ID
104104

105+
settings, err := c.GetServerSettings(ctx)
106+
if err != nil {
107+
return fmt.Errorf("Getting Server Settings: %w", err)
108+
}
109+
105110
// after Login, fetch MetadataTypeSettings to finish the Client Setup
106-
c.setMetadataTypeSettings(ctx)
111+
err = c.setMetadataTypeSettings(ctx, settings)
107112
if err != nil {
108113
return fmt.Errorf("Setup Metadata Type Settings: %w", err)
109114
}
110115

116+
err = c.setPasswordExpirySettings(ctx, settings)
117+
if err != nil {
118+
return fmt.Errorf("Setup Password Expiry Settings: %w", err)
119+
}
120+
111121
return nil
112122
}
113123

api/client.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Client struct {
3636
// Server Settings Determining which Metadata Keys to use
3737
metadataKeySettings MetadataKeySettings
3838

39+
// Server Settings for password expiry
40+
passwordExpirySettings PasswordExpirySettings
41+
3942
// used for solving MFA challenges. You can block this to for example wait for user input.
4043
// You shouden't run any unrelated API Calls while you are in this callback.
4144
// You need to Return the Cookie that Passbolt expects to verify you MFA, usually it is called passbolt_mfa
@@ -207,12 +210,7 @@ func (c *Client) GetPublicKey(ctx context.Context) (string, string, error) {
207210
}
208211

209212
// setMetadataTypeSettings Gets and configures the Client to use the Types the Server wants us to use
210-
func (c *Client) setMetadataTypeSettings(ctx context.Context) error {
211-
settings, err := c.GetServerSettings(ctx)
212-
if err != nil {
213-
return fmt.Errorf("Getting Server Settings: %w", err)
214-
}
215-
213+
func (c *Client) setMetadataTypeSettings(ctx context.Context, settings *ServerSettingsResponse) error {
216214
if settings.Passbolt.IsPluginEnabled("metadata") {
217215
c.log("Server has metadata plugin enabled, is v5 or Higher")
218216
metadataTypeSettings, err := c.GetServerMetadataTypeSettings(ctx)
@@ -241,7 +239,31 @@ func (c *Client) setMetadataTypeSettings(ctx context.Context) error {
241239
return nil
242240
}
243241

242+
// setPasswordExpirySettings fetches and configures the Client to use the password expiry plugin
243+
func (c *Client) setPasswordExpirySettings(ctx context.Context, settings *ServerSettingsResponse) error {
244+
if settings.Passbolt.IsPluginEnabled("passwordExpiry") && settings.Passbolt.IsPluginEnabled("passwordExpiryPolicies") {
245+
c.log("Server has password expiry plugin enabled.")
246+
passwordExpirySettings, err := c.getServerPasswordExpirySettings(ctx)
247+
if err != nil {
248+
return fmt.Errorf("Getting Password Expiry Settings: %w", err)
249+
}
250+
251+
c.log("passwordExpirySettings: %+v", passwordExpirySettings)
252+
c.passwordExpirySettings = *passwordExpirySettings
253+
} else {
254+
c.log("Server has password expiry plugin disabled or not installed.")
255+
c.passwordExpirySettings = getDefaultPasswordExpirySettings()
256+
}
257+
258+
return nil
259+
}
260+
244261
// GetPGPHandle Gets the Gopgenpgp Handler
245262
func (c *Client) GetPGPHandle() *crypto.PGPHandle {
246263
return c.pgp
247264
}
265+
266+
// GetPasswordExpirySettings returns the password expiry settings for the client
267+
func (c *Client) GetPasswordExpirySettings() PasswordExpirySettings {
268+
return c.passwordExpirySettings
269+
}

api/password_expiry.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"time"
7+
)
8+
9+
// PasswordExpirySettings contains the Password expiry settings
10+
type PasswordExpirySettings struct {
11+
ID string `json:"id"`
12+
DefaultExpiryPeriod int `json:"default_expiry_period,omitempty"`
13+
PolicyOverride bool `json:"policy_override"`
14+
AutomaticExpiry bool `json:"automatic_expiry"`
15+
AutomaticUpdate bool `json:"automatic_update"`
16+
ExpiryNotificationPeriod int `json:"expiry_notification_period,omitempty"`
17+
Created time.Time `json:"created"`
18+
Modified time.Time `json:"modified"`
19+
CreatedBy string `json:"created_by"`
20+
ModifiedBy string `json:"modified_by"`
21+
}
22+
23+
// getServerPasswordExpirySettings gets the servers password expiry settings
24+
func (c *Client) getServerPasswordExpirySettings(ctx context.Context) (*PasswordExpirySettings, error) {
25+
msg, err := c.DoCustomRequestV5(ctx, "GET", "/password-expiry/settings.json", nil, nil)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
var passwordExpirySettings PasswordExpirySettings
31+
err = json.Unmarshal(msg.Body, &passwordExpirySettings)
32+
if err != nil {
33+
return nil, err
34+
}
35+
return &passwordExpirySettings, nil
36+
}
37+
38+
func getDefaultPasswordExpirySettings() PasswordExpirySettings {
39+
return PasswordExpirySettings{
40+
ID: "default",
41+
DefaultExpiryPeriod: 0,
42+
PolicyOverride: false,
43+
AutomaticExpiry: false,
44+
AutomaticUpdate: false,
45+
ExpiryNotificationPeriod: 0,
46+
Created: time.Now(),
47+
Modified: time.Now(),
48+
CreatedBy: "default",
49+
}
50+
}

api/resources.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Resource struct {
3535

3636
Secrets []Secret `json:"secrets,omitempty"`
3737
Tags []Tag `json:"tags,omitempty"`
38+
Expired *Time `json:"expired,omitempty"`
3839
}
3940

4041
// Tag is a Passbolt Password Tag
@@ -122,6 +123,7 @@ func (c *Client) UpdateResource(ctx context.Context, resourceID string, resource
122123
if err != nil {
123124
return nil, fmt.Errorf("Checking ID format: %w", err)
124125
}
126+
125127
msg, err := c.DoCustomRequest(ctx, "PUT", "/resources/"+resourceID+".json", "v2", resource, nil)
126128
if err != nil {
127129
return nil, err

helper/resource_create.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"time"
78

89
"github.com/passbolt/go-passbolt/api"
910
)
@@ -99,6 +100,12 @@ func CreateResourceV5(ctx context.Context, c *api.Client, folderParentID, name,
99100
}
100101
resource.Secrets = []api.Secret{{Data: encSecretData}}
101102

103+
passwordExpirySettings := c.GetPasswordExpirySettings()
104+
if passwordExpirySettings.DefaultExpiryPeriod != 0 {
105+
expiry := time.Now().Add(time.Hour * 24 * time.Duration(passwordExpirySettings.DefaultExpiryPeriod))
106+
resource.Expired = &api.Time{Time: expiry}
107+
}
108+
102109
newresource, err := c.CreateResource(ctx, resource)
103110
if err != nil {
104111
return "", fmt.Errorf("Creating Resource: %w", err)
@@ -154,6 +161,12 @@ func CreateResourceV4(ctx context.Context, c *api.Client, folderParentID, name,
154161
}
155162
resource.Secrets = []api.Secret{{Data: encSecretData}}
156163

164+
passwordExpirySettings := c.GetPasswordExpirySettings()
165+
if passwordExpirySettings.DefaultExpiryPeriod != 0 {
166+
expiry := time.Now().Add(time.Hour * 24 * time.Duration(passwordExpirySettings.DefaultExpiryPeriod))
167+
resource.Expired = &api.Time{Time: expiry}
168+
}
169+
157170
newresource, err := c.CreateResource(ctx, resource)
158171
if err != nil {
159172
return "", fmt.Errorf("Creating Resource: %w", err)

helper/resource_update.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"time"
78

89
"github.com/ProtonMail/gopenpgp/v3/crypto"
910
"github.com/passbolt/go-passbolt/api"
@@ -376,6 +377,12 @@ func UpdateResource(ctx context.Context, c *api.Client, resourceID, name, userna
376377
})
377378
}
378379

380+
passwordExpirySettings := c.GetPasswordExpirySettings()
381+
if resource.Expired != nil && passwordExpirySettings.AutomaticUpdate {
382+
expiry := time.Now().Add(time.Hour * 24 * time.Duration(passwordExpirySettings.DefaultExpiryPeriod))
383+
newResource.Expired = &api.Time{expiry}
384+
}
385+
379386
_, err = c.UpdateResource(ctx, resourceID, newResource)
380387
if err != nil {
381388
return fmt.Errorf("Updating Resource: %w", err)

0 commit comments

Comments
 (0)