From 0827f53d1c9e6be7e9f846dd28ca31ebfbfcff1f Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 14:16:47 +0000 Subject: [PATCH 1/3] Add /logout API --- client.go | 12 ++++++++++++ responses.go | 3 +++ 2 files changed, 15 insertions(+) diff --git a/client.go b/client.go index c3a4d4e..d10704e 100644 --- a/client.go +++ b/client.go @@ -350,6 +350,18 @@ func (cli *Client) Login(req *ReqLogin, setOnClient bool) (resp *RespLogin, err return } +// Logout the current user. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout +// If "removeCredentials" is true, the user ID and access token will be removed from this client instance on success. +func (cli *Client) Logout(removeCredentials bool) (resp *RespLogout, err error) { + urlPath := cli.BuildURL("logout") + _, err = cli.MakeRequest("POST", urlPath, nil, &resp) + if removeCredentials && err == nil { + cli.UserID = "" + cli.AccessToken = "" + } + return +} + // JoinRoom joins the client to a room ID or alias. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-join-roomidoralias // // If serverName is specified, this will be added as a query param to instruct the homeserver to join via that server. If content is specified, it will diff --git a/responses.go b/responses.go index 1cfd4e0..f71742e 100644 --- a/responses.go +++ b/responses.go @@ -74,6 +74,9 @@ type RespLogin struct { UserID string `json:"user_id"` } +// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout +type RespLogout struct{} + // RespCreateRoom is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom type RespCreateRoom struct { RoomID string `json:"room_id"` From 16f8e30850e7b9eee2f895f277fc98ebba732c09 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 6 Jan 2017 09:38:59 +0000 Subject: [PATCH 2/3] Merge master into kegan/api-logout --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index a12dd38..b30b190 100644 --- a/client.go +++ b/client.go @@ -359,7 +359,7 @@ func (cli *Client) Logout(removeCredentials bool) (resp *RespLogout, err error) cli.UserID = "" cli.AccessToken = "" } - return + return } // Versions returns the list of supported Matrix versions on this homeserver. See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-versions From 837d0c30d07dbe4d62809e0976a3d044275a5c97 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 6 Jan 2017 10:54:26 +0000 Subject: [PATCH 3/3] Review comments: be explicit at call-sites when setting credentials --- client.go | 41 +++++++++++++++++++++-------------------- client_examples_test.go | 7 ++++--- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/client.go b/client.go index b30b190..dee17b2 100644 --- a/client.go +++ b/client.go @@ -90,6 +90,18 @@ func (cli *Client) BuildURLWithQuery(urlPath []string, urlQuery map[string]strin return u.String() } +// SetCredentials sets the user ID and access token on this client instance. +func (cli *Client) SetCredentials(userID, accessToken string) { + cli.AccessToken = accessToken + cli.UserID = userID +} + +// ClearCredentials removes the user ID and access token on this client instance. +func (cli *Client) ClearCredentials() { + cli.AccessToken = "" + cli.UserID = "" +} + // Sync starts syncing with the provided Homeserver. If Sync() is called twice then the first sync will be stopped and the // error will be nil. // @@ -302,18 +314,19 @@ func (cli *Client) RegisterGuest(req *ReqRegister) (*RespRegister, *RespUserInte // RegisterDummy performs m.login.dummy registration according to https://matrix.org/docs/spec/client_server/r0.2.0.html#dummy-auth // // Only a username and password need to be provided on the ReqRegister struct. Most local/developer homeservers will allow registration -// this way. If the homeserver does not, an error is returned. If "setOnClient" is true, the access_token and user_id will be set on -// this client instance. +// this way. If the homeserver does not, an error is returned. +// +// This does not set credentials on the client instance. See SetCredentials() instead. // // res, err := cli.RegisterDummy(&gomatrix.ReqRegister{ // Username: "alice", // Password: "wonderland", -// }, false) +// }) // if err != nil { // panic(err) // } // token := res.AccessToken -func (cli *Client) RegisterDummy(req *ReqRegister, setOnClient bool) (*RespRegister, error) { +func (cli *Client) RegisterDummy(req *ReqRegister) (*RespRegister, error) { res, uia, err := cli.Register(req) if err != nil && uia == nil { return nil, err @@ -331,34 +344,22 @@ func (cli *Client) RegisterDummy(req *ReqRegister, setOnClient bool) (*RespRegis if res == nil { return nil, fmt.Errorf("registration failed: does this server support m.login.dummy?") } - if setOnClient { - cli.UserID = res.UserID - cli.AccessToken = res.AccessToken - } return res, nil } // Login a user to the homeserver according to http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login -// If 'setOnClient' is true, the user ID and access token on login will be set to this client instance. -func (cli *Client) Login(req *ReqLogin, setOnClient bool) (resp *RespLogin, err error) { +// 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 setOnClient && resp != nil { - cli.UserID = resp.UserID - cli.AccessToken = resp.AccessToken - } return } // Logout the current user. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout -// If "removeCredentials" is true, the user ID and access token will be removed from this client instance on success. -func (cli *Client) Logout(removeCredentials bool) (resp *RespLogout, err error) { +// This does not clear the credentials from the client instance. See ClearCredentials() instead. +func (cli *Client) Logout() (resp *RespLogout, err error) { urlPath := cli.BuildURL("logout") _, err = cli.MakeRequest("POST", urlPath, nil, &resp) - if removeCredentials && err == nil { - cli.UserID = "" - cli.AccessToken = "" - } return } diff --git a/client_examples_test.go b/client_examples_test.go index 7c20c77..57a8d9f 100644 --- a/client_examples_test.go +++ b/client_examples_test.go @@ -104,15 +104,16 @@ func ExampleClient_JoinRoom_alias() { } } -// Login to a local homeserver. This will set Client.UserID and Client.AccessToken on success. +// Login to a local homeserver and set the user ID and access token on success. func ExampleClient_Login() { cli, _ := NewClient("http://localhost:8008", "", "") - _, err := cli.Login(&ReqLogin{ + resp, err := cli.Login(&ReqLogin{ Type: "m.login.password", User: "alice", Password: "wonderland", - }, true) + }) if err != nil { panic(err) } + cli.SetCredentials(resp.UserID, resp.AccessToken) }