Browse Source

initial commit

master
emile 2 years ago
commit
b43e5d69d2
Signed by: emile
GPG Key ID: 4D8DD313A751DED7
  1. 12
      example/basic/go.mod
  2. 13
      example/basic/go.sum
  3. 29
      example/basic/main.go
  4. 3
      example/go.mod
  5. 3
      go.mod
  6. 75
      login.go

12
example/basic/go.mod

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
module git.darknebu.la/emile/matrix/example/basic
go 1.13
replace git.darknebu.la/emile/matrix => ../../
require (
git.darknebu.la/emile/matrix v0.0.0-00010101000000-000000000000
github.com/h2non/gentleman v2.0.4+incompatible // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
gopkg.in/h2non/gentleman.v2 v2.0.4 // indirect
)

13
example/basic/go.sum

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
github.com/h2non/gentleman v2.0.4+incompatible h1:C5vcqSVYEmc1aJcK7biWLfEmTFRbbvwjsWEFS7y6J+E=
github.com/h2non/gentleman v2.0.4+incompatible/go.mod h1:ddrWLQr62qU8EOGml9CArC88lNeDkYPmysfWX1PUKK4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/h2non/gentleman.v2 v2.0.4 h1:9R3K6CFYd/RdXDLi0pGXwaPnRx/pn5EZlrN3VkNygWc=
gopkg.in/h2non/gentleman.v2 v2.0.4/go.mod h1:A1c7zwrTgAyyf6AbpvVksYtBayTB4STBUGmdkEtlHeA=

29
example/basic/main.go

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
package main
import (
"fmt"
"git.darknebu.la/emile/matrix"
)
func main() {
fmt.Println("Basic example")
username := "testbot1"
password := "testbot1"
homeserver := "https://matrix.emile.space"
authinfo, err := matrix.Login(username, password, homeserver)
if err != nil {
fmt.Println("ERROR")
fmt.Println(err)
return
}
fmt.Printf("UserID %v\n", authinfo.UserID)
fmt.Printf("HomeServer %v\n", authinfo.HomeServer)
fmt.Printf("DeviceID %v\n", authinfo.DeviceID)
fmt.Printf("AccessToken %v\n", authinfo.AccessToken)
matrix.Sync(authinfo)
}

3
example/go.mod

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
module git.darknebu.la/emile/matrix/example
go 1.13

3
go.mod

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
module git.darknebu.la/emile/matrix
go 1.13

75
login.go

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
package matrix
import (
"encoding/json"
"github.com/h2non/gentleman/plugins/body"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/query"
)
// 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
}
return authinfo, nil
}
func Sync(authinfo Authinfo) {
cli := gentleman.New()
cli.URL(authinfo.HomeServer)
req := cli.Request()
req.Path("/_matrix/client/r0/sync")
req.Method("GET")
req.Use(query.Set("access_token", authinfo.AccessToken))
res, err := req.Send()
if err != nil {
return err
}
if !res.Ok {
return err
}
var authinfo Authinfo
if err := json.Unmarshal(res.Bytes(), &authinfo); err != nil {
return Authinfo{}, err
}
return authinfo, nil
}
// Authinfo defines the fields returned after logging in
type Authinfo struct {
UserID string `json:"user_id"`
HomeServer string `json:"home_server"`
DeviceID string `json:"device_id"`
AccessToken string `json:"access_token"`
}
Loading…
Cancel
Save