/// <summary> /// Calculates the payload information based on the request and the authentication information. /// </summary> /// <param name="request"> /// The request which has the information (method, url and content) for the calculation. /// </param> /// <param name="authInfo"> /// The <see cref="PrivateKeyAuthenticationInfo"/> which holds the application uuid, the time of the /// signature and the private key. /// </param> /// <returns>A task object which will result the payload as a Base64 encoded string when completed.</returns> public static async Task <string> CalculatePayload( this HttpRequestMessage request, PrivateKeyAuthenticationInfo authInfo) { var unsignedData = await request.GetSignature(authInfo); var signer = new Pkcs1Encoding(new RsaEngine()); signer.Init(true, authInfo.PrivateKey.AsCipherParameters()); return(Convert.ToBase64String(signer.ProcessBlock(unsignedData, 0, unsignedData.Length))); }
/// <summary> /// Adds authentication information to a <see cref="HttpRequestMessage"/>. /// </summary> /// <param name="request">The request to add the information to.</param> /// <param name="authInfo"> /// The authentication information with a private key to calculate the payload with. /// </param> /// <returns> /// A Task object which will result the request with the authentication information added when it completes. /// </returns> internal async Task <HttpRequestMessage> AddAuthenticationInfo( HttpRequestMessage request, PrivateKeyAuthenticationInfo authInfo) { var authHeader = $"{MAuthVersion.MWSV2} {authInfo.ApplicationUuid.ToHyphenString()}:" + $"{await CalculatePayload(request, authInfo).ConfigureAwait(false)};"; request.Headers.Add(Constants.MAuthHeaderKeyV2, authHeader); request.Headers.Add(Constants.MAuthTimeHeaderKeyV2, authInfo.SignedTime.ToUnixTimeSeconds().ToString()); return(request); }
/// <summary> /// Adds authentication information to a <see cref="HttpRequestMessage"/>. /// </summary> /// <param name="request">The request to add the information to.</param> /// <param name="authInfo"> /// The authentication information with a private key to calculate the payload with. /// </param> /// <returns> /// A Task object which will result the request with the authentication information added when it completes. /// </returns> public async static Task <HttpRequestMessage> AddAuthenticationInfo( this HttpRequestMessage request, PrivateKeyAuthenticationInfo authInfo) { var authHeader = $"MWS {authInfo.ApplicationUuid.ToHyphenString()}:" + $"{await request.CalculatePayload(authInfo)}"; request.Headers.Add(Constants.MAuthHeaderKey, authHeader); request.Headers.Add(Constants.MAuthTimeHeaderKey, authInfo.SignedTime.ToUnixTimeSeconds().ToString()); return(request); }
/// <summary> /// Calculates the payload information based on the request and the authentication information. /// </summary> /// <param name="request"> /// The request which has the information (method, url and content) for the calculation. /// </param> /// <param name="authInfo"> /// The <see cref="PrivateKeyAuthenticationInfo"/> which holds the application uuid, the time of the /// signature and the private key. /// </param> /// <returns>A task object which will result the payload as a Base64 encoded string when completed.</returns> internal async Task <string> CalculatePayload( HttpRequestMessage request, PrivateKeyAuthenticationInfo authInfo) { var unsignedData = await GetSignature(request, authInfo).ConfigureAwait(false); var signer = new RSACryptoServiceProvider(); signer.PersistKeyInCsp = false; signer.ImportParameters(authInfo.PrivateKey.AsRsaParameters()); return(Convert.ToBase64String(signer.SignData(unsignedData, CryptoConfig.MapNameToOID("SHA512")))); }