a simple matrix package for basic actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
966 B

package matrix
import (
"encoding/json"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/body"
)
// Login logs in to the homeserver and returns an Authinfo struct containing
// information such as the UserID, the HomeServer of the entity that was logged
// and most important: the AccessToken for further verification
func Login(username, password, homeserver string) (Authinfo, error) {
cli := gentleman.New()
cli.URL(homeserver)
req := cli.Request()
req.Path("/_matrix/client/r0/login")
req.Method("POST")
data := map[string]string{
"type": "m.login.password",
"user": username,
"password": password,
}
req.Use(body.JSON(data))
res, err := req.Send()
if err != nil {
return Authinfo{}, err
}
if !res.Ok {
return Authinfo{}, err
}
var authinfo Authinfo
if err := json.Unmarshal(res.Bytes(), &authinfo); err != nil {
return Authinfo{}, err
}
authinfo.HomeServer = homeserver
return authinfo, nil
}