Exemplo n.º 1
0
 /// <summary>
 /// Validate the client. Client is only validated if IsAuthenticated = true; all other is false.
 /// Use the ClientAuthenticationValidationSelector delegate to over ride this behavior. Or override this
 /// method in a derived class to change the behavior
 /// </summary>
 /// <param name="user">Defines the basic functionality of a principal object.</param>
 /// <param name="authenticationSchemes">Specifies protocols for authentication.</param>
 /// <returns>True if the client has been validated; else false.</returns>
 public virtual Boolean ClientValidation(IPrincipal user, Nequeo.Security.AuthenticationType authenticationSchemes)
 {
     // Does the user priciple exist.
     if (user != null)
     {
         // Does the user identity exist.
         if (user.Identity != null)
         {
             // If the client was validated.
             if (!user.Identity.IsAuthenticated)
             {
                 return(false);
             }
             else
             {
                 if (_clientAuthenticationValidator != null)
                 {
                     // Custom client authentication validator.
                     return(_clientAuthenticationValidator(user, authenticationSchemes));
                 }
                 else
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Represents the Identity of a member.
 /// </summary>
 /// <param name="name">The unique identifier of the current user.</param>
 /// <param name="password">The password of the current user.</param>
 /// <param name="domain">The domain of the current user.</param>
 /// <param name="applicationName">The application name of the current user; else (All).</param>
 /// <param name="authenticationSchemes">The type of authentication schemes used.</param>
 public IdentityMember(string name, string password, string domain = "", string applicationName = "All",
                       Nequeo.Security.AuthenticationType authenticationSchemes = Nequeo.Security.AuthenticationType.None)
 {
     _uniqueIdentifier      = name;
     _password              = password;
     _domain                = domain;
     _applicationName       = applicationName;
     _authenticationSchemes = authenticationSchemes;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor with host attributes
 /// </summary>
 /// <param name="username">The username attribute.</param>
 /// <param name="password">The password attribute.</param>
 /// <param name="domain">The domain attribute.</param>
 /// <param name="authorisationType">The authorisationType attribute.</param>
 /// <param name="authenticationType">The authenticationType attribute.</param>
 /// <param name="applicationName">The application name attribute.</param>
 public UserCredentialsElement(String username, String password, String domain,
                               Nequeo.Security.AuthorisationType authorisationType,
                               Nequeo.Security.AuthenticationType authenticationType,
                               String applicationName)
 {
     Username           = username;
     Password           = password;
     Domain             = domain;
     AuthorisationType  = authorisationType;
     AuthenticationType = authenticationType;
     ApplicationName    = applicationName;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Represents the Identity of a member.
        /// </summary>
        /// <param name="name">The unique identifier of the current user.</param>
        /// <param name="password">The password of the current user.</param>
        /// <param name="domain">The domain of the current user.</param>
        /// <param name="applicationName">The application name of the current user; else (All).</param>
        /// <param name="authenticationSchemes">The type of authentication schemes used.</param>
        public IdentityMember(string name, string password, string domain = "", string applicationName = "All",
                              Nequeo.Security.AuthenticationType authenticationSchemes = Nequeo.Security.AuthenticationType.None)
        {
            _uniqueIdentifier      = name;
            _password              = password;
            _domain                = domain;
            _applicationName       = applicationName;
            _authenticationSchemes = authenticationSchemes;

            // Get the secure password.
            _passwordSecure = new Nequeo.Security.SecureText().GetSecureText(password);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get specifies protocols for authentication.
        /// </summary>
        /// <returns>Specifies protocols for authentication.</returns>
        public virtual Nequeo.Security.AuthenticationType AuthenticationSchemeForClient()
        {
            if (_clientAuthenticationSchemes != null)
            {
                _authenticationSchemes = _clientAuthenticationSchemes();

                // Return the schema.
                return(_authenticationSchemes);
            }
            else
            {
                // Return the schema.
                return(_authenticationSchemes);
            }
        }
Exemplo n.º 6
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.º 7
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);
            }
        }