Ejemplo n.º 1
0
        Authenticate(Simias.Storage.Domain Domain, HttpContext HttpCtx)
        {
            Simias.Authentication.Status authStatus;

            log.Debug("Authenticate called");

            try
            {
                // Check for an authorization header.
                string[] encodedCredentials = HttpCtx.Request.Headers.GetValues("Authorization");
                if ((encodedCredentials != null) && (encodedCredentials[0] != null))
                {
                    // Get the basic encoding type from the http header.
                    string[] encodingName = HttpCtx.Request.Headers.GetValues("Basic-Encoding");
                    if ((encodingName == null) || (encodingName[0] == null))
                    {
                        // Use the specified default encoding.
                        encodingName = new string[] { defaultBasicEncodingName };
                    }
                    // Get the credentials from the auth header.
                    SimiasCredentials creds = new SimiasCredentials();
                    if (creds.AuthorizationHeaderToCredentials(encodedCredentials[0], encodingName[0]))
                    {
                        // Valid credentials?
                        if ((creds.Username != null) && (creds.Password != null))
                        {
                            // Only support basic.
                            if (creds.AuthType == "basic")
                            {
                                Member member = Domain.GetMemberByName(creds.Username);
                                if (member == null)
                                {
                                    member = Domain.GetMemberByDN(creds.Username);
                                }
                                if (member == null)
                                {
                                    member = Domain.GetMemberByOldName(creds.Username);
                                    if (member != null)
                                    {
                                        creds.Username = member.Name;                                         // give new username for e-dir auth
                                    }
                                }

                                if (member != null)
                                {
                                    if (Domain.IsLoginDisabled(member.UserID) != true)
                                    {
                                        try
                                        {
                                            // Authenticate the user.
                                            authStatus = AuthenticateByName(Domain.ID, creds.Username, creds.Password);
                                            HostNode hNode = HostNode.GetLocalHost();
                                            if (hNode.IsMasterHost != true)
                                            {
                                                for (int i = 0; i < 10; i++)
                                                {
                                                    log.Debug("System Sync Status : " + Domain.SystemSyncStatus.ToString());
                                                    if ((Domain.SystemSyncStatus &
                                                         (ulong)CollectionSyncClient.StateMap.CatalogSyncOnce) ==
                                                        (ulong)CollectionSyncClient.StateMap.CatalogSyncOnce ||
                                                        (CollectionSyncClient.ServerSyncStatus &
                                                         CollectionSyncClient.StateMap.CatalogSyncOnce) ==
                                                        CollectionSyncClient.StateMap.CatalogSyncOnce)
                                                    {
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        syncEvent.WaitOne(5000, false);
                                                    }

                                                    if (i == 9)
                                                    {
                                                        authStatus = new Simias.Authentication.Status(SCodes.InvalidCredentials);
                                                    }
                                                }
                                            }

                                            HostNode mNode = member.HomeServer;
                                            log.Debug("id.Auth : localhost userid  is :" + hNode.UserID);
                                            Http.UserMoved = 0;
                                            if (mNode != null)
                                            {
                                                log.Debug("id.Auth : member's home server userid is :" + mNode.UserID);
                                                if (hNode.UserID != mNode.UserID)
                                                {
                                                    log.Debug("id.Aith : sending useralreadymoved status back to client");
                                                    Http.UserMoved = 1;
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            log.Error(e.Message);
                                            log.Error(e.StackTrace);
                                            authStatus = new Simias.Authentication.Status(SCodes.InternalException);
                                            authStatus.ExceptionMessage = e.Message;
                                        }
                                    }
                                    else
                                    {
                                        log.Debug("Login is disabled for user " + creds.Username);
                                        authStatus = new Simias.Authentication.Status(SCodes.SimiasLoginDisabled);
                                    }
                                }
                                else
                                {
                                    log.Debug(creds.Username + " is not member of simias");
                                    authStatus = new Simias.Authentication.Status(SCodes.InvalidCredentials);
                                }
                            }
                            else
                            {
                                authStatus = new Simias.Authentication.Status(SCodes.MethodNotSupported);
                            }
                        }
                        else
                        {
                            authStatus = new Simias.Authentication.Status(SCodes.InvalidCredentials);
                        }
                    }
                    else
                    {
                        authStatus = new Simias.Authentication.Status(SCodes.InvalidCredentials);
                    }
                }
                else
                {
                    authStatus = new Simias.Authentication.Status(SCodes.InvalidCredentials);
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Error(e.StackTrace);
                authStatus = new Simias.Authentication.Status(SCodes.InternalException);
                authStatus.ExceptionMessage = e.Message;
            }
            return(authStatus);
        }