Exemplo n.º 1
0
        /// <summary>
        /// Validate the current client.
        /// </summary>
        /// <param name="user">The current user principal.</param>
        /// <param name="authenticationSchemes">The authentication type.</param>
        /// <returns>True if the client has been validated; else false.</returns>
        public override bool ClientValidation(System.Security.Principal.IPrincipal user, Nequeo.Security.AuthenticationType authenticationSchemes)
        {
            // Does the user priciple exist.
            if (user != null)
            {
                // Does the user identity exist.
                if (user.Identity != null)
                {
                    // Select the curent Authentication Schemes
                    switch (authenticationSchemes)
                    {
                    case Nequeo.Security.AuthenticationType.User:
                    case Nequeo.Security.AuthenticationType.Basic:
                        // If the authentication type is 'Basic'

                        // If the identity is IIdentityMember.
                        if (user.Identity is Nequeo.Security.IdentityMember)
                        {
                            // The username and password are passed for
                            // Basic authentication type.
                            Nequeo.Security.IdentityMember identityMember = (Nequeo.Security.IdentityMember)user.Identity;

                            // Return the result of the authentication.
                            return(_provider.AuthenticateUser(identityMember.GetCredentials()));
                        }

                        // If the identity is HttpListenerBasicIdentity.
                        if (user.Identity is HttpListenerBasicIdentity)
                        {
                            // The username and password are passed for
                            // Basic authentication type.
                            HttpListenerBasicIdentity httpListenerBasicIdentity = (HttpListenerBasicIdentity)user.Identity;
                            string userName = httpListenerBasicIdentity.Name;
                            string password = httpListenerBasicIdentity.Password;

                            // Create the user credentials.
                            Nequeo.Security.UserCredentials credentials = new Nequeo.Security.UserCredentials();
                            credentials.Username = userName;
                            credentials.Password = password;

                            // Return the result of the authentication.
                            return(_provider.AuthenticateUser(credentials));
                        }
                        return(false);

                    case Nequeo.Security.AuthenticationType.Integrated:
                        // If the authentication type is WindowsIdentity
                        if (user.Identity is System.Security.Principal.WindowsIdentity)
                        {
                            WindowsIdentity windowsIdentity = (WindowsIdentity)user.Identity;
                            if (user.IsInRole("Administrators") || user.IsInRole("Users"))
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        break;

                    case Nequeo.Security.AuthenticationType.None:
                    case Nequeo.Security.AuthenticationType.Anonymous:
                        return(true);

                    default:
                        return(false);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set the request headers from the input stream within the current http context.
        /// </summary>
        /// <param name="webSocketContext">The current web socket context.</param>
        /// <param name="timeout">The maximum time in milliseconds to wait for the end of the header data; -1 wait Indefinitely.</param>
        /// <param name="maxReadLength">The maximun number of bytes to read before cancelling (must be greater then zero).</param>
        /// <param name="requestBufferStore">The request buffer store stream.</param>
        /// <exception cref="System.Exception"></exception>
        /// <returns>True if the headers have been found; else false.</returns>
        public static bool SetRequestHeaders(Nequeo.Net.WebSockets.WebSocketContext webSocketContext, long timeout = -1, int maxReadLength = 0, System.IO.Stream requestBufferStore = null)
        {
            // Header has not been found at this point.
            string requestMethod   = "";
            string protocolVersion = "";

            byte[]           rawData = null;
            List <NameValue> headers = null;

            // Web socket context is null.
            if (webSocketContext == null)
            {
                return(false);
            }

            // Web socket request context is null.
            if (webSocketContext.WebSocketRequest == null)
            {
                return(false);
            }

            // Web socket request context stream is null.
            if (webSocketContext.WebSocketRequest.Input == null)
            {
                return(false);
            }

            // If not using the buffer store.
            if (requestBufferStore == null)
            {
                // We need to wait until we get all the header
                // data then send the context to the server.
                headers = Nequeo.Net.Utility.
                          ParseHeaders(webSocketContext.WebSocketRequest.Input, out requestMethod, ref rawData, timeout, maxReadLength);
            }
            else
            {
                // We need to wait until we get all the header
                // data then send the context to the server.
                headers = Nequeo.Net.Utility.
                          ParseHeaders(webSocketContext.RequestBufferStore, out requestMethod, ref rawData, timeout, maxReadLength);
            }

            // If headers exist then all has been found.
            if (headers != null)
            {
                // Set all the request headers.
                webSocketContext.WebSocketRequest.ReadWebSocketHeaders(headers, requestMethod);
                protocolVersion = webSocketContext.WebSocketRequest.ProtocolVersion;
                webSocketContext.WebSocketRequest.HeadersFound = true;

                // If the client is using protocol version "HTTP/1.1"
                if (protocolVersion.ToUpper().Trim().Replace(" ", "").Contains("HTTP/1.1"))
                {
                    // Do nothing.
                }

                // If the client is using protocol version "HTTP/2.0".
                if (protocolVersion.ToUpper().Trim().Replace(" ", "").Contains("HTTP/2"))
                {
                    // Do nothing.
                }

                // Set the user principle if credentials
                // have been passed.
                if (webSocketContext.WebSocketRequest.Credentials != null)
                {
                    // Add the credentials.
                    Nequeo.Security.IdentityMember identity =
                        new Nequeo.Security.IdentityMember(
                            webSocketContext.WebSocketRequest.Credentials.UserName,
                            webSocketContext.WebSocketRequest.Credentials.Password,
                            webSocketContext.WebSocketRequest.Credentials.Domain);

                    Nequeo.Security.AuthenticationType authType = Nequeo.Security.AuthenticationType.None;
                    try
                    {
                        // Attempt to get the authentication type.
                        authType = (Nequeo.Security.AuthenticationType)
                                   Enum.Parse(typeof(Nequeo.Security.AuthenticationType), webSocketContext.WebSocketRequest.AuthorizationType);
                    }
                    catch { }

                    // Set the cuurent authentication schema.
                    identity.AuthenticationSchemes = authType;

                    // Create the principal.
                    Nequeo.Security.PrincipalMember principal = new Nequeo.Security.PrincipalMember(identity, null);

                    // Assign the principal
                    webSocketContext.User = principal;
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }