public ActionResult ValidateUser([FromBody] UserLogin request) { if (HttpContext.Request.Scheme != Uri.UriSchemeHttps) { Console.WriteLine("Warning: Request made from non-SSL origin! Please update this to use SSL as soon as it's available."); } MatrixLoginResponse ret = null; if (request.Password != null) { ret = _matrixAuthService.LoginWithPassword(request.Homeserver, request.UserName, request.Password); } else if (request.Token != null) { ret = _matrixAuthService.LoginWithToken(request.Homeserver, request.UserName, request.Token); } if (ret == null) { return(StatusCode(401)); } var response = new UserResponse { access_token = ret.access_token, user_id = ret.user_id, home_server = ret.home_server, refresh_token = ret.refresh_token }; return(Json(response)); }
private string LoginSync(string username, string password, bool saveToken = false) { string error = null; try { MatrixLoginResponse response = _cp.Client.LoginWithPassword(username, password); IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForAssembly(); if (saveToken) { SaveToken(response, _tokenPath, isoStore); } _cp.Client.StartSync(); } catch (MatrixException e) { error = e.Message; } catch (AggregateException e) { if (!IsWebException(e)) { throw e; } error = "Server connection failed"; } return(error); }
static void Main(string[] args) { string homeserverUrl = "https://matrix.org"; MatrixClient client; if (File.Exists("/tmp/mx_access")) { string[] tokens = File.ReadAllText("/tmp/mx_access.txt").Split("$"); client = new MatrixClient(homeserverUrl); client.UseExistingToken(tokens[1], tokens[0]); } else { string username = "******"; string password = "******"; client = new MatrixClient(homeserverUrl); MatrixLoginResponse login = client.LoginWithPassword(username, password); File.WriteAllText("/tmp/mx_access.txt", $"{login.access_token}${login.user_id}"); } Console.WriteLine("Starting sync"); client.StartSync(); Console.WriteLine("Finished initial sync"); foreach (var room in client.GetAllRooms()) { Console.WriteLine($"Found room: {room.ID}"); } }
public MatrixLoginResponse LoginWithPassword(string homeserver, string username, string password) { MatrixLoginResponse response = null; if (homeserver.Length > 0 && username.Length > 0) { if (_matrixClients == null) { _matrixClients = new Dictionary <string, MatrixClient>(); } if (!_matrixClients.Keys.Contains(username)) { try { _matrixClients[username] = new MatrixClient(homeserver); var passwordHash = password;//Utils.HashUtil.GetSha256FromString(password); //TODO: turn this back on response = _matrixClients[username].LoginWithPassword(username, passwordHash); } catch (Exception e) { response = null; Console.WriteLine($"Error encountered while password-logging in user {username}: {e.Message}"); _matrixClients.Remove(username); } } else { response = _matrixClients[username].GetCurrentLogin(); } } return(response); }
public void ClientLogin(MatrixLogin login) { JObject result; MatrixRequestError error = mbackend.Post("/_matrix/client/r0/login", false, JObject.FromObject(login), out result); if (error.IsOk) { current_login = result.ToObject <MatrixLoginResponse> (); SetLogin(current_login); } else { throw new MatrixException(error.ToString()); //TODO: Need a better exception } }
private void SaveToken(MatrixLoginResponse login, string path, IsolatedStorageFile store) { var tokenKey = new TokenKey(login.user_id, _cp.Url); if (_tokens.ContainsKey(tokenKey)) // Invariant: All tokens in file are loaded in dictionary -> this avoids duplicate lines in file { return; // Invariant: All tokens added to dictionary are immediately saved to file -> this doesn't prevent unsaved tokens } _tokens[tokenKey] = login.access_token; using (var isoStream = new IsolatedStorageFileStream(path, FileMode.Append, FileAccess.Write, store)) { using (var writer = new StreamWriter(isoStream)) { writer.WriteLine($"{login.access_token}${tokenKey.UserId}${tokenKey.Server}"); } } }
private RoomsViewModel GetUserRooms(User user) { string homeserverUrl = "https://matrix.org"; MatrixClient client; string username = user.Username; string password = user.Password; client = new MatrixClient(homeserverUrl); MatrixLoginResponse login = client.LoginWithPassword(username, password); client.StartSync(); RoomsViewModel rmv = new RoomsViewModel(); rmv.rooms = client.GetAllRooms(); return(rmv); }
public MatrixLoginResponse LoginWithToken(string homeserver, string username, string token) { MatrixLoginResponse response = null; if (homeserver.Length > 0 && username.Length > 0) { if (_matrixClients == null) { _matrixClients = new Dictionary <string, MatrixClient>(); } if (!_matrixClients.Keys.Contains(username)) { try { _matrixClients[username] = new MatrixClient(homeserver); _matrixClients[username].UseExistingToken(username, token); //_matrixClients[username].LoginWithToken(username, token); response = _matrixClients[username].GetCurrentLogin(); } catch (Exception e) { response = null; Console.WriteLine($"Error encountered while token-logging in user {username}: {e.Message}"); } } else { _matrixClients[username].UseExistingToken(username, token); response = _matrixClients[username].GetCurrentLogin(); } } return(response); }
public void SetLogin(MatrixLoginResponse response) { current_login = response; user_id = response.user_id; mbackend.SetAccessToken(response.access_token); }