Skip to content

Commit

Permalink
Add wrapper for account changing password
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 24, 2023
1 parent ab9d6e5 commit 682f467
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,27 @@ func (cli *Client) RegisterDummy(req *ReqRegister) (*RespRegister, error) {
return res, nil
}

func (cli *Client) ChangePassword(req *ReqChangePassword) (resp *RespChangePassword, uiaResp *RespUserInteractive, err error) {
var bodyBytes []byte
bodyBytes, err = cli.MakeFullRequest(FullRequest{
Method: http.MethodPost,
URL: cli.BuildClientURL("v3", "account", "password"),
RequestJSON: req,
SensitiveContent: len(req.NewPassword) > 0,
})
if err != nil {
httpErr, ok := err.(HTTPError)
// if response has a 401 status, but doesn't have the errcode field, it's probably a UIA response.
if ok && httpErr.IsStatus(http.StatusUnauthorized) && httpErr.RespError == nil {
err = json.Unmarshal(bodyBytes, &uiaResp)
}
} else {
// body should be RespRegister
err = json.Unmarshal(bodyBytes, &resp)
}
return
}

// GetLoginFlows fetches the login flows that the homeserver supports using https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3login
func (cli *Client) GetLoginFlows() (resp *RespLoginFlows, err error) {
urlPath := cli.BuildClientURL("v3", "login")
Expand Down
6 changes: 6 additions & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ type ReqRegister struct {
Type AuthType `json:"type,omitempty"`
}

type ReqChangePassword struct {
Auth any `json:"auth"`
LogoutDevices bool `json:"logout_devices"`
NewPassword string `json:"new_password"`
}

type BaseAuthData struct {
Type AuthType `json:"type"`
Session string `json:"session,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ type RespRegister struct {
HomeServer string `json:"home_server,omitempty"`
}

type RespChangePassword struct{}

type LoginFlow struct {
Type AuthType `json:"type"`
}
Expand Down

0 comments on commit 682f467

Please sign in to comment.