diff options
Diffstat (limited to 'mxlib')
-rw-r--r-- | mxlib/api.go | 13 | ||||
-rw-r--r-- | mxlib/client.go | 26 |
2 files changed, 38 insertions, 1 deletions
diff --git a/mxlib/api.go b/mxlib/api.go index 7752abc..6d35108 100644 --- a/mxlib/api.go +++ b/mxlib/api.go @@ -26,6 +26,19 @@ type Event struct { OriginServerTs int `json:"origin_server_ts"` } +type PasswordLoginRequest struct { + Type string `json:"type"` + Identifier map[string]string `json:"identifier"` + Password string `json:"password"` + DeviceID string `json:"device_id"` + InitialDeviceDisplayNAme string `json:"initial_device_display_name"` +} + +type LoginResponse struct { + UserID string `json:"user_id"` + AccessToken string `json:"access_token"` +} + type RegisterRequest struct { Username string `json:"username"` } diff --git a/mxlib/client.go b/mxlib/client.go index f8124d3..e382ddd 100644 --- a/mxlib/client.go +++ b/mxlib/client.go @@ -79,7 +79,9 @@ func (mx *Client) PostApiCall(endpoint string, data interface{}, response interf } func (mx *Client) DoAndParse(req *http.Request, response interface{}) error { - req.Header.Add("Authorization", "Bearer "+mx.Token) + if mx.Token != "" { + req.Header.Add("Authorization", "Bearer "+mx.Token) + } resp, err := mx.httpClient.Do(req) if err != nil { @@ -107,6 +109,28 @@ func (mx *Client) DoAndParse(req *http.Request, response interface{}) error { // ---- +func (mx *Client) PasswordLogin(username string, password string, device_id string, device_name string) (string, error) { + req := PasswordLoginRequest{ + Type: "m.login.password", + Identifier: map[string]string{ + "type": "m.id.user", + "user": username, + }, + Password: password, + DeviceID: device_id, + InitialDeviceDisplayNAme: device_name, + } + var rep LoginResponse + err := mx.PostApiCall("/_matrix/client/r0/login", &req, &rep) + if err != nil { + return "", err + } + if mx.Token == "" { + mx.Token = rep.AccessToken + } + return rep.UserID, nil +} + func (mx *Client) RegisterUser(username string) error { req := RegisterRequest{ Username: username, |