private void AuthenticatorSceneFactory(ISceneHost scene) { scene.AddProcedure("login", async p => { try { //scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Logging in an user.", null); var accessor = scene.DependencyResolver.Resolve <Management.ManagementClientAccessor>(); var authenticationCtx = p.ReadObject <Dictionary <string, string> >(); //scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Authentication context read.", authenticationCtx); var result = new LoginResult(); var userService = scene.DependencyResolver.Resolve <IUserService>(); var userSessions = scene.DependencyResolver.Resolve <IUserSessions>(); var handled = false; foreach (var provider in _config.AuthenticationProviders) { var authResult = await provider.Authenticate(authenticationCtx, userService); if (authResult == null) { continue; } handled = true; if (authResult.Success) { //scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Authentication successful.", authResult); var oldPeer = await userSessions.GetPeer(authResult.AuthenticatedUser.Id); if (oldPeer != null) { await oldPeer.DisconnectFromServer("User connected elsewhere"); } await userSessions.Login(p.RemotePeer, authResult.AuthenticatedUser, authResult.PlatformId); result.Success = true; var client = await accessor.GetApplicationClient(); result.UserId = authResult.AuthenticatedUser.Id; result.Username = authResult.Username; break; } else { //scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Authentication failed.", authResult); result.ErrorMsg = authResult.ReasonMsg; break; } } if (!handled) { scene.DependencyResolver.Resolve <ILogger>().Log(LogLevel.Error, "UsersManagement.login", "Login failed: provider not found ", authenticationCtx); result.ErrorMsg = "No authentication provider able to handle these credentials were found."; } p.SendValue(result); if (!result.Success) { var _ = Task.Delay(2000).ContinueWith(t => p.RemotePeer.DisconnectFromServer("Authentication failed")); } } catch (Exception ex) { scene.DependencyResolver.Resolve <ILogger>().Log(LogLevel.Error, "UsersManagement.login", "an exception occurred while trying to log in a user", ex); throw; } }); //scene.AddController<GroupController>(); scene.AddController <SceneAuthorizationController>(); scene.Disconnected.Add(async args => { await scene.GetComponent <IUserSessions>().LogOut(args.Peer); }); scene.Starting.Add(_ => { foreach (var provider in _config.AuthenticationProviders) { provider.Initialize(scene); } return(Task.FromResult(true)); }); }
private void AuthenticatorSceneFactory(ISceneHost scene) { scene.AddProcedure("login", async p => { scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Logging in an user.", null); var accessor = scene.DependencyResolver.Resolve<Management.ManagementClientAccessor>(); var authenticationCtx = p.ReadObject<Dictionary<string, string>>(); var result = new LoginResult(); var userService = scene.DependencyResolver.Resolve<IUserService>(); foreach (var provider in _config.AuthenticationProviders) { var authResult = await provider.Authenticate(authenticationCtx, userService); if (authResult == null) { continue; } if (authResult.Success) { scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Authentication successful.", authResult); result.Success = true; var client = await accessor.GetApplicationClient(); result.Token = await client.CreateConnectionToken(_config.OnRedirect(authResult), _config.UserDataSelector(authResult)); userService.SetUid(p.RemotePeer, authResult.AuthenticatedId); break; } else { scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "Authentication failed.", authResult); result.ErrorMsg = authResult.ReasonMsg; break; } } if (!result.Success) { if (result.ErrorMsg == null) { result.ErrorMsg = "No authentication provider able to handle these credentials were found."; } } if (result.Success) { scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "User logged in.", null); } else { scene.GetComponent<ILogger>().Log(LogLevel.Trace, "user.login", "User failed to log in.", null); } p.SendValue(result); }); foreach (var provider in _config.AuthenticationProviders) { provider.AdjustScene(scene); } }