mirror of https://github.com/matrix-org/gomatrix
Merge pull request #11 from matrix-org/kegan/api-logout
Add /logout API
This commit is contained in:
commit
f3c8c9580d
41
client.go
41
client.go
|
@ -90,6 +90,18 @@ func (cli *Client) BuildURLWithQuery(urlPath []string, urlQuery map[string]strin
|
||||||
return u.String()
|
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
|
// 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.
|
// 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
|
// 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
|
// 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 way. If the homeserver does not, an error is returned.
|
||||||
// this client instance.
|
//
|
||||||
|
// This does not set credentials on the client instance. See SetCredentials() instead.
|
||||||
//
|
//
|
||||||
// res, err := cli.RegisterDummy(&gomatrix.ReqRegister{
|
// res, err := cli.RegisterDummy(&gomatrix.ReqRegister{
|
||||||
// Username: "alice",
|
// Username: "alice",
|
||||||
// Password: "wonderland",
|
// Password: "wonderland",
|
||||||
// }, false)
|
// })
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// panic(err)
|
// panic(err)
|
||||||
// }
|
// }
|
||||||
// token := res.AccessToken
|
// 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)
|
res, uia, err := cli.Register(req)
|
||||||
if err != nil && uia == nil {
|
if err != nil && uia == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -331,22 +344,22 @@ func (cli *Client) RegisterDummy(req *ReqRegister, setOnClient bool) (*RespRegis
|
||||||
if res == nil {
|
if res == nil {
|
||||||
return nil, fmt.Errorf("registration failed: does this server support m.login.dummy?")
|
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
|
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
|
// 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.
|
// This does not set credentials on this client instance. See SetCredentials() instead.
|
||||||
func (cli *Client) Login(req *ReqLogin, setOnClient bool) (resp *RespLogin, err error) {
|
func (cli *Client) Login(req *ReqLogin) (resp *RespLogin, err error) {
|
||||||
urlPath := cli.BuildURL("login")
|
urlPath := cli.BuildURL("login")
|
||||||
_, err = cli.MakeRequest("POST", urlPath, req, &resp)
|
_, err = cli.MakeRequest("POST", urlPath, req, &resp)
|
||||||
if setOnClient && resp != nil {
|
return
|
||||||
cli.UserID = resp.UserID
|
}
|
||||||
cli.AccessToken = resp.AccessToken
|
|
||||||
}
|
// Logout the current user. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout
|
||||||
|
// 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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
func ExampleClient_Login() {
|
||||||
cli, _ := NewClient("http://localhost:8008", "", "")
|
cli, _ := NewClient("http://localhost:8008", "", "")
|
||||||
_, err := cli.Login(&ReqLogin{
|
resp, err := cli.Login(&ReqLogin{
|
||||||
Type: "m.login.password",
|
Type: "m.login.password",
|
||||||
User: "alice",
|
User: "alice",
|
||||||
Password: "wonderland",
|
Password: "wonderland",
|
||||||
}, true)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
cli.SetCredentials(resp.UserID, resp.AccessToken)
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,6 +94,9 @@ type RespLogin struct {
|
||||||
UserID string `json:"user_id"`
|
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
|
// 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 {
|
type RespCreateRoom struct {
|
||||||
RoomID string `json:"room_id"`
|
RoomID string `json:"room_id"`
|
||||||
|
|
Loading…
Reference in New Issue