Esempio n. 1
0
 public async Task <LoginResult> Login(AuthParameters parameters)
 {
     return(await _auth.Login(parameters, this.Request.RemotePeer, this.Request.CancellationToken));
 }
        public async Task <LoginResult> Login(AuthParameters auth, IScenePeerClient peer, CancellationToken ct)
        {
            var sessions = _sessions as UserSessions;

            if (sessions == null)
            {
                throw new InvalidOperationException("Cannot call login on another scene than the authenticator scene");
            }
            _logger.Log(LogLevel.Debug, "user.login", "AuthenticationService login", auth);

            var result  = new LoginResult();
            var session = await sessions.GetSessionRecordById(peer.SessionId);

            var authenticationCtx = new AuthenticationContext(auth.Parameters, peer, session?.CreateView());
            var validationCtx     = new LoggingInCtx {
                AuthCtx = authenticationCtx, Type = auth.Type
            };

            await _handlers().RunEventHandler(h => h.OnLoggingIn(validationCtx), ex => _logger.Log(LogLevel.Error, "user.login", "An error occured while running Validate event handler", ex));


            if (!validationCtx.HasError)
            {
                AuthenticationResult authResult = null;
                var provider = GetProviders().FirstOrDefault(p => p.Type == auth.Type);
                if (provider == null)
                {
                    throw new ClientException($"authentication.notSupported?type={auth.Type}");
                }

                authResult = await provider.Authenticate(authenticationCtx, ct);


                if (authResult.Success)
                {
                    _logger.Log(LogLevel.Trace, "user.login", "Authentication successful.", authResult);
                    if (authResult.AuthenticatedUser != null)
                    {
                        var oldPeer = await _sessions.GetPeer(authResult.AuthenticatedUser.Id);

                        if (oldPeer != null && oldPeer.SessionId != peer.SessionId)
                        {
                            try
                            {
                                await sessions.LogOut(oldPeer);

                                await oldPeer.DisconnectFromServer("auth.login.new_connection");
                            }
                            catch (Exception)
                            {
                            }

                            await sessions.Login(peer, authResult.AuthenticatedUser, authResult.PlatformId, authResult.initialSessionData);
                        }
                        if (oldPeer == null)
                        {
                            await sessions.Login(peer, authResult.AuthenticatedUser, authResult.PlatformId, authResult.initialSessionData);
                        }
                    }

                    await sessions.Login(peer, authResult.AuthenticatedUser, authResult.PlatformId, authResult.initialSessionData);



                    await sessions.UpdateSession(peer.SessionId, s =>
                    {
                        s.Authentications[provider.Type] = authResult.PlatformId.ToString();
                        if (authResult.ExpirationDate.HasValue)
                        {
                            s.AuthenticationExpirationDates[provider.Type] = authResult.ExpirationDate.Value;
                        }

                        authResult.OnSessionUpdated?.Invoke(s);
                        return(Task.FromResult(s));
                    });

                    result.Success  = true;
                    result.UserId   = authResult?.AuthenticatedUser?.Id;
                    result.Username = authResult.Username;
                    session         = await sessions.GetSessionRecordById(peer.SessionId);

                    result.Authentications = session.Authentications.ToDictionary(entry => entry.Key, entry => entry.Value);
                    var ctx = new LoggedInCtx {
                        Result = result, Session = session
                    };
                    await _handlers().RunEventHandler(h => h.OnLoggedIn(ctx), ex => _logger.Log(LogLevel.Error, "user.login", "An error occured while running OnLoggedIn event handler", ex));
                }
                else
                {
                    //_logger.Log(LogLevel.Warn, "user.login", $"Authentication failed, reason: {authResult.ReasonMsg}", authResult);

                    result.ErrorMsg = authResult.ReasonMsg;
                }
            }
            else
            {
                //throw new ClientException(validationCtx.Reason);
                result.Success  = false; // currently doesnt return error to client with verison mismatch reason
                result.ErrorMsg = validationCtx.Reason;
                //return result;
            }

            if (!result.Success && session == null)
            {
                // FIXME: Temporary workaround to issue where disconnections cause large increases in CPU/Memory usage
                var _ = Task.Delay(1000).ContinueWith(t => peer.DisconnectFromServer(result.ErrorMsg));
            }

            return(result);
        }
Esempio n. 3
0
 public async Task Register(AuthParameters ctx)
 {
     await _auth.SetupAuth(ctx);
 }