| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/base64" |
| 6 |
|
| 7 |
"github.com/ProtonMail/go-srp" |
| 8 |
"github.com/go-resty/resty/v2" |
| 9 |
) |
| 10 |
|
| 11 |
func (c *Client) GetUser(ctx context.Context) (User, error) { |
| 12 |
var res struct { |
| 13 |
User User |
| 14 |
} |
| 15 |
|
| 16 |
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 17 |
return r.SetResult(&res).Get("/core/v4/users") |
| 18 |
}); err != nil { |
| 19 |
return User{}, err |
| 20 |
} |
| 21 |
|
| 22 |
return res.User, nil |
| 23 |
} |
| 24 |
|
| 25 |
func (c *Client) DeleteUser(ctx context.Context, password []byte, req DeleteUserReq) error { |
| 26 |
user, err := c.GetUser(ctx) |
| 27 |
if err != nil { |
| 28 |
return err |
| 29 |
} |
| 30 |
|
| 31 |
info, err := c.m.AuthInfo(ctx, AuthInfoReq{Username: user.Name}) |
| 32 |
if err != nil { |
| 33 |
return err |
| 34 |
} |
| 35 |
|
| 36 |
srpAuth, err := srp.NewAuth(info.Version, user.Name, password, info.Salt, info.Modulus, info.ServerEphemeral) |
| 37 |
if err != nil { |
| 38 |
return err |
| 39 |
} |
| 40 |
|
| 41 |
proofs, err := srpAuth.GenerateProofs(2048) |
| 42 |
if err != nil { |
| 43 |
return err |
| 44 |
} |
| 45 |
|
| 46 |
return c.do(ctx, func(r *resty.Request) (*resty.Response, error) { |
| 47 |
return r.SetBody(struct { |
| 48 |
DeleteUserReq |
| 49 |
AuthReq |
| 50 |
}{ |
| 51 |
DeleteUserReq: req, |
| 52 |
AuthReq: AuthReq{ |
| 53 |
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof), |
| 54 |
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral), |
| 55 |
SRPSession: info.SRPSession, |
| 56 |
}, |
| 57 |
}).Delete("/core/v4/users/delete") |
| 58 |
}) |
| 59 |
} |