-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccountMgmt.go
97 lines (87 loc) · 2.9 KB
/
accountMgmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package api
import (
"encoding/json"
"fmt"
"github.com/chanbakjsd/gotrix/api/httputil"
"github.com/chanbakjsd/gotrix/matrix"
)
// PasswordChange sends a request to the homeserver to change the password.
// All devices except the current one will be logged out if logoutDevices is set to true.
func (c *Client) PasswordChange(newPassword string, logoutDevices bool) (*UserInteractiveAuthAPI, error) {
var req struct {
Auth interface{} `json:"auth,omitempty"`
NewPassword string `json:"new_password"`
LogoutDevices bool `json:"logout_devices,omitempty"`
}
req.NewPassword = newPassword
req.LogoutDevices = logoutDevices
uiaa := &UserInteractiveAuthAPI{}
uiaa.Request = func(auth, to interface{}) error {
req.Auth = auth
err := c.Request(
"POST", c.Endpoints.AccountPassword(), to,
httputil.WithToken(), httputil.WithJSONBody(req),
)
if err != nil {
return fmt.Errorf("error changing password: %w", err)
}
return nil
}
uiaa.RequestThreePID = func(authType string, auth, to interface{}) error {
return c.Request(
"POST", c.Endpoints.AccountPasswordRequestToken(authType), nil,
httputil.WithJSONBody(auth),
)
}
err := uiaa.Auth(nil)
return uiaa, err
}
// DeactivateAccount deactivates the account of the current user.
//
// idServer is the identity server to unbind all of the user's 3PID from.
// It is optional and if not provided, the homeserver is responsible for determining
// the unbind source.
//
// It returns an InteractiveDeactivate object which should be used to interactively
// fulfill authentication requirements of the server.
func (c *Client) DeactivateAccount(idServer string) (InteractiveDeactivate, error) {
var req struct {
Auth interface{} `json:"auth,omitempty"`
IDServer string `json:"id_server"`
}
req.IDServer = idServer
uiaa := InteractiveDeactivate{
UserInteractiveAuthAPI: &UserInteractiveAuthAPI{},
}
uiaa.Request = func(auth, to interface{}) error {
req.Auth = auth
err := c.Request(
"POST", c.Endpoints.AccountDeactivate(), to,
httputil.WithToken(), httputil.WithJSONBody(req),
)
if err != nil {
return fmt.Errorf("error deactivating account: %w", err)
}
return nil
}
err := uiaa.Auth(nil)
return uiaa, err
}
// InteractiveDeactivate is a struct that adds response parsing helper functions onto UserInteractiveAuthAPI.
// To see functions on authenticating, refer to it instead.
type InteractiveDeactivate struct {
*UserInteractiveAuthAPI
}
// DeactivateResponse formats the Result() as the response of the deactivation.
// It returns an error if there isn't any result yet.
func (i InteractiveDeactivate) DeactivateResponse() (matrix.IDServerUnbindResult, error) {
msg, err := i.Result()
if err != nil {
return "", err
}
var result struct {
IDServerUnbindResult matrix.IDServerUnbindResult `json:"id_server_unbind_result"`
}
err = json.Unmarshal(*msg, &result)
return result.IDServerUnbindResult, err
}