Ejemplo n.º 1
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));
            }
        }