Exemplo n.º 1
0
        public async Task <RegistrationResult> Register(string username, string password)
        {
            var result = await Post.To("/_matrix/client/r0/register")
                         .Using(RestClient)
                         .AddBody(new RegistrationRequest
            {
                Username           = username,
                Password           = password,
                AuthenticationData = new AuthenticationData
                {
                    Type = "m.login.dummy"
                },
                InhibitLogin             = true,
                DeviceId                 = null,
                InitialDeviceDisplayName = null
            })
                         .On(400, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .On(401, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .On(403, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .On(429, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .WhenSucceeded(new Action <RegistrationResult>(r =>
            {
                AccountRegistered?.Invoke(this, new RegistrationEventArgs(MxId.Parse(r.UserId)));
            }))
                         .Execute <RegistrationResult>();

            return(result);
        }
Exemplo n.º 2
0
        public async Task <LoginResult> Login(string username, string password, string deviceId = null,
                                              string initialDisplayName = "TensorMatrix")
        {
            var methodQueryResult = await QueryLoginMethods();

            if (!methodQueryResult.SupportedMethods.Any(x => x.Type == "m.login.password"))
            {
                throw new UnsupportedLoginMethodException("Homeserver does not support password-based login.");
            }

            var result = await Post.To("/_matrix/client/r0/login")
                         .Using(RestClient)
                         .AddBody(new PasswordLoginRequest <MxidIdentifier>
            {
                Identifier = new MxidIdentifier {
                    User = username
                },
                DeviceId = deviceId,
                InitialDeviceDisplayName = initialDisplayName,
                Password = password
            })
                         .On(400, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .On(403, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .On(429, (req, res, err) => ProtocolError?.Invoke(this, new ErrorEventArgs(err, res)))
                         .WhenSucceeded(new Action <LoginResult>(r =>
            {
                MxId                     = MxId.Parse(r.UserId);
                JwtAuthenticator         = new JwtAuthenticator(r.AccessToken);
                RestClient.Authenticator = JwtAuthenticator;
                DeviceId                 = r.DeviceId;

                if (r.WellKnown != null)
                {
                    RestClient.BaseUrl = new Uri(r.WellKnown.HomeServer.BaseUrl);
                }

                LoggedIn?.Invoke(this, new LoginEventArgs(MxId, r.AccessToken, r.DeviceId));
            }))
                         .Execute <LoginResult>();

            return(result);
        }