Beispiel #1
0
        public static Task <RequestTokenResponseData> RequestAccessTokenAnonymouslyAsync(Configuration config, MASDevice device)
        {
            var url = config.GetEndpointPath(config.OAuth.SystemEndpoints.Token);

            var headers = new Dictionary <string, string>
            {
                { HttpHeaders.Authorization, device.AuthHeaderValue },
                { "mag-identifier", device.MagId },
                { HttpHeaders.ContentType, HttpContentTypes.UrlEncoded },
                { HttpHeaders.Accept, HttpContentTypes.Json }
            };

            var scope = config.DefaultClientId.Scope;

            var builder = new HttpUrlBuilder();

            builder.Add("scope", "openid msso phone profile address email");
            //builder.Add("client_id", device.ClientId);
            //builder.Add("client_secret", device.ClientSecret);
            builder.Add("grant_type", "client_credentials");

            return(HttpRequestFactory.RequestAsync <RequestTokenResponseData>(new HttpRequestInfo()
            {
                Method = HttpMethod.POST,
                Url = url,
                Headers = headers,
                Body = builder.ToString()
            }));
        }
Beispiel #2
0
        public static Task <RequestTokenResponseData> RefreshAccessTokenAsync(Configuration config, MASDevice device, string refreshToken)
        {
            var url     = config.GetEndpointPath(config.OAuth.SystemEndpoints.Token);
            var headers = new Dictionary <string, string>
            {
                { HttpHeaders.Authorization, device.AuthHeaderValue },
                { "mag-identifier", device.MagId },
                { HttpHeaders.ContentType, HttpContentTypes.UrlEncoded },
                { HttpHeaders.Accept, HttpContentTypes.Json }
            };

            var scope = config.DefaultClientId.Scope;

            var builder = new HttpUrlBuilder();

            builder.Add("scope", scope);
            builder.Add("refresh_token", refreshToken);
            builder.Add("grant_type", "refresh_token");

            return(HttpRequestFactory.RequestAsync <RequestTokenResponseData>(new HttpRequestInfo()
            {
                Method = HttpMethod.POST,
                Url = url,
                Headers = headers,
                Body = builder.ToString()
            }));
        }
Beispiel #3
0
        public static async Task LogoutSessionAsync(Configuration config, MASDevice device, MASUser user)
        {
            var url = config.GetEndpointPath(config.OAuth.SystemEndpoints.UserSessionLogout);

            HttpUrlBuilder builder = new HttpUrlBuilder();

            builder.Add("logout_apps", config.Mag.MobileSdk.IsSSOEnabled ? "true" : "false");
            builder.Add("id_token", user.IdToken);
            builder.Add("id_token_type", user.IdTokenType);

            var headers = new Dictionary <string, string>
            {
                { HttpHeaders.Authorization, device.AuthHeaderValue },
                { HttpHeaders.Accept, HttpContentTypes.Json },
                { "mag-identifier", device.MagId },
                { HttpHeaders.ContentType, HttpContentTypes.UrlEncoded }
            };

            await HttpRequestFactory.RequestAsync <HttpResponseBaseData>(new HttpRequestInfo()
            {
                Method  = HttpMethod.POST,
                Headers = headers,
                Url     = url,
                Body    = builder.ToString()
            });
        }
Beispiel #4
0
        public static async Task <ClientCredentialsResponseData> GetClientCredentialsAsync(Configuration config, string deviceIdBase64)
        {
            var url = config.GetEndpointPath(config.Mag.SystemEndpoints.ClientCredentialInit);

            var clientId = config.OAuth.Client.ClientIds[0];

            var builder = new HttpUrlBuilder();

            builder.Add("client_id", clientId.Id);
            builder.AddNonce();

            var headers = new Dictionary <string, string>
            {
                { HttpHeaders.Authorization, clientId.BasicAuthValue },
                { "device-id", deviceIdBase64 },
                { HttpHeaders.ContentType, HttpContentTypes.UrlEncoded },
                { HttpHeaders.Accept, HttpContentTypes.Json }
            };

            return(await HttpRequestFactory.RequestAsync <ClientCredentialsResponseData>(new HttpRequestInfo()
            {
                Method = HttpMethod.POST,
                Url = url,
                Headers = headers,
                Body = builder.ToString()
            }));
        }
Beispiel #5
0
        static string FormatBody(RequestType type, PropertyList parameters)
        {
            string body = string.Empty;

            if (parameters != null)
            {
                switch (type)
                {
                case RequestType.FormUrlEncoded:
                {
                    HttpUrlBuilder builder = new HttpUrlBuilder();
                    foreach (var item in parameters)
                    {
                        builder.Add(item.Key, item.Value);
                    }
                    body = builder.ToString();
                }
                break;

                default:
                    body = parameters.FirstOrDefault()?.Value ?? string.Empty;
                    break;
                }
            }

            return(body);
        }
        public void ChangesScheme()
        {
            var builder = new HttpUrlBuilder("http://host1:1337/path");

            builder.WithScheme("https");

            Assert.Equal("https://host1:1337/path", builder.ToString());
        }
        public void ChangesPost()
        {
            var builder = new HttpUrlBuilder("http://host1:1337/path");

            builder.WithPort(2112);

            Assert.Equal("http://host1:2112/path", builder.ToString());
        }
        public void ChangesHostName()
        {
            var builder = new HttpUrlBuilder("http://host1:1337/path");

            builder.WithHost("host2");

            Assert.Equal("http://host2:1337/path", builder.ToString());
        }
Beispiel #9
0
        public static async Task RevokeAccessTokenAsync(Configuration config, MASDevice device, MASUser user)
        {
            var url = config.GetEndpointPath(config.OAuth.SystemEndpoints.TokenRevocation);

            var accessToken = await user.GetAccessTokenAsync();

            HttpUrlBuilder builder = new HttpUrlBuilder(url);

            builder.Add("token", accessToken);
            builder.Add("token_type_hint", "access_token");

            var headers = new Dictionary <string, string>
            {
                { HttpHeaders.Authorization, device.AuthHeaderValue },
                { HttpHeaders.Accept, HttpContentTypes.Json }
            };

            await HttpRequestFactory.RequestAsync <HttpResponseBaseData>(new HttpRequestInfo()
            {
                Method  = HttpMethod.DELETE,
                Headers = headers,
                Url     = builder.ToString(),
            });
        }
Beispiel #10
0
        static async Task <TextResponse> RequestHttpAsync(HttpMethod method, string endPointPath,
                                                          PropertyCollection parameters, PropertyCollection headers,
                                                          RequestType requestType, ResponseType responseType, bool attemptTokenRefresh)
        {
            if (!MASApplication.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

            if (!MASDevice.Current.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.DeviceNotRegistered);
            }

            string url  = endPointPath;
            string body = null;

            if (method == HttpMethod.GET || method == HttpMethod.DELETE)
            {
                var builder = new HttpUrlBuilder(endPointPath);
                if (parameters != null)
                {
                    foreach (var paramInfo in parameters.Properties)
                    {
                        builder.Add(paramInfo.Key, paramInfo.Value);
                    }
                }

                url = builder.ToString();
            }
            else if (parameters != null)
            {
                body = FormatBody(requestType, parameters.Properties);
            }


            MASUser requestUser = null;

            if (MASUser.Current != null && MASUser.Current.IsLoggedIn)
            {
                requestUser = MASUser.Current;
            }
            else if (MASApplication.Current.Client != null)
            {
                requestUser = MASApplication.Current.Client;
            }

            var requestHeaders = await SetupRequestHeaders(requestUser, headers?.Properties, requestType, responseType);

            try
            {
                var requestResponse = await HttpRequestFactory.RequestTextAsync(new HttpRequestInfo()
                {
                    Url         = url,
                    Method      = method,
                    Headers     = requestHeaders,
                    Body        = body,
                    Certificate = MASDevice.Current.Certificate
                });

                return(ToMASResponse(requestResponse));
            }
            catch (MASException exp)
            {
                if (requestUser == null || attemptTokenRefresh == false || exp.MASErrorCode != ErrorCode.TokenAccessExpired)
                {
                    throw exp;
                }
            }

            // Our token has expired, attempt to refresh it and try again!
            await requestUser.RefreshAccessTokenAsync();

            // Lets not try to refresh token after our first attempt
            return(await RequestHttpAsync(method, endPointPath, parameters,
                                          headers, requestType, responseType, false));
        }
        public void ConvertsAbsoluteUrlToString()
        {
            var builder = new HttpUrlBuilder("http://localhost/api/foo", UriKind.Absolute);

            Assert.Equal("http://localhost/api/foo", builder.ToString());
        }
        public void ConvertsRelativeUrlToString()
        {
            var builder = new HttpUrlBuilder("/api/foo", UriKind.Relative);

            Assert.Equal("/api/foo", builder.ToString());
        }
        public void ConvertsToString()
        {
            var builder = new HttpUrlBuilder("http://localhost/api/foo");

            Assert.Equal("http://localhost/api/foo", builder.ToString());
        }