public async override Task Invoke(IOwinContext context)
        {
            var request = context.Request;

            var header = request.Headers["Authorization"];

            var authtoken = request.Headers["X-Auth-Token"];

            if (!String.IsNullOrWhiteSpace(authtoken))
            {
                string token = Encoding.UTF8.GetString(Convert.FromBase64String(authtoken));
                var    parts = token.Split(':');

                string userName = parts[0];
                string password = parts[1];

                if (_apiClient.Account != null)
                {
                    _apiClient.WebApi.Logout();
                }

                await _apiClient.LoginAsync(new NetworkCredential(userName, password));

                var claims = new[]
                {
                    new Claim(ClaimTypes.Name, userName)
                };
                var identity = new ClaimsIdentity(claims, "Basic");

                request.User = new ClaimsPrincipal(identity);
            }

            if (!String.IsNullOrWhiteSpace(header))
            {
                var authHeader = System.Net.Http.Headers
                                 .AuthenticationHeaderValue.Parse(header);

                if ("Basic".Equals(authHeader.Scheme,
                                   StringComparison.OrdinalIgnoreCase))
                {
                    string parameter = Encoding.UTF8.GetString(
                        Convert.FromBase64String(
                            authHeader.Parameter));
                    var parts = parameter.Split(':');

                    string userName = parts[0];
                    string password = parts[1];

                    if (_apiClient.Account != null)
                    {
                        _apiClient.WebApi.Logout();
                    }

                    await _apiClient.LoginAsync(new NetworkCredential(userName, password));

                    var claims = new[]
                    {
                        new Claim(ClaimTypes.Name, userName)
                    };
                    var identity = new ClaimsIdentity(claims, "Basic");

                    request.User = new ClaimsPrincipal(identity);
                }
            }

            await Next.Invoke(context);
        }
        public async Task <TokenIssueResponse> IssueToken(TokenIssueRequest request)
        {
            // Login to CaaS
            IAccount account = await _computeClient.LoginAsync(
                new NetworkCredential(
                    request.Message.Credentials.UserName,
                    request.Message.Credentials.Password));

            // Get available clouds
            IEnumerable <DatacenterWithMaintenanceStatusType> dataCenters =
                await _computeClient.GetDataCentersWithMaintenanceStatuses();

            string loginToken = request.Message.Credentials.UserName + ":" + request.Message.Credentials.Password;

            byte[] buffer            = new byte[loginToken.Length];
            string loginTokenEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(loginToken));

            List <Endpoint> endPoints = new List <Endpoint>();

            endPoints.Add(new Endpoint()
            {
                Url         = ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName),
                Id          = "AU1",                // TODO: Map to cloud id?
                InternalURL = ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName),
                PublicURL   = ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName),
                Region      = "RegionOne"
            });
            foreach (var dataCenter in dataCenters)
            {
                endPoints.Add(new Endpoint()
                {
                    Url         = ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName),
                    Id          = dataCenter.location,            // TODO: Map to cloud id?
                    InternalURL = ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName),
                    PublicURL   = ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName),
                    Region      = "Dimension Data " + dataCenter.displayName
                });
            }

            TokenIssueResponse response = new TokenIssueResponse()
            {
                AccessToken = new AccessToken()
                {
                    Token   = new Token(request.Message.TenantName, request.Message.TenantName, loginTokenEncoded),
                    Catalog = new ServiceCatalogEntry[]
                    {
                        new ServiceCatalogEntry()
                        {
                            Endpoints      = endPoints.ToArray(),
                            EndpointsLinks = new string[]
                            {
                                ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName)
                            },
                            Name = "nova",
                            Type = EndpointType.compute
                        },
                        new ServiceCatalogEntry()
                        {
                            Endpoints      = endPoints.ToArray(),
                            EndpointsLinks = new string[]
                            {
                                ConfigurationHelpers.GetTenantUrl(Request.RequestUri.Host, request.Message.TenantName)
                            },
                            Name = "keystone",
                            Type = EndpointType.identity
                        }
                    },
                    User = new User()
                    {
                        Id         = Guid.NewGuid().ToString(),
                        Name       = account.FullName,
                        Roles      = new User.Role[] { },
                        RolesLinks = new string[] {},
                        UserName   = request.Message.Credentials.UserName
                    }
                }
            };

            return(response);
        }