Пример #1
0
        public async Task <AuthenticationResult> Authenticate(Dictionary <string, string> authenticationCtx, IUserService userService)
        {
            if (authenticationCtx["provider"] != PROVIDER_NAME)
            {
                return(null);
            }

            string username;

            authenticationCtx.TryGetValue("pseudo", out username);

            var pId = new PlatformId {
                Platform = PROVIDER_NAME
            };

            if (string.IsNullOrWhiteSpace(username))
            {
                return(AuthenticationResult.CreateFailure("Pseudo must not be empty", pId, authenticationCtx));
            }

            var user = await userService.GetUserByClaim(PROVIDER_NAME, "pseudo", username);

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

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

                var claim = new JObject();
                claim["pseudo"] = username;
                user            = await userService.AddAuthentication(user, PROVIDER_NAME, claim, username);
            }

            return(AuthenticationResult.CreateSuccess(user, pId, authenticationCtx));
        }
Пример #2
0
        public Task <AuthenticationResult> Authenticate(AuthenticationContext authenticationCtx, CancellationToken ct)
        {
            AuthenticationResult result;

            if (authenticationCtx.Parameters.TryGetValue("testkey", out var testValue) && testValue == "testvalue")
            {
                result = AuthenticationResult.CreateSuccess(new User {
                    Id = Guid.NewGuid().ToString()
                }, PlatformId.Unknown, authenticationCtx.Parameters);

                if (authenticationCtx.Parameters.TryGetValue("testrenewal", out var testRenewal))
                {
                    result.ExpirationDate = DateTime.Now + TimeSpan.Parse(testRenewal);
                    using (var sessionData = new MemoryStream())
                    {
                        _serializer.Serialize("initial", sessionData);
                        result.initialSessionData["testData"] = sessionData.ToArray();
                    }
                }
            }
            else
            {
                result = AuthenticationResult.CreateFailure("Invalid auth parameters, should be testkey=testvalue", PlatformId.Unknown, authenticationCtx.Parameters);
            }

            return(Task.FromResult(result));
        }
Пример #3
0
        public async Task <AuthenticationResult> Authenticate(AuthenticationContext authenticationCtx, CancellationToken ct)
        {
            var token    = authenticationCtx.Parameters["token"];
            var appInfos = await _env.GetApplicationInfos();

            try
            {
                var gameId = TokenGenerator.DecodeToken <string>(token, appInfos.PrimaryKey);


                return(AuthenticationResult.CreateSuccess(new User {
                    Id = "ds-" + gameId
                }, new PlatformId {
                    OnlineId = gameId, Platform = PROVIDER_NAME
                }, authenticationCtx.Parameters));
            }
            catch (Exception ex)
            {
                return(AuthenticationResult.CreateFailure("Invalid token :" + ex.ToString(), new PlatformId {
                    Platform = PROVIDER_NAME
                }, authenticationCtx.Parameters));
            }
        }
        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));
            }
        }