Esempio n. 1
0
        public async Task <AuthenticationResult> Authenticate(Dictionary <string, string> authenticationCtx, IUserService userService)
        {
            if (authenticationCtx["provider"] != Provider_Name)
            {
                return(null);
            }
            string secret;
            var    pId = new PlatformId {
                Platform = Provider_Name
            };

            if (!authenticationCtx.TryGetValue("secret", out secret) || string.IsNullOrWhiteSpace(secret))
            {
                return(AuthenticationResult.CreateFailure("Missing impersonation secret.", pId, authenticationCtx));
            }

            if (secret != _secret)
            {
                return(AuthenticationResult.CreateFailure("Invalid impersonation secret.", pId, authenticationCtx));
            }

            string ImpersonatingProvider;
            string ImpersonatingClaimPath;
            string ImpersonatingClaimValue;

            if (!authenticationCtx.TryGetValue("impersonated-provider", out ImpersonatingProvider) || string.IsNullOrWhiteSpace(ImpersonatingProvider))
            {
                return(AuthenticationResult.CreateFailure("'impersonated-provider' must not be empty.", pId, authenticationCtx));
            }

            if (!authenticationCtx.TryGetValue("claimPath", out ImpersonatingClaimPath) || string.IsNullOrWhiteSpace(ImpersonatingClaimPath))
            {
                return(AuthenticationResult.CreateFailure("'claimPath' must not be empty.", pId, authenticationCtx));
            }
            if (!authenticationCtx.TryGetValue("claimValue", out ImpersonatingClaimValue) || string.IsNullOrWhiteSpace(ImpersonatingClaimValue))
            {
                return(AuthenticationResult.CreateFailure("'claimValue' must not be empty.", pId, authenticationCtx));
            }
            var user = await userService.GetUserByClaim(ImpersonatingProvider, ImpersonatingClaimPath, ImpersonatingClaimValue);

            if (user == null)
            {
                return(AuthenticationResult.CreateFailure($"The user '{ImpersonatingProvider}/{ImpersonatingClaimPath} = {ImpersonatingClaimValue}' does not exist.", pId, authenticationCtx));
            }
            else
            {
                return(AuthenticationResult.CreateSuccess(user, new PlatformId {
                    Platform = ImpersonatingProvider, OnlineId = ImpersonatingClaimValue
                }, authenticationCtx));
            }
        }
        public async Task <AuthenticationResult> Authenticate(Dictionary <string, string> authenticationCtx, IUserService userService)
        {
            if (authenticationCtx["provider"] != PROVIDER_NAME)
            {
                return(null);
            }

            string ticket;
            var    pId = new PlatformId {
                Platform = PROVIDER_NAME
            };

            if (!_enabled)
            {
                return(AuthenticationResult.CreateFailure("Provider disabled", pId, authenticationCtx));
            }

            if (!authenticationCtx.TryGetValue("login", out ticket) || string.IsNullOrWhiteSpace(ticket))
            {
                return(AuthenticationResult.CreateFailure("'login' field cannot be empty.", pId, authenticationCtx));
            }
            try
            {
                if (_blackList.Contains(ticket))
                {
                    return(AuthenticationResult.CreateFailure("Login blacklisted.", pId, authenticationCtx));
                }
                else
                {
                    var user = await userService.GetUserByClaim(PROVIDER_NAME, ClaimPath, ticket);

                    if (user == null)
                    {
                        var uid = Guid.NewGuid().ToString("N");

                        user = await userService.CreateUser(uid, JObject.FromObject(new { login = ticket, pseudo = ticket }));

                        var claim = new JObject();
                        claim[ClaimPath] = ticket;
                        user             = await userService.AddAuthentication(user, PROVIDER_NAME, claim, ticket);
                    }
                    return(AuthenticationResult.CreateSuccess(user, pId, authenticationCtx));
                }
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Debug, "authenticator.test", $"Test authentication failed. Ticket : {ticket}", ex);
                return(AuthenticationResult.CreateFailure($"Test auth failed.", pId, authenticationCtx));
            }
        }
Esempio n. 3
0
        public async Task <AuthenticationResult> Authenticate(Dictionary <string, string> authenticationCtx, IUserService _userService)
        {
            if (authenticationCtx["provider"] != PROVIDER_NAME)
            {
                return(null);
            }

            string login;

            authenticationCtx.TryGetValue("login", out login);

            string password;

            authenticationCtx.TryGetValue("password", out password);

            var pId = new PlatformId {
                Platform = PROVIDER_NAME
            };

            if (string.IsNullOrWhiteSpace(login) || string.IsNullOrWhiteSpace(password))
            {
                return(AuthenticationResult.CreateFailure("Login and password must be non empty.", pId, authenticationCtx));
            }

            var user = await _userService.GetUserByClaim(PROVIDER_NAME, "login", login);

            if (user == null)
            {
                return(AuthenticationResult.CreateFailure("No user found that matches the provided login/password.", pId, authenticationCtx));
            }

            dynamic authData = user.Auth[PROVIDER_NAME];

            string salt = authData.salt;
            string hash = authData.password;

            var candidateHash = HashPassword(password, salt);

            if (hash != candidateHash)
            {
                return(AuthenticationResult.CreateFailure("No user found that matches the provided login/password.", pId, authenticationCtx));
            }

            await _userService.UpdateCommunicationChannel(user.Id, "email", JObject.FromObject(new { value = (string)authData.email }));

            pId.OnlineId = user.Id;
            return(AuthenticationResult.CreateSuccess(user, pId, authenticationCtx));
        }
Esempio n. 4
0
        public async Task Login(IScenePeerClient peer, User user, PlatformId onlineId)
        {
            if (peer == null)
            {
                throw new ArgumentNullException("peer");
            }
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            bool added = false;

            while (!added)
            {
                var r = await _userPeerIndex.GetOrAdd(user.Id, peer.Id);

                if (r.Value != peer.Id)
                {
                    await LogOut(r.Value);
                }
                else
                {
                    added = true;
                }
            }


            await _userPeerIndex.TryAdd(onlineId.ToString(), peer.Id);

            await _peerUserIndex.TryAdd(peer.Id.ToString(), new Session { User = user, platformId = onlineId });

            await _eventHandlers.RunEventHandler(h => h.OnLoggedIn(peer, user, onlineId), ex => logger.Log(LogLevel.Error, "usersessions", "An error occured while running LoggedIn event handlers", ex));

            await _userService.LoginEventOccured(user, peer);

            //logger.Trace("usersessions", $"Added '{user.Id}' (peer : '{peer.Id}') in scene '{_scene.Id}'.");
        }
 public async Task OnLoggedIn(IScenePeerClient client, User user, PlatformId platformId)
 {
     await Task.FromResult(true);
 }
Esempio n. 6
0
 public static AuthenticationResult CreateFailure(string reason, PlatformId platformId, Dictionary <string, string> context)
 {
     return(new AuthenticationResult {
         Success = false, ReasonMsg = reason, PlatformId = platformId, AuthenticationContext = context
     });
 }
Esempio n. 7
0
 public static AuthenticationResult CreateSuccess(User user, PlatformId platformId, Dictionary <string, string> context)
 {
     return(new AuthenticationResult {
         Success = true, AuthenticatedUser = user, PlatformId = platformId, AuthenticationContext = context
     });
 }
Esempio n. 8
0
        public async Task <AuthenticationResult> Authenticate(Dictionary <string, string> authenticationCtx, IUserService userService)
        {
            if (authenticationCtx["provider"] != PROVIDER_NAME)
            {
                return(null);
            }

            string ticket;
            var    pId = new PlatformId {
                Platform = PROVIDER_NAME
            };

            if (!authenticationCtx.TryGetValue("ticket", out ticket) || string.IsNullOrWhiteSpace(ticket))
            {
                return(AuthenticationResult.CreateFailure("Steam session ticket must not be empty.", pId, authenticationCtx));
            }
            try
            {
                var steamId = await _authenticator.AuthenticateUserTicket(ticket);

                if (!steamId.HasValue)
                {
                    return(AuthenticationResult.CreateFailure("Invalid steam session ticket.", pId, authenticationCtx));
                }
                pId.OnlineId = steamId.ToString();

                if (_vacEnabled)
                {
                    AuthenticationResult result = null;
                    string vacSessionId         = null;
                    try
                    {
                        vacSessionId = await _steamService.OpenVACSession(steamId.Value.ToString());

                        _vacSessions[steamId.Value] = vacSessionId;
                    }
                    catch (Exception ex)
                    {
                        _logger.Log(LogLevel.Error, "authenticator.steam", $"Failed to start VAC session for {steamId}", ex);
                        result = AuthenticationResult.CreateFailure($"Failed to start VAC session.", pId, authenticationCtx);
                    }

                    try
                    {
                        if (!await _steamService.RequestVACStatusForUser(steamId.Value.ToString(), vacSessionId))
                        {
                            result = AuthenticationResult.CreateFailure($"Connection refused by VAC.", pId, authenticationCtx);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Log(LogLevel.Error, "authenticator.steam", $"Failed to check VAC status for  {steamId}", ex);
                        result = AuthenticationResult.CreateFailure($"Failed to check VAC status for user.", pId, authenticationCtx);
                    }

                    if (result != null)//Failed
                    {
                        if (_vacSessions.TryRemove(steamId.Value, out vacSessionId))
                        {
                            try
                            {
                                await _steamService.CloseVACSession(steamId.ToString(), vacSessionId);
                            }
                            catch (Exception ex)
                            {
                                _logger.Log(LogLevel.Error, $"authenticator.steam", $"Failed to close vac session for user '{steamId}'", ex);
                            }
                        }
                        return(result);
                    }
                }
                var steamIdString = steamId.GetValueOrDefault().ToString();
                var user          = await userService.GetUserByClaim(PROVIDER_NAME, ClaimPath, steamIdString);

                var playerSummary = await _steamService.GetPlayerSummary(steamId.Value);

                if (user == null)
                {
                    var uid = Guid.NewGuid().ToString("N");

                    user = await userService.CreateUser(uid, JObject.FromObject(new { steamid = steamIdString, pseudo = playerSummary.personaname, avatar = playerSummary.avatarfull }));

                    var claim = new JObject();
                    claim[ClaimPath] = steamIdString;
                    user             = await userService.AddAuthentication(user, PROVIDER_NAME, claim, steamIdString);
                }
                else
                {
                    user.UserData["pseudo"] = playerSummary.personaname;
                    user.UserData["avatar"] = playerSummary.avatarfull;
                    await userService.UpdateUserData(user.Id, user.UserData);
                }

                return(AuthenticationResult.CreateSuccess(user, pId, authenticationCtx));
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Debug, "authenticator.steam", $"Steam authentication failed. Ticket : {ticket}", ex);
                return(AuthenticationResult.CreateFailure($"Invalid steam session ticket.", pId, authenticationCtx));
            }
        }
Esempio n. 9
0
 public Task OnLoggedIn(IScenePeerClient client, User user, PlatformId platformId)
 {
     return(Task.FromResult(true));
 }
Esempio n. 10
0
 public Task <Session> GetSession(PlatformId id)
 {
     return(GetSession(id.ToString()));
 }