public IFacade GetFacade(ILoginToken loginToken)
        {
            string a = "";

            if (loginToken != null)
            {
                a = loginToken.GetType().GenericTypeArguments[0].Name;
            }
            if (a == "Administrator")
            {
                return(new LoggedInAdministratorFacade());
            }
            else if (a == "AirlineCompany")
            {
                return(new LoggedInAirlineFacade());
            }
            else if (a == "Customer")
            {
                return(new LoggedInCustomerFacade());
            }
            else // IloginToken is null - > user is Anonymous
            {
                return(new AnonymousUserFacade());
            }
        }
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,
                                                                              "You must enter user name + password");
            }
            else
            {
                string authenticationToken = actionContext.Request.Headers.Authorization.Parameter;

                string decodedAuthenticationToken = Encoding.UTF8.GetString(
                    Convert.FromBase64String(authenticationToken));

                string[] usernamePasswordArray = decodedAuthenticationToken.Split(':');
                string   username = usernamePasswordArray[0];
                string   password = usernamePasswordArray[1];

                FlyingCenterSystem fcs        = FlyingCenterSystem.GetInstance();
                ILoginToken        loginToken = fcs.Login(username, password);
                FacadeBase         facade     = fcs.GetFacade(loginToken);
                if (loginToken.GetType() == typeof(LoginToken <Administrator>))
                {
                    // LoginToken<Administrator> token = (LoginToken<Administrator>)loginToken;
                    // LoggedInAdministratorFacade LogFacade = (LoggedInAdministratorFacade)facade;
                    actionContext.Request.Properties["AdminUser"]   = loginToken;
                    actionContext.Request.Properties["AdminFacade"] = facade;
                }
                else if (loginToken.GetType() == typeof(LoginToken <AirlineCompany>))
                {
                    actionContext.Request.Properties["AirlineUser"]   = loginToken;
                    actionContext.Request.Properties["AirlineFacade"] = facade;
                }
                else if (loginToken.GetType() == typeof(LoginToken <Customer>))
                {
                    actionContext.Request.Properties["CustomerUser"]   = loginToken;
                    actionContext.Request.Properties["CustomerFacade"] = facade;
                }

                else
                {
                    actionContext.Response = actionContext.Request
                                             .CreateResponse(HttpStatusCode.Unauthorized, "You are not allowed");
                }
            }
        }
 public FacadeBase GetFacade(ILoginToken token)
 {
     if (token == null)
     {
         return(new AnonymousUserFacade());
     }
     if (token.GetType() == typeof(LoginToken <Administrator>))
     {
         return(new LoggedInAdministratorFacade());
     }
     if (token.GetType() == typeof(LoginToken <Customer>))
     {
         return(new LoggedInCustomerFacade());
     }
     if (token.GetType() == typeof(LoginToken <AirlineCompany>))
     {
         return(new LoggedInAirlineFacade());
     }
     return(new AnonymousUserFacade());
 }
Ejemplo n.º 4
0
        public IFacade GetFacade(ILoginToken loginToken)
        {
            // IloginToken is null - > user is Anonymous
            if (loginToken == null)
            {
                return(new AnonymousUserFacade());
            }

            if (loginToken.GetType() == typeof(LoginToken <Administrator>))
            {
                return(new LoggedInAdministratorFacade());
            }
            if (loginToken.GetType() == typeof(LoginToken <AirLineCompany>))
            {
                return(new LoggedInAirlineFacade());
            }
            if (loginToken.GetType() == typeof(LoginToken <Customer>))
            {
                return(new LoggedInCustomerFacade());
            }
            // if no other option user is Anonymous
            return(new AnonymousUserFacade());
        }
Ejemplo n.º 5
0
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,
                                                                              "You must enter user name + password");
            }
            else
            {
                string authenticationToken = actionContext.Request.Headers.Authorization.Parameter;

                string decodedAuthenticationToken = Encoding.UTF8.GetString(
                    Convert.FromBase64String(authenticationToken));

                string[] usernamePasswordArray = decodedAuthenticationToken.Split(':');
                string   username = usernamePasswordArray[0];
                string   password = usernamePasswordArray[1];


                FlyingCenterSystem fcs = FlyingCenterSystem.GetInstance();
                try
                {
                    ILoginToken loginToken = fcs.Login(username, password);
                    IFacade     facade     = fcs.GetFacade(loginToken);

                    if (loginToken.GetType() == typeof(LoginToken <Administrator>))
                    {
                        LoginToken <Administrator>  token     = (LoginToken <Administrator>)loginToken;
                        LoggedInAdministratorFacade LogFacade = (LoggedInAdministratorFacade)facade;
                        actionContext.Request.Properties["AdminUser"]   = token;
                        actionContext.Request.Properties["AdminFacade"] = LogFacade;
                    }
                    else if (loginToken.GetType() == typeof(LoginToken <Customer>))
                    {
                        LoginToken <Customer>  token     = (LoginToken <Customer>)loginToken;
                        LoggedInCustomerFacade LogFacade = (LoggedInCustomerFacade)facade;
                        actionContext.Request.Properties["CustomerUser"]   = token;
                        actionContext.Request.Properties["CustomerFacade"] = LogFacade;
                    }
                    else if (loginToken.GetType() == typeof(LoginToken <AirLineCompany>))
                    {
                        LoginToken <AirLineCompany> token     = (LoginToken <AirLineCompany>)loginToken;
                        LoggedInAirlineFacade       LogFacade = (LoggedInAirlineFacade)facade;
                        actionContext.Request.Properties["AirlineUser"]   = token;
                        actionContext.Request.Properties["AirlineFacade"] = LogFacade;
                    }
                    userIsBlocked = 0;
                }
                catch (Exception e)
                {
                    userIsBlocked++;
                    if (userIsBlocked == 3)
                    {
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, $"you blocked!");
                    }
                    else
                    {
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, $"{e.Message}");
                    }
                }
            }
        }