Exemplo n.º 1
0
        public static HttpContent Create(OAuth20AuthenticationConfig oAuthConfig)
        {
            if (oAuthConfig.HttpRequestConfig == null)
            {
                throw new ArgumentNullException(nameof(oAuthConfig.HttpRequestConfig), $"You are attempting to create {AutomatonConstants.OAuth20.OAUTH_20} {nameof(HttpContent)} but the {nameof(oAuthConfig.HttpRequestConfig)} is null");
            }

            HttpContent httpContent = null;

            switch (oAuthConfig.HttpRequestConfig.ContentType)
            {
            case HttpRequestContentType.ApplicationFormUrlEncoded:
                httpContent = CreateFormUrlEncodedContent(oAuthConfig);
                break;

            default:
                break;
            }

            return(httpContent);
        }
Exemplo n.º 2
0
        public static async Task SignOut(OAuth20AuthenticationConfig authConfig, OAuth20AuthResponse authResponse)
        {
            HttpRequestMessage revocationRequest = new HttpRequestMessage
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(authConfig.HttpRequestConfig.RevocationUrl)
            };

            var formKeyValuePairs = OAuth20FormUrlEncodedFactory.CreateUsingRevocation(authConfig, authResponse);

            revocationRequest.Content = new FormUrlEncodedContent(formKeyValuePairs);

            AddDefaultHeaders(revocationRequest);

            var httpClient         = GetHttpClient();
            var revocationResponse = await httpClient.SendAsync(revocationRequest);

            var authResponseContent = await revocationResponse.Content.ReadAsStringAsync();

            if (revocationResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Trying to access resource: {revocationRequest.RequestUri}, HttpStatusCode: {revocationResponse.StatusCode}, Reason: {authResponseContent}, Configuration: {JsonConvert.SerializeObject(authConfig)}");
            }
        }
Exemplo n.º 3
0
        private static async Task <OAuth20AuthResponse> AuthenticateUsingOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig)
        {
            var authRequestContent = HttpOAuth20ContentFactory.Create(authConfig);

            HttpRequestMessage authRequest = new HttpRequestMessage
            {
                Content    = authRequestContent,
                Method     = HttpMethod.Post,
                RequestUri = new Uri(authConfig.HttpRequestConfig.AuthUrl),
            };

            authRequest.Headers.UserAgent.TryParseAdd("PostmanRuntime/7.26.8");

            authRequest.Headers.TryAddWithoutValidation("Accept", "*/*");
            authRequest.Headers.TryAddWithoutValidation("Cache-Control", "no-cache");
            authRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br");
            authRequest.Headers.TryAddWithoutValidation("Connection", "keep-alive");

            var handler = new HttpClientHandler();

            handler.AllowAutoRedirect = true;
            handler.UseProxy          = false;
            handler.CheckCertificateRevocationList = false;

            var httpClient   = new HttpClient(handler);
            var authResponse = await httpClient.SendAsync(authRequest);

            var authResponseContent = await authResponse.Content.ReadAsStringAsync();

            if (authResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Trying to access resource: {httpRequest.RequestUri}, HttpStatusCode: {authResponse.StatusCode}, Reason: {authResponseContent}, Configuration: {JsonConvert.SerializeObject(authConfig)}");
            }

            OAuth20AuthResponse oAuthTokens = JsonConvert.DeserializeObject <OAuth20AuthResponse>(authResponseContent);

            return(oAuthTokens);
        }
Exemplo n.º 4
0
        private static async Task <HttpRequestMessage> SetRequestWithOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig)
        {
            var oauthTokenResponse = await AuthenticateUsingOAuth20Async(httpRequest, authConfig);

            httpRequest.Headers.TryAddWithoutValidation($"{AutomatonConstants.OAuth20.AUTHORIZATION}", $"{AutomatonConstants.OAuth20.AUTHORIZATION_BEARER} {oauthTokenResponse.access_token}");

            return(httpRequest);
        }
Exemplo n.º 5
0
        private static async Task <OAuth20AuthResponse> AuthenticateUsingOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig)
        {
            var authRequestContent = HttpOAuth20ContentFactory.Create(authConfig);

            HttpRequestMessage authRequest = new HttpRequestMessage
            {
                Content    = authRequestContent,
                Method     = HttpMethod.Post,
                RequestUri = new Uri(authConfig.HttpRequestConfig.AuthUrl),
            };

            authRequest.Headers.UserAgent.TryParseAdd("PostmanRuntime/7.26.8");

            AddDefaultHeaders(authRequest);

            var httpClient   = GetHttpClient();
            var authResponse = await httpClient.SendAsync(authRequest);

            var authResponseContent = await authResponse.Content.ReadAsStringAsync();

            if (authResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Reason: {authResponseContent}, Trying to access resource: {httpRequest.RequestUri}, HttpStatusCode: {authResponse.StatusCode}, Configuration: {JsonConvert.SerializeObject(authConfig)}");
            }

            OAuth20AuthResponse oAuthTokens = JsonConvert.DeserializeObject <OAuth20AuthResponse>(authResponseContent);

            return(oAuthTokens);
        }
Exemplo n.º 6
0
        private static HttpContent CreateFormUrlEncodedContent(OAuth20AuthenticationConfig oAuthConfig)
        {
            var formKeyValuePairs = OAuth20FormUrlEncodedFactory.CreateUsingGrantType(oAuthConfig.Grant);

            return(new FormUrlEncodedContent(formKeyValuePairs));
        }
Exemplo n.º 7
0
        public static IEnumerable <KeyValuePair <string, string> > CreateUsingRevocation(OAuth20AuthenticationConfig authConfig, OAuth20AuthResponse authResponse)
        {
            var formKeyValuePairs = new List <KeyValuePair <string, string> >();

            ValidateDefaultFormValues(authConfig.Grant);

            formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.CLIENT_ID, authConfig.Grant.ClientId));
            formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.CLIENT_SECRET, authConfig.Grant.ClientSecret));

            formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.TOKEN, authResponse.refresh_token));
            formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.TOKEN_TYPE_HYNT, AutomatonConstants.OAuth20.TOKEN_TYPE_HYNT_REFRESH_TOKEN));

            return(formKeyValuePairs);
        }