public async Task GetRawTokenAsync_SendsCorrectRequest()
        {
            HttpConfiguration config = new HttpConfiguration();

            config.SetMobileAppSettingsProvider(new MobileAppSettingsProvider());

            string     accessToken  = "facebookAccessToken";
            string     authToken    = "zumoAuthToken";
            string     facebookId   = "facebookUserId";
            string     providerName = "Facebook";
            TokenEntry tokenEntry   = new TokenEntry(providerName);

            tokenEntry.AccessToken         = accessToken;
            tokenEntry.AuthenticationToken = authToken;
            tokenEntry.UserId = facebookId;

            MockHttpMessageHandler handlerMock = new MockHttpMessageHandler(CreateResponse(tokenEntry));

            var webappUri = "http://test";
            AppServiceHttpClient appServiceClientMock = new AppServiceHttpClient(new HttpClient(handlerMock));

            // Act
            TokenEntry result = await appServiceClientMock.GetRawTokenAsync(new Uri(webappUri), accessToken, "Facebook");

            // Assert
            Assert.Equal(accessToken, result.AccessToken);
            Assert.Equal(authToken, result.AuthenticationToken);
            Assert.Equal(facebookId, result.UserId);
            Assert.Equal(webappUri + "/.auth/me?provider=facebook", handlerMock.ActualRequest.RequestUri.ToString());
            Assert.Equal(accessToken, handlerMock.ActualRequest.Headers.GetValues("x-zumo-auth").Single());
            Assert.Equal("MobileAppNetServerSdk", handlerMock.ActualRequest.Headers.GetValues("User-Agent").Single());
        }
        /// <summary>
        /// Gets the provider specific identity details for the ServiceUser
        /// </summary>
        /// <typeparam name="T">The provider type</typeparam>
        /// <returns>The provider credentials if found, otherwise null</returns>
        public async Task <T> GetIdentityAsync <T>() where T : ProviderCredentials, new()
        {
            if (this.Identity == null || !this.Identity.IsAuthenticated || string.IsNullOrEmpty(this.MobileAppAuthenticationToken))
            {
                return(null);
            }

            string gatewayUrl = ConfigurationManager.AppSettings["EMA_RuntimeUrl"];

            if (gatewayUrl == null)
            {
                throw new NullReferenceException(RResources.Missing_EmaRuntimeUrl);
            }

            AppServiceHttpClient client = this.CreateAppServiceHttpClient(new Uri(gatewayUrl));

            ProviderCredentials credentials = (ProviderCredentials) new T();
            TokenResult         tokenResult = await client.GetRawTokenAsync(this.MobileAppAuthenticationToken, credentials.Provider);

            if (!IsTokenValid(tokenResult))
            {
                return(null);
            }

            PopulateProviderCredentials(tokenResult, credentials);

            return((T)credentials);
        }
        public async Task GetRawTokenAsync_Throws_IfParametersAreNull(string authToken, string tokenProviderName, string parameterThatThrows)
        {
            AppServiceHttpClient appServiceClient = new AppServiceHttpClient(new HttpClient());

            // Act
            var ex = await Assert.ThrowsAsync <ArgumentNullException>(() => appServiceClient.GetRawTokenAsync(new Uri("http://testuri"), authToken, tokenProviderName));

            // Assert
            Assert.NotNull(ex);
            Assert.Equal(parameterThatThrows, ex.ParamName);
        }
        public async Task GetRawTokenAsync_Throws_IfParametersAreNull(string first, string second, string parameterThatThrows)
        {
            AppServiceHttpClient appServiceClient = new AppServiceHttpClient(new Uri("http://testuri"));

            // Act
            var ex = await Assert.ThrowsAsync <ArgumentNullException>(() => appServiceClient.GetRawTokenAsync(first, second));

            // Assert
            Assert.NotNull(ex);
            Assert.Equal(parameterThatThrows, ex.ParamName);
        }
Exemplo n.º 5
0
        public static async Task <T> GetAppServiceIdentityAsync <T>(this IPrincipal principal, HttpRequestMessage request, HttpClient httpClient) where T : ProviderCredentials, new()
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            ClaimsPrincipal user = principal as ClaimsPrincipal;

            if (user == null)
            {
                throw new ArgumentOutOfRangeException(RResources.ParameterMustBeOfType.FormatInvariant("principal", typeof(ClaimsPrincipal).Name), (Exception)null);
            }

            // Get the token from the request
            string zumoAuthToken = request.GetHeaderOrDefault("x-zumo-auth");

            if (string.IsNullOrEmpty(zumoAuthToken))
            {
                return(null);
            }

            // Base the url on the issuer of the JWT
            Claim issuerClaim = user.FindFirst(JwtRegisteredClaimNames.Iss);

            if (issuerClaim == null)
            {
                throw new ArgumentOutOfRangeException(RResources.GetIdentity_ClaimsMustHaveIssuer, (Exception)null);
            }

            string issuerUrl = issuerClaim.Value;
            ProviderCredentials  credentials  = (ProviderCredentials) new T();
            TokenEntry           tokenEntry   = null;
            AppServiceHttpClient appSvcClient = new AppServiceHttpClient(httpClient);

            try
            {
                tokenEntry = await appSvcClient.GetRawTokenAsync(new Uri(issuerUrl), zumoAuthToken, credentials.Provider);
            }
            catch (HttpResponseException ex)
            {
                throw new InvalidOperationException(RResources.GetIdentity_HttpError.FormatInvariant(ex.Response.ToString()));
            }

            if (!IsTokenValid(tokenEntry))
            {
                return(null);
            }

            PopulateProviderCredentials(tokenEntry, credentials);

            return((T)credentials);
        }
        public async Task GetRawTokenAsync_ReturnsNull_IfNoToken()
        {
            string accessToken = "facebookAccessToken";
            MockHttpMessageHandler handlerMock = new MockHttpMessageHandler(CreateEmptyResponse());

            var webappUri = "http://test";
            AppServiceHttpClient appServiceClientMock = new AppServiceHttpClient(new HttpClient(handlerMock));

            // Act
            TokenEntry result = await appServiceClientMock.GetRawTokenAsync(new Uri(webappUri), accessToken, "Facebook");

            // Assert
            Assert.Null(result);
            Assert.Equal(webappUri + "/.auth/me?provider=facebook", handlerMock.ActualRequest.RequestUri.ToString());
            Assert.Equal(accessToken, handlerMock.ActualRequest.Headers.GetValues("x-zumo-auth").Single());
            Assert.Equal("MobileAppNetServerSdk", handlerMock.ActualRequest.Headers.GetValues("User-Agent").Single());
        }
        public async Task GetRawTokenAsync_Throws_IfResponseIsNotSuccess(HttpStatusCode status)
        {
            HttpConfiguration config = new HttpConfiguration();

            config.SetMobileAppSettingsProvider(new MobileAppSettingsProvider());

            var response = new HttpResponseMessage(status);
            MockHttpMessageHandler handlerMock = new MockHttpMessageHandler(response);
            var gatewayUri = "http://test";

            AppServiceHttpClient appServiceClientMock = new AppServiceHttpClient(new HttpClient(handlerMock));

            // Act
            var ex = await Assert.ThrowsAsync <HttpResponseException>(() => appServiceClientMock.GetRawTokenAsync(new Uri(gatewayUri), "123456", "Facebook"));

            // Assert
            Assert.NotNull(ex);
            Assert.Same(response, ex.Response);
        }
        public async Task GetRawTokenAsync_Throws_IfParametersAreNull(string first, string second, string parameterThatThrows)
        {
            AppServiceHttpClient appServiceClient = new AppServiceHttpClient(new Uri("http://testuri"));

            // Act
            var ex = await Assert.ThrowsAsync<ArgumentNullException>(() => appServiceClient.GetRawTokenAsync(first, second));

            // Assert
            Assert.NotNull(ex);
            Assert.Equal(parameterThatThrows, ex.ParamName);
        }