matrixprotobuf

git clone git://archive.git.mtrnord.blog/MTRNord/matrixprotobuf.git
Log | Files | Refs | README | LICENSE

commit 6a4acf16074b3035d66590920c0e1ec9c8407399
parent 14321c990199b2898e633ae6ee4709f65f7e57c9
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sun, 17 Sep 2017 15:49:24 +0200

Rework RPC Login and RPC Client

Signed-off-by: MTRNord <mtrnord1@gmail.com>

Diffstat:
Msrc/github.com/Nordgedanken/MatrixProtoBuf/jsonTypes/unauthorized.go | 31+++++++++++++++++++++++++++++++
Asrc/github.com/Nordgedanken/MatrixProtoBuf/matrix/noAuth/matrixRequests.go | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go | 66++++++++++++++++++++++++++++++++++++++----------------------------
Msrc/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto | 7+++++--
Msrc/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go | 12+++++++++++-
Msrc/github.com/Nordgedanken/mpbRPCClient/client.go | 15++++++++++++---
6 files changed, 156 insertions(+), 34 deletions(-)

diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/jsonTypes/unauthorized.go b/src/github.com/Nordgedanken/MatrixProtoBuf/jsonTypes/unauthorized.go @@ -3,3 +3,34 @@ package jsonTypes type HSVersions struct { Versions []string `json:"versions"` } + +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 RespLogin struct { + AccessToken string `json:"access_token"` + DeviceID string `json:"device_id"` + HomeServer string `json:"home_server"` + UserID string `json:"user_id"` + RefreshToken string `json:"refresh_token,omitempty"` +} + +// RespError is the standard JSON error response from Homeservers. It also implements the Golang "error" interface. +// See http://matrix.org/docs/spec/client_server/r0.2.0.html#api-standards +type RespError struct { + ErrCode string `json:"errcode"` + Err string `json:"error"` +} + +// Error returns the errcode and error message. +func (e RespError) Error() string { + return e.ErrCode + ": " + e.Err +} diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrix/noAuth/matrixRequests.go b/src/github.com/Nordgedanken/MatrixProtoBuf/matrix/noAuth/matrixRequests.go @@ -0,0 +1,59 @@ +package noAuth + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "github.com/Nordgedanken/MatrixProtoBuf/jsonTypes" + "io/ioutil" + "net/http" +) + +func Login(address string, req *jsonTypes.ReqLogin, resp *jsonTypes.RespLogin) (err error, errCode int64) { + var data []byte + data, err = json.Marshal(req) + if err != nil { + errCode = 500 + return + } + + reqresp, err := http.Post("https://"+address+"/_matrix/client/r0/login", "application/json", bytes.NewBuffer(data)) + if err != nil { + errCode = 500 + return + } + + defer reqresp.Body.Close() + body, err := ioutil.ReadAll(reqresp.Body) + + if reqresp.StatusCode/100 != 2 { // not 2xx + var wrap error + var respErr jsonTypes.RespError + if _ = json.Unmarshal(body, &respErr); respErr.ErrCode != "" { + fmt.Errorf(respErr.Error()) + errCode = 3 + err = errors.New("failed to interpret JSON ERROR from Login Endpoint of the defined HS") + return + } + + // If we failed to decode as RespError, don't just drop the HTTP body, include it in the + // HTTP error instead (e.g proxy errors which return HTML). + msg := "Failed to POST JSON to " + reqresp.Request.URL.String() + if wrap == nil { + msg = msg + ": " + string(body) + } + + errCode = int64(reqresp.StatusCode) + err = errors.New(msg) + return + } + + if err = json.Unmarshal(body, resp); err != nil { + fmt.Errorf(err.Error()) + errCode = 500 + err = errors.New("failed to interpret JSON from Login Endpoint of the defined HS") + return + } + return +} diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go b/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go @@ -13,6 +13,7 @@ It has these top-level messages: VersionRequest LoginRequest LoginResponse + LoginError */ package matrixProtos @@ -137,8 +138,6 @@ type LoginResponse struct { HomeServer string `protobuf:"bytes,2,opt,name=home_server,json=homeServer" json:"home_server,omitempty"` UserId string `protobuf:"bytes,3,opt,name=user_id,json=userId" json:"user_id,omitempty"` RefreshToken string `protobuf:"bytes,4,opt,name=refresh_token,json=refreshToken" json:"refresh_token,omitempty"` - Errcode string `protobuf:"bytes,5,opt,name=errcode" json:"errcode,omitempty"` - Error string `protobuf:"bytes,6,opt,name=error" json:"error,omitempty"` } func (m *LoginResponse) Reset() { *m = LoginResponse{} } @@ -174,14 +173,24 @@ func (m *LoginResponse) GetRefreshToken() string { return "" } -func (m *LoginResponse) GetErrcode() string { +type LoginError struct { + Errcode string `protobuf:"bytes,1,opt,name=errcode" json:"errcode,omitempty"` + Error string `protobuf:"bytes,2,opt,name=error" json:"error,omitempty"` +} + +func (m *LoginError) Reset() { *m = LoginError{} } +func (m *LoginError) String() string { return proto.CompactTextString(m) } +func (*LoginError) ProtoMessage() {} +func (*LoginError) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} } + +func (m *LoginError) GetErrcode() string { if m != nil { return m.Errcode } return "" } -func (m *LoginResponse) GetError() string { +func (m *LoginError) GetError() string { if m != nil { return m.Error } @@ -194,6 +203,7 @@ func init() { proto.RegisterType((*VersionRequest)(nil), "matrixProtos.VersionRequest") proto.RegisterType((*LoginRequest)(nil), "matrixProtos.LoginRequest") proto.RegisterType((*LoginResponse)(nil), "matrixProtos.LoginResponse") + proto.RegisterType((*LoginError)(nil), "matrixProtos.LoginError") } // Reference imports to suppress errors if they are not otherwise used. @@ -335,28 +345,28 @@ var _LoginService_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("matrix.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 357 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0xc1, 0x4e, 0xe3, 0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c8, 0x76, 0x5d, 0x61, 0xb5, 0x19, 0xa3, 0xb3, 0x1d, 0xe3, - 0xdb, 0x83, 0x3e, 0xc7, 0xe0, 0xcb, 0x39, 0x85, 0x30, 0x49, 0x53, 0x24, 0x9a, 0x57, 0xea, 0x15, - 0xdd, 0x3f, 0xf6, 0x2c, 0xbb, 0x37, 0x48, 0x9c, 0x40, 0x6f, 0xa1, 0x0a, 0x9c, 0x13, 0xea, 0x75, - 0x93, 0x0e, 0x0c, 0xba, 0xab, 0x89, 0x38, 0x80, 0xc0, 0x64, 0x9d, 0xe7, 0x2e, 0xa2, 0x6f, 0xe4, - 0x4d, 0x26, 0x86, 0xd0, 0xd7, 0xf8, 0xac, 0x91, 0x16, 0x3c, 0xdd, 0xe6, 0x0c, 0x19, 0xda, 0xf1, - 0x12, 0x02, 0xd4, 0x3a, 0x55, 0x19, 0xba, 0xb4, 0x2c, 0xc5, 0x7f, 0xe8, 0xa0, 0xd6, 0x4a, 0x4b, - 0xbf, 0xe6, 0x56, 0x4c, 0x1e, 0xe1, 0xaf, 0x5b, 0xb1, 0xf9, 0x7e, 0x9e, 0xa2, 0xb8, 0x86, 0xae, - 0x43, 0xe2, 0x68, 0xf7, 0x6e, 0xed, 0xb5, 0x47, 0xc7, 0x3b, 0xab, 0xcd, 0x5b, 0x99, 0x4c, 0x79, - 0x4d, 0x6e, 0xf2, 0x25, 0x74, 0x6a, 0x2d, 0xa2, 0xed, 0xc6, 0xcd, 0x5d, 0x46, 0x87, 0x3b, 0x6b, - 0x76, 0xe2, 0x93, 0x5f, 0x3f, 0xd3, 0x8b, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x58, 0xc4, 0x47, - 0xdc, 0xb6, 0x02, 0x00, 0x00, + // 362 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x3f, 0x4f, 0xe3, 0x40, + 0x10, 0xc5, 0xe5,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x56, 0x44, 0x6d, 0xc7, 0x68, 0xed, 0xc6, + 0xf8, 0xf4, 0xa0, 0x2f, 0x31, 0xe4, 0x71, 0x8e, 0xc1, 0x8f, 0x93, 0x04, 0x99, 0x17, 0xa5, 0x7e, + 0x46, 0x77, 0xc7, 0x5e, 0xc5, 0x6e, 0x0d, 0x52, 0x47, 0xd0, 0x5b, 0xea, 0x1c, 0x17, 0x8c, 0xb4, + 0xa9, 0xd3, 0x81, 0x41, 0x37, 0x96, 0xa8, 0x7f, 0xd0, 0x31, 0x59, 0x17, 0x99, 0x8b, 0xd8, 0x36, + 0xf2, 0x2a, 0x55, 0x43, 0xe8, 0x13, 0x3e, 0x12, 0xf2, 0x52, 0xa6, 0x57, 0x39, 0x7d, 0x81, 0x76, + 0x7c, 0x74, 0x0e, 0x60, 0x23, 0x4d, 0x89, 0x34, 0x99, 0xec, 0x48, 0x94, 0xe8, 0xd4, 0x3d, 0x8d, + 0x93, 0xea, 0x37, 0xb4, 0xd0, 0x58, 0x24, 0x40, 0x25, 0x26, 0xf7, 0xf0, 0xd3, 0x2d, 0xdc, 0xa4, + 0xc9, 0x12, 0x54, 0x97, 0xd0, 0x75, 0x48, 0x1d, 0xec, 0xdf, 0x74, 0xb5, 0x84, 0xf0, 0x70, 0x6f, + 0xb5, 0xfe, 0x39, 0x93, 0x99, 0x2c, 0xcd, 0x4d, 0xbe, 0x80, 0x96, 0xd5, 0x2a, 0xdc, 0x6d, 0xdc, + 0xde, 0x6c, 0xf8, 0x7f, 0x6f, 0xad, 0x9a, 0xf8, 0xd0, 0xb6, 0x9f, 0xf6, 0xec, 0x2b, 0x00, 0x00, + 0xff, 0xff, 0xe2, 0x4a, 0x17, 0x1e, 0xc4, 0x02, 0x00, 0x00, } diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto b/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto @@ -34,6 +34,9 @@ message LoginResponse { string home_server = 2; string user_id = 3; string refresh_token = 4; - string errcode = 5; - string error = 6; +} + +message LoginError { + string errcode = 1; + string error = 2; } \ No newline at end of file diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go b/src/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "github.com/Nordgedanken/MatrixProtoBuf/jsonTypes" + "github.com/Nordgedanken/MatrixProtoBuf/matrix/noAuth" pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos" "golang.org/x/net/context" "google.golang.org/grpc" @@ -23,7 +24,16 @@ type loginServ struct{} // Login implements pb.VersionsServiceServer func (s *loginServ) Login(_ context.Context, r *pb.LoginRequest) (*pb.LoginResponse, error) { - return &pb.LoginResponse{"test", "localhost", r.User, "test", "", ""}, nil + var hsLoginResp jsonTypes.RespLogin + hsLoginReq := jsonTypes.ReqLogin{ + Type: r.Type, + User: r.User, + Password: r.Password, + } + + noAuth.Login(r.Address, &hsLoginReq, &hsLoginResp) + + return &pb.LoginResponse{AccessToken: hsLoginResp.AccessToken, HomeServer: hsLoginResp.HomeServer, UserId: hsLoginResp.UserID, RefreshToken: hsLoginResp.RefreshToken}, nil } // server is used to implement pb.LoginServiceServer. diff --git a/src/github.com/Nordgedanken/mpbRPCClient/client.go b/src/github.com/Nordgedanken/mpbRPCClient/client.go @@ -18,21 +18,30 @@ func main() { log.Fatalf("did not connect: %v", err) } defer conn.Close() + + Login(conn) + GetHSVersions(conn) +} + +func Login(conn *grpc.ClientConn) { lc := pb.NewLoginServiceClient(conn) // Contact the server and print out its response. - lr, err := lc.Login(context.Background(), &pb.LoginRequest{"m.password", "test", "", "", ""}) + //TODO REMOVE MY PASSWORD!!!!! + lr, err := lc.Login(context.Background(), &pb.LoginRequest{Type: "m.login.password", User: "@MTRNord:matrix.org", Password: "", Medium: "", Address: "matrix.org"}) if err != nil { - log.Fatalf("could not greet: %v", err) + log.Fatalf("could not get Login: %v - %v", grpc.Code(err), grpc.ErrorDesc(err)) } log.Printf("Greeting: %s", lr.String()) +} +func GetHSVersions(conn *grpc.ClientConn) { vc := pb.NewVersionsServiceClient(conn) // Contact the server and print out its response. vr, err := vc.Versions(context.Background(), &pb.VersionRequest{Address: "matrix.org"}) if err != nil { - log.Fatalf("could not greet: %v", err) + log.Fatalf("could not get Version: %v - %v", grpc.Code(err), grpc.ErrorDesc(err)) } log.Println("Versions:")