Esempio n. 1
0
        async Task <User> GetUserViaBasicAuthAsync()
        {
            var headers = _httpContextAccessor.HttpContext.Request.Headers;

            if (!headers.ContainsKey(HeaderName))
            {
                return(null);
            }

            var headerValue = string.Empty;

            if (headers[HeaderName].Count > 0)
            {
                headerValue = headers[HeaderName][0];
            }

            if (String.IsNullOrWhiteSpace(headerValue))
            {
                return(null);
            }

            var startIndex = headerValue.IndexOf(Token, StringComparison.InvariantCultureIgnoreCase);

            if (startIndex == -1)
            {
                return(null);
            }

            // ensure we have a credentials
            var credentials = headerValue.Substring(Token.Length);

            if (String.IsNullOrEmpty(credentials))
            {
                return(null);
            }

            var    separatorIndex = credentials.IndexOf(':');
            string appApiKey = null, userApiKey = null;

            if (separatorIndex >= 0)
            {
                appApiKey  = credentials.Substring(0, separatorIndex);
                userApiKey = credentials.Substring(separatorIndex + 1);
            }
            else
            {
                appApiKey = credentials;
            }

            // ensure we have a app API key
            if (String.IsNullOrEmpty(appApiKey))
            {
                return(null);
            }

            // Get site settings
            var settings = await _siteSettingsStore.GetAsync();

            if (settings == null)
            {
                return(null);
            }

            // Do the app keys match?
            if (!appApiKey.Equals(settings.ApiKey, StringComparison.InvariantCulture))
            {
                return(null);
            }

            // Do we have a user api key?
            if (String.IsNullOrEmpty(userApiKey))
            {
                return(null);
            }

            return(await _platoUserStore.GetByApiKeyAsync(userApiKey));
        }