/// <summary>
        /// Adds the authorization header to the given web client.
        /// </summary>
        /// <param name="webClient">The web client.</param>
        /// <param name="accessToken">The access token.</param>
        public static void AddAuthorizationHeader(this WebClient webClient, JsonWebTokenAccessToken accessToken)
        {
            if (webClient == null)
            {
                throw new ArgumentNullException("webClient");
            }

            if (accessToken == null)
            {
                throw new ArgumentNullException("accessToken");
            }

            webClient.Headers.Add(HttpRequestHeader.Authorization, accessToken.TokenType + " " + accessToken.AccessToken);
        }
Example #2
0
        /// <summary>
        /// Queries a JSON web token service with the given claim set signed with the given signer
        /// returning a JSON web access token response.
        /// </summary>
        /// <param name="claimSet">The claim set to use.</param>
        /// <param name="signer">The signer to use to sign the JSON web token before querying.</param>
        /// <returns>The JSON web access token response.</returns>
        /// <exception cref="System.ArgumentNullException">If claimSet or signer are null.</exception>
        /// <exception cref="System.InvalidOperationException">
        /// If JsonWebToken.JsonDeserialize has not been configured or claimSet.Audience does not
        /// contain a valid service Uri for authorizing the claim.
        /// </exception>
        public static async Task <JsonWebTokenAccessToken> Authorize(JsonWebTokenClaimSet claimSet, IJsonWebTokenSigner signer)
        {
            if (claimSet == null)
            {
                throw new ArgumentNullException("claimSet");
            }

            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            if (JsonWebTokenJsonDeserialize == null)
            {
                throw new InvalidOperationException(
                          "JsonWebToken.JsonDeserialize must be set to a valid JSON deserializer before calling authorize.");
            }

            if (claimSet.Audience == null)
            {
                throw new InvalidOperationException("claimSet.Audience must contain the Uri of the service that will authorize the claim.");
            }

            var jsonWebToken = CreateToken(claimSet, signer);

            var parameters = new NameValueCollection()
            {
                { "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
                { "assertion", jsonWebToken }
            };

            var webClient = new WebClient();

            var response = await webClient.UploadValuesTaskAsync(claimSet.Audience, "POST", parameters);

            var jsonResult = Encoding.UTF8.GetString(response);
            var result     = JsonWebTokenJsonDeserialize(jsonResult);

            var accessToken = new JsonWebTokenAccessToken(result);

            return(accessToken);
        }