diff options
-rw-r--r-- | example/basic/go.mod | 12 | ||||
-rw-r--r-- | example/basic/go.sum | 13 | ||||
-rw-r--r-- | example/basic/main.go | 29 | ||||
-rw-r--r-- | example/go.mod | 3 | ||||
-rw-r--r-- | go.mod | 3 | ||||
-rw-r--r-- | login.go | 75 |
6 files changed, 135 insertions, 0 deletions
diff --git a/example/basic/go.mod b/example/basic/go.mod new file mode 100644 index 0000000..c5a9de2 --- /dev/null +++ b/example/basic/go.mod @@ -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 +) diff --git a/example/basic/go.sum b/example/basic/go.sum new file mode 100644 index 0000000..86c028c --- /dev/null +++ b/example/basic/go.sum @@ -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= diff --git a/example/basic/main.go b/example/basic/main.go new file mode 100644 index 0000000..fd802ea --- /dev/null +++ b/example/basic/main.go @@ -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) +} diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..88ec6dc --- /dev/null +++ b/example/go.mod @@ -0,0 +1,3 @@ +module git.darknebu.la/emile/matrix/example + +go 1.13 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c2b9f1c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.darknebu.la/emile/matrix + +go 1.13 diff --git a/login.go b/login.go new file mode 100644 index 0000000..3e35858 --- /dev/null +++ b/login.go @@ -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"` +} |