public void GenerateAndDecomposeTokenWithAdditionalElements(string resourceUri, string key, string policyName, int expiryInSeconds)
        {
            var token = SharedAccessTokens.GenerateSasToken(resourceUri, key, policyName, expiryInSeconds);

            Assert.IsTrue(!string.IsNullOrWhiteSpace(token), "The token should not be null or whitespace");
            Assert.IsFalse(SharedAccessTokens.DecomposeSasToken($"{ token }&ad=additionalstuff", out var _, out var _, out var _, out var _, out var _), "Successfully decomposes");
        }
Beispiel #2
0
        /// <summary>
        /// Authenticates the client credentials against the hub
        /// </summary>
        /// <param name="request">The request</param>
        /// <param name="cancellationToken">A token to monitor for abort requests</param>
        /// <returns>The authentication response</returns>
        protected virtual async Task <AuthenticationResponsePayload> AuthenticateClientAsync(HttpRequest request, CancellationToken cancellationToken)
        {
            AuthenticationResponsePayload response = null;

            using (var reader = new StreamReader(request.Body))
            {
                var requestPayload = JsonConvert.DeserializeObject <AuthenticationRequestPayload>(reader.ReadToEnd());

                using (_authenticationTiming.Time())
                {
                    if (await AuthenticateAsync(request, requestPayload, cancellationToken).ConfigureAwait(false))
                    {
                        var accessToken = SharedAccessTokens.GenerateSasToken($"{_sasTokenScheme}://{requestPayload.UserName}", _sasSigningKey, _sasTokenPolicyName, (int)_sasTokenTimeout.TotalSeconds);

                        response = new AuthenticationResponsePayload
                        {
                            TokenType   = "bearer",
                            AccessToken = accessToken
                        };
                    }
                }
            }

            return(response);
        }
        public void GenerateAndValidateToken(string resourceUri, string key, string policyName, int expiryInSeconds)
        {
            var token = SharedAccessTokens.GenerateSasToken(resourceUri, key, policyName, expiryInSeconds);

            Assert.IsTrue(!string.IsNullOrWhiteSpace(token), "The token should not be null or whitespace");
            Assert.IsTrue(SharedAccessTokens.DecomposeSasToken(token, out var outputResourceUri, out var outputPolicyName, out var _, out var stringToValidate, out var signature), "Successfully decomposes");
            Assert.IsTrue(SharedAccessTokens.IsSignatureValid(signature, key, stringToValidate), "The signature was not valid");
            Assert.IsTrue(string.Equals(outputResourceUri, resourceUri, StringComparison.Ordinal), "Resource URI not expected");
            Assert.IsTrue(string.Equals(outputPolicyName, policyName, StringComparison.Ordinal), "Policy name not expected");
        }
        public void GenerateAndDecomposeToken(string resourceUri, string key, string policyName, int expiryInSeconds)
        {
            var token = SharedAccessTokens.GenerateSasToken(resourceUri, key, policyName, expiryInSeconds);

            System.Diagnostics.Debug.Print(token);
            Assert.IsTrue(!string.IsNullOrWhiteSpace(token), "The token should not be null or whitespace");
            Assert.IsTrue(SharedAccessTokens.DecomposeSasToken(token, out var outputResourceUri, out var outputPolicyName, out var _, out var _, out var _), "Successfully decomposes");
            Assert.IsTrue(string.Equals(outputResourceUri, resourceUri, StringComparison.Ordinal), "Resource URI not expected");
            Assert.IsTrue(string.Equals(outputPolicyName, policyName, StringComparison.Ordinal), "Policy name not expected");
        }