Add option to store credentials directly in Login()

This commit is contained in:
Tulir Asokan 2020-07-04 20:18:22 +03:00
commit 1ee81815fd
3 changed files with 12 additions and 2 deletions

View file

@ -178,6 +178,8 @@ func (cli *Client) BuildURLWithQuery(urlPath URLPath, urlQuery map[string]string
}
// SetCredentials sets the user ID and access token on this client instance.
//
// Deprecated: use the StoreCredentials field in ReqLogin instead.
func (cli *Client) SetCredentials(userID id.UserID, accessToken string) {
cli.AccessToken = accessToken
cli.UserID = userID
@ -470,10 +472,15 @@ func (cli *Client) GetLoginFlows() (resp *RespLoginFlows, err error) {
}
// Login a user to the homeserver according to http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login
// This does not set credentials on this client instance. See SetCredentials() instead.
func (cli *Client) Login(req *ReqLogin) (resp *RespLogin, err error) {
urlPath := cli.BuildURL("login")
_, err = cli.MakeRequest("POST", urlPath, req, &resp)
if req.StoreCredentials && err == nil {
cli.DeviceID = resp.DeviceID
cli.AccessToken = resp.AccessToken
cli.UserID = resp.UserID
// TODO update cli.HomeserverURL based on the .well-known data in the login response
}
return
}

View file

@ -38,6 +38,9 @@ type ReqLogin struct {
Token string `json:"token,omitempty"`
DeviceID id.DeviceID `json:"device_id,omitempty"`
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"`
// Whether or not the returned credentials should be stored in the Client
StoreCredentials bool `json:"-"`
}
// ReqCreateRoom is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom

View file

@ -134,8 +134,8 @@ type RespLoginFlows struct {
type RespLogin struct {
AccessToken string `json:"access_token"`
DeviceID id.DeviceID `json:"device_id"`
HomeServer string `json:"home_server"`
UserID id.UserID `json:"user_id"`
// TODO add .well-known field here
}
// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout