protected bool AuthenticateImpl(HttpRequestMessage request)
        {
            if (m_authFlow != AuthFlow.Obtained)
            {
                if (!ExternalAuth)
                {
                    var                host          = m_authPath; //GetHost(request.RequestUri.AbsoluteUri);
                    var                authUriString = GetAuthUriString(host);
                    HttpClient         authClient    = new HttpClient();
                    HttpRequestMessage authReq       = new HttpRequestMessage()
                    {
                        RequestUri = new Uri(authUriString),
                        Method     = HttpMethod.Post
                    };
                    authReq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    List <KeyValuePair <string, string> > authReqContent = BuildAuthRequestContent();
                    authReq.Content = new FormUrlEncodedContent(authReqContent);
                    authReq.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

                    var authResponse = authClient.SendAsync(authReq).Result;
                    if (authResponse.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var content = authResponse.Content.ReadAsStringAsync().Result;
                        AuthDataDeserializeImpl(content);
                        m_authFlow = AuthFlow.Obtained;
                    }
                    else
                    {
                        var reason = SdkAuthException.Reason.Common;
                        ErrorImpl  = new SdkAuthException(reason, authResponse.ReasonPhrase);
                        m_authFlow = AuthFlow.ObtainAccessTokenPending;
                        return(false);
                    }
                }
                else
                {
                    m_authFlow = AuthFlow.ObtainAccessTokenPending;
                    return(false);
                }
            }
            else
            {
                if (IsAccessTokenExpired())
                {
                    if (ExternalAuth)
                    {
                        ErrorImpl = new SdkAuthException(SdkAuthException.Reason.TokenExpired, "Externally provided authorization token expired.");
                    }

                    m_authFlow = AuthFlow.ObtainAccessTokenPending;
                    return(false);
                }
            }
            return(true);
        }
Пример #2
0
        public bool Authenticate(HttpRequestMessage request)
        {
            bool res = AuthenticateImpl(request);

            if (!res)
            {
                if (m_authData != null && m_authData.HasError)
                {
                    ErrorImpl = new SdkAuthException(SdkAuthException.Reason.Common, $"{m_authData.Error}; \r\nDescription: {m_authData.ErrorDescription}");
                }
            }
            else
            {
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", m_authData.AccessToken);
            }
            return(res);
        }