From c698fb0c10213cad0c2f6fa63c3d296071e73df2 Mon Sep 17 00:00:00 2001 From: f4814n Date: Thu, 27 Aug 2020 14:05:45 +0200 Subject: [PATCH] Update Login() Logout() and LogoutAll() to r0.6.0 (#73) Co-authored-by: Neil Alexander --- client.go | 10 +++++++- identifier.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ requests.go | 19 +++++++------- responses.go | 26 ++++++++++++++----- 4 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 identifier.go diff --git a/client.go b/client.go index 68a7bdd..fd31946 100644 --- a/client.go +++ b/client.go @@ -354,7 +354,7 @@ func (cli *Client) Login(req *ReqLogin) (resp *RespLogin, err error) { return } -// Logout the current user. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout +// Logout the current user. See http://matrix.org/docs/spec/client_server/r0.6.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") @@ -362,6 +362,14 @@ func (cli *Client) Logout() (resp *RespLogout, err error) { return } +// LogoutAll logs the current user out on all devices. See https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-logout-all +// This does not clear the credentials from the client instance. See ClearCredentails() instead. +func (cli *Client) LogoutAll() (resp *RespLogoutAll, err error) { + urlPath := cli.BuildURL("logout/all") + err = cli.MakeRequest("POST", urlPath, nil, &resp) + 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 func (cli *Client) Versions() (resp *RespVersions, err error) { urlPath := cli.BuildBaseURL("_matrix", "client", "versions") diff --git a/identifier.go b/identifier.go new file mode 100644 index 0000000..4a61d08 --- /dev/null +++ b/identifier.go @@ -0,0 +1,69 @@ +package gomatrix + +// Identifier is the interface for https://matrix.org/docs/spec/client_server/r0.6.0#identifier-types +type Identifier interface { + // Returns the identifier type + // https://matrix.org/docs/spec/client_server/r0.6.0#identifier-types + Type() string +} + +// UserIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#matrix-user-id +type UserIdentifier struct { + IDType string `json:"type"` // Set by NewUserIdentifer + User string `json:"user"` +} + +// Type implements the Identifier interface +func (i UserIdentifier) Type() string { + return "m.id.user" +} + +// NewUserIdentifier creates a new UserIdentifier with IDType set to "m.id.user" +func NewUserIdentifier(user string) UserIdentifier { + return UserIdentifier{ + IDType: "m.id.user", + User: user, + } +} + +// ThirdpartyIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#third-party-id +type ThirdpartyIdentifier struct { + IDType string `json:"type"` // Set by NewThirdpartyIdentifier + Medium string `json:"medium"` + Address string `json:"address"` +} + +// Type implements the Identifier interface +func (i ThirdpartyIdentifier) Type() string { + return "m.id.thirdparty" +} + +// NewThirdpartyIdentifier creates a new UserIdentifier with IDType set to "m.id.user" +func NewThirdpartyIdentifier(medium, address string) ThirdpartyIdentifier { + return ThirdpartyIdentifier{ + IDType: "m.id.thirdparty", + Medium: medium, + Address: address, + } +} + +// PhoneIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#phone-number +type PhoneIdentifier struct { + IDType string `json:"type"` // Set by NewPhoneIdentifier + Country string `json:"country"` + Phone string `json:"phone"` +} + +// Type implements the Identifier interface +func (i PhoneIdentifier) Type() string { + return "m.id.phone" +} + +// NewPhoneIdentifier creates a new UserIdentifier with IDType set to "m.id.user" +func NewPhoneIdentifier(country, phone string) PhoneIdentifier { + return PhoneIdentifier{ + IDType: "m.id.phone", + Country: country, + Phone: phone, + } +} diff --git a/requests.go b/requests.go index af99a22..31c426d 100644 --- a/requests.go +++ b/requests.go @@ -10,16 +10,17 @@ type ReqRegister struct { Auth interface{} `json:"auth,omitempty"` } -// ReqLogin is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login +// ReqLogin is the JSON request for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-login type ReqLogin struct { - Type string `json:"type"` - Password string `json:"password,omitempty"` - Medium string `json:"medium,omitempty"` - User string `json:"user,omitempty"` - Address string `json:"address,omitempty"` - Token string `json:"token,omitempty"` - DeviceID string `json:"device_id,omitempty"` - InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"` + Type string `json:"type"` + Identifier Identifier `json:"identifier,omitempty"` + Password string `json:"password,omitempty"` + Medium string `json:"medium,omitempty"` + User string `json:"user,omitempty"` + Address string `json:"address,omitempty"` + Token string `json:"token,omitempty"` + DeviceID string `json:"device_id,omitempty"` + InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"` } // ReqCreateRoom is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom diff --git a/responses.go b/responses.go index effb609..e0b8697 100644 --- a/responses.go +++ b/responses.go @@ -122,17 +122,31 @@ type RespRegister struct { UserID string `json:"user_id"` } -// RespLogin is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login +// RespLogin is the JSON response for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-login type RespLogin struct { - AccessToken string `json:"access_token"` - DeviceID string `json:"device_id"` - HomeServer string `json:"home_server"` - UserID string `json:"user_id"` + AccessToken string `json:"access_token"` + DeviceID string `json:"device_id"` + HomeServer string `json:"home_server"` + UserID string `json:"user_id"` + WellKnown DiscoveryInformation `json:"well_known"` } -// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout +// DiscoveryInformation is the JSON Response for https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client and a part of the JSON Response for https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-login +type DiscoveryInformation struct { + Homeserver struct { + BaseURL string `json:"base_url"` + } `json:"m.homeserver"` + IdentityServer struct { + BaseURL string `json:"base_url"` + } `json:"m.identitiy_server"` +} + +// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-logout type RespLogout struct{} +// RespLogoutAll is the JSON response for https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-logout-all +type RespLogoutAll 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"`