-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
porting changes to support VendorCredentials (which are used for auth…
…orization IPN requests)
- Loading branch information
Showing
22 changed files
with
421 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/KeyHub.Model/Definition/Application/VendorCredential.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace KeyHub.Model | ||
{ | ||
/// <summary> | ||
/// Shared secret used by vendors when POST'ing transactions | ||
/// </summary> | ||
public class VendorCredential | ||
{ | ||
/// <summary> | ||
/// Indentifier for the PrivateKey entity. | ||
/// </summary> | ||
[Key] | ||
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] | ||
public Guid VendorCredentialId { get; set; } | ||
|
||
/// <summary> | ||
/// The vendor this key is owned by. | ||
/// </summary> | ||
[Required] | ||
public Guid VendorId { get; set; } | ||
|
||
/// <summary> | ||
/// The vendor this key is owned by. | ||
/// </summary> | ||
[ForeignKey("VendorId")] | ||
public Vendor Vendor { get; set; } | ||
|
||
|
||
/// <summary> | ||
/// The name of the shared secret (managed by the vendor) | ||
/// </summary> | ||
[Required] | ||
public string CredentialName { get; set; } | ||
|
||
/// <summary> | ||
/// The shared secret, encrypted by SymmetricEncryption.EncryptForDatabase | ||
/// </summary> | ||
[Required] | ||
public byte[] CredentialValue { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System.Text; | ||
using System.Data.Entity; | ||
using System.Text; | ||
using KeyHub.Common.Collections; | ||
using System; | ||
using System.Collections.Generic; | ||
|
@@ -9,6 +10,7 @@ | |
using System.Web.Http; | ||
using System.Xml; | ||
using KeyHub.Common.Extensions; | ||
using KeyHub.Common.Utils; | ||
using KeyHub.Core.Mail; | ||
using KeyHub.Data; | ||
using KeyHub.Web.Api.Controllers.LicenseValidation; | ||
|
@@ -39,20 +41,39 @@ public TransactionByIpnController(IDataContextFactory dataContextFactory, IMailS | |
/// <param name="postedData"></param> | ||
public HttpResponseMessage PostTransactionByIpn([FromUri]string id, [FromBody]FormDataCollection postedData) | ||
{ | ||
string vendor = id; | ||
var vendorId = Guid.Parse(id); | ||
var txn = new Transaction(); | ||
var d = postedData.ReadAsNameValueCollection(); | ||
|
||
//To calculate 'handshake', run 'md5 -s [password]', then 'md5 -s [email protected][Last MD5 result]' | ||
if (!"ff35a320762dcec799d9c0bb9831577c".Equals(d.Pluck("handshake",null), StringComparison.OrdinalIgnoreCase)) throw new Exception("Invalid handshake provided"); | ||
string handshakeParameter = d.Pluck("handshake",null); | ||
if (handshakeParameter == null) | ||
throw new Exception("Missing parameter 'handshake'."); | ||
|
||
using (var dataContext = dataContextFactory.Create()) | ||
{ | ||
var vendor = | ||
dataContext.Vendors.Where(v => v.ObjectId == vendorId) | ||
.Include(v => v.VendorCredentials) | ||
.FirstOrDefault(); | ||
|
||
if (vendor == null) | ||
throw new Exception("Could not find vendor with id: " + vendorId); | ||
|
||
string[] vendorCredentials = vendor.VendorCredentials.Select( | ||
c => Encoding.UTF8.GetString(SymmetricEncryption.DecryptForDatabase(c.CredentialValue)).ToLower()).ToArray(); | ||
|
||
if (!vendorCredentials.Contains(handshakeParameter.ToLower())) | ||
throw new Exception("Invalid handshake provided"); | ||
} | ||
|
||
string txn_id = d.Pluck("txn_id"); | ||
//TODO: We must ignore duplicate POSTs with the same txn_id - all POSTs will contain the same information | ||
|
||
if (!"Completed".Equals(d.Pluck("payment_status"), StringComparison.OrdinalIgnoreCase)) throw new Exception("Only completed transactions should be sent to this URL"); | ||
|
||
//var txn = new Transaction(); | ||
txn.VendorId = Guid.Parse(vendor); | ||
txn.VendorId = vendorId; | ||
txn.ExternalTransactionId = txn_id; | ||
txn.PaymentDate = ConvertPayPalDateTime(d.Pluck("payment_date")); | ||
txn.PayerEmail = d.Pluck("payer_email"); | ||
|
@@ -101,7 +122,7 @@ public HttpResponseMessage PostTransactionByIpn([FromUri]string id, [FromBody]Fo | |
txn.Other = d; | ||
|
||
//All transactions go through TransactionController | ||
base.ProcessTransaction(txn.ToTransactionRequest(dataContextFactory), User.Identity); | ||
base.ProcessTransaction(txn.ToTransactionRequest(dataContextFactory), new [] { vendorId}); | ||
|
||
return new HttpResponseMessage(HttpStatusCode.OK); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.