Ejemplo n.º 1
0
        public async Task AuthenticateRequestAsync(HttpRequestMessage requestMessage)
        {
            var accessToken = await AcquireTokenAsync();

            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            if (accessToken != null)
            {
                AuthenticationSuccessful?.Invoke();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Hooks into EAC checks and exposes events for it;
        /// MUST be called after in GameStartDone or later
        /// </summary>
        public static void Init()
        {
            //if (_isInitialized)
            //    throw new InvalidOperationException(nameof(EacTools) + "." + nameof(Init) + " may only be called once.");

            Log.Debug("Hooking into the EAC response callbacks ...");

            if (EACServer.Instance == null)
            {
                throw new ApplicationException("Cannot activate EAC monitoring because EAC server is not (yet) started.");
            }

            var successDelegate = EACServer.Instance.GetSuccessDelegate();
            var kickDelegate    = EACServer.Instance.GetKickDelegate();

            if (successDelegate == null || kickDelegate == null)
            {
                throw new ApplicationException("Cannot activate EAC monitoring because success and kick delegates are not (yet) set.");
            }

            var kickDelegateNew = new KickPlayerDelegate(delegate(ClientInfo info, GameUtils.KickPlayerData data)
            {
                if (PersistentData.Instance.EacWhitelist.Contains(info.playerId))
                {
                    Log.Out($"EAC check failed but player \"{info.playerName}\" ({info.playerId}) is exempt from EAC kicks.");
                    // Call success delegate instead
                    successDelegate(info);
                }
                else
                {
                    // Let original kick delegate handle it
                    kickDelegate(info, data);
                    PlayerKicked?.Invoke(info, data);
                }
            });

            // Replace original kick delegate with our new modified one
            EACServer.Instance.SetKickDelegate(kickDelegateNew);

            var successDelegateNew = new AuthenticationSuccessfulCallbackDelegate(delegate(ClientInfo info)
            {
                // Let original kick delegate handle it
                successDelegate(info);
                AuthenticationSuccessful?.Invoke(info);
            });

            // Replace original success delegate with our new modified one
            EACServer.Instance.SetSuccessDelegate(successDelegateNew);

            //_isInitialized = true;
            Log.Debug("EAC monitoring activated.");
        }
        /// <summary>
        /// Handles the response to the <see cref="US.OpenServer.Protocols.WinAuth.WinAuthProtocolCommands.AUTHENTICATE"/>
        /// command packet.
        /// </summary>
        /// <remarks>
        /// <para>
        /// When an <see cref="US.OpenServer.Protocols.WinAuth.WinAuthProtocolCommands.AUTHENTICATED"/>
        /// response is received, notifies the <see cref="US.OpenServer.SessionBase"/> the
        /// client is authenticated enabling the session to allow execution of higher
        /// protocol layers. Finally, signals the <see cref="Authenticate(String, String, String)" />
        /// function to unblock the calling thread.
        /// </para>
        /// <para>
        /// When an <see cref="US.OpenServer.Protocols.WinAuth.WinAuthProtocolCommands.ACCESS_DENIED"/>
        /// response is received, logs the error then signals the <see cref="Authenticate(String, String, String)" />
        /// function to unblock the calling thread.
        /// </para>
        /// <para>
        /// When an <see cref="US.OpenServer.Protocols.WinAuth.WinAuthProtocolCommands.ERROR"/>
        /// response is received, logs the error then signals the <see cref="Authenticate(String, String, String)" />
        /// function to unblock the calling thread.
        /// </para>
        /// <para>
        /// Prior to authentication the session disallows all protocols commands.
        /// </para>
        /// </remarks>
        /// <param name="br">A BinaryReader that contains the command packet.</param>
        public override void OnPacketReceived(BinaryReader br)
        {
            lock (this)
            {
                if (Session == null)
                {
                    return;
                }

                WinAuthProtocolCommands command = (WinAuthProtocolCommands)br.ReadByte();
                switch (command)
                {
                case WinAuthProtocolCommands.AUTHENTICATED:
                    IsAuthenticated         = true;
                    Session.IsAuthenticated = true;

                    AuthenticationSuccessful?.Invoke(Session.Address, Session.UserName);

                    Log(Level.Info, "Authenticated.");
                    Monitor.PulseAll(this);
                    break;

                case WinAuthProtocolCommands.ACCESS_DENIED:

                    AuthenticationFailed?.Invoke(Session.Address, Session.UserName);

                    Log(Level.Notice, "Access denied.");
                    Monitor.PulseAll(this);
                    break;

                case WinAuthProtocolCommands.ERROR:
                {
                    string errorMessage = br.ReadString();

                    AuthenticationError?.Invoke(Session.Address, Session.UserName, errorMessage);

                    Log(Level.Notice, errorMessage);
                    Monitor.PulseAll(this);
                    break;
                }

                default:
                    Log(Level.Error, string.Format("Invalid or unsupported command.  Command: {0}", command));
                    break;
                }
            }
        }
        /// <summary>
        /// Handles the <see cref="US.OpenServer.Protocols.WinAuth.WinAuthProtocolCommands.AUTHENTICATE"/>
        /// command packet request.
        /// </summary>
        /// <param name="br">A BinaryReader that contains the command packet.</param>
        public override void OnPacketReceived(BinaryReader br)
        {
            lock (this)
            {
                if (Session == null)
                {
                    return;
                }

                WinAuthProtocolCommands command = (WinAuthProtocolCommands)br.ReadByte();
                try
                {
                    switch (command)
                    {
                    case WinAuthProtocolCommands.AUTHENTICATE:

                        string userName = br.ReadString();
                        string password = br.ReadString();
                        string domain   = br.ReadString();

                        try
                        {
                            // I commented ou this line because it constantly hindered my
                            // Authentication no matter what.
                            //wp = GetPrincipal(userName, password, domain);

                            if (!Authenticate(userName, password))
                            {
                                throw new Exception("Insufficient privileges.");
                            }

                            UserId          = userName;
                            UserName        = userName;
                            IsAuthenticated = true;

                            Session.UserName               = userName;
                            Session.IsAuthenticated        = true;
                            Session.AuthenticationProtocol = this;

                            Log(Level.Info, string.Format(@"Authenticated {0}\{1}.", domain, userName));

                            MemoryStream ms = new MemoryStream();
                            GetBinaryWriter(ms, WinAuthProtocolCommands.AUTHENTICATED);

                            //Fires the authentication succefull event
                            AuthenticationSuccessful?.Invoke(Session.Address, Session.UserName);

                            Session.Send(ms);
                        }
                        catch (Exception ex)
                        {
                            Log(Level.Notice, string.Format(@"Access denied.  {0}.  User: {1}\{2}", ex.Message, domain, userName));

                            MemoryStream ms = new MemoryStream();
                            GetBinaryWriter(ms, WinAuthProtocolCommands.ACCESS_DENIED);
                            Session.Send(ms);
                        }
                        break;

                    default:
                        throw new Exception("Invalid or unsupported command.");
                    }
                }
                catch (Exception ex)
                {
                    Log(Level.Error, string.Format("{0}  Command: {1}", ex.Message, command));

                    MemoryStream ms = new MemoryStream();
                    BinaryWriter bw = GetBinaryWriter(ms, WinAuthProtocolCommands.ERROR);
                    bw.Write(ex.Message);
                    Session.Send(ms);
                }
            }
        }
Ejemplo n.º 5
0
 public Task AuthenticateRequestAsync(HttpRequestMessage request)
 {
     AuthenticationSuccessful?.Invoke();
     return(Task.CompletedTask);
 }