Skip to content

Commit d1c66f1

Browse files
committed
Adding Flex Token Verification code to SDK
1 parent 60226da commit d1c66f1

File tree

7 files changed

+251
-9
lines changed

7 files changed

+251
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace CyberSource.Utilities.Flex.Exception
4+
{
5+
public abstract class FlexException : System.Exception, ISerializable
6+
{
7+
/// <summary>
8+
/// Constructs a new instance of the exception with a specified error message.
9+
/// </summary>
10+
/// <param name="message">The error message that explains the reason for the exception</param>
11+
public FlexException(string message) : base(message) { }
12+
13+
/// <summary>
14+
/// Constructs a new instance of the exception with a specified error message
15+
/// and a reference to the inner exception that is the cause of this exception.
16+
/// </summary>
17+
/// <param name="message">The error message that explains the reason for the exception</param>
18+
/// <param name="inner">The exception that is the cause of the current exception</param>
19+
public FlexException(string message, System.Exception inner) : base(message, inner) { }
20+
21+
protected FlexException(SerializationInfo serializationInfo, StreamingContext streamingContext)
22+
: base(serializationInfo, streamingContext)
23+
{
24+
}
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace CyberSource.Utilities.Flex.Exception
4+
{
5+
/// <summary>
6+
/// Exception that is thrown when there is an error performing an internal SDK function.
7+
/// </summary>
8+
public class FlexInternalException : FlexException
9+
{
10+
public FlexInternalException(string message) : base(message) { }
11+
12+
public FlexInternalException(string message, System.Exception inner) : base(message, inner) { }
13+
14+
protected FlexInternalException(SerializationInfo serializationInfo, StreamingContext streamingContext)
15+
: base(serializationInfo, streamingContext)
16+
{
17+
}
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace CyberSource.Utilities.Flex.Model
2+
{
3+
/// <summary>
4+
/// Represents the public RSA key in DER format.
5+
/// </summary>
6+
public class FlexDerPublicKey
7+
{
8+
/// <summary>
9+
/// Gets and sets the public key format.
10+
/// </summary>
11+
public string format { get; set; }
12+
13+
/// <summary>
14+
/// Gets and sets the algorithm with which the key is used.
15+
/// </summary>
16+
public string algorithm { get; set; }
17+
18+
/// <summary>
19+
/// Gets and sets the encoded key specification.
20+
/// </summary>
21+
public string publicKey { get; set; }
22+
23+
/// <summary>
24+
/// Default constructor. Use if you wish to set properties individually.
25+
/// </summary>
26+
public FlexDerPublicKey() { }
27+
28+
/// <summary>
29+
/// Constructs a DerPublicKey instance with the supplied format, algorithm and encoded key.
30+
/// </summary>
31+
/// <param name="format">The format</param>
32+
/// <param name="algorithm">The algorithm</param>
33+
/// <param name="publicKey">The encoded key</param>
34+
public FlexDerPublicKey(string format, string algorithm, string publicKey)
35+
{
36+
this.format = format;
37+
this.algorithm = algorithm;
38+
this.publicKey = publicKey;
39+
}
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace CyberSource.Utilities.Flex.Model
2+
{
3+
public class FlexPublicKey
4+
{
5+
/// <summary>
6+
/// Gets or sets the unique ID of the key.
7+
/// </summary>
8+
public string keyId { get; set; }
9+
10+
/// <summary>
11+
/// Gets or sets the DER representation of the key.
12+
/// </summary>
13+
public FlexDerPublicKey der { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the JSON Web Key representation of the key for use with the Web Crypto API.
17+
/// </summary>
18+
public JsonWebKey jwk { get; set; }
19+
20+
/// <summary>
21+
/// Default constructor. Use if you wish to set properties individually.
22+
/// </summary>
23+
public FlexPublicKey() { }
24+
25+
/// <summary>
26+
/// Constructs a FlexPublicKey instance with the specified ID, DER representation and JWK representation.
27+
/// </summary>
28+
/// <param name="keyId">The ID</param>
29+
/// <param name="der">The DER representation</param>
30+
/// <param name="jwk">The JWK representation</param>
31+
public FlexPublicKey(string keyId, FlexDerPublicKey der, JsonWebKey jwk)
32+
{
33+
this.keyId = keyId;
34+
this.der = der;
35+
this.jwk = jwk;
36+
}
37+
38+
public override string ToString()
39+
{
40+
return System.String.Format("FlexPublicKey[keyId={0}]", keyId);
41+
}
42+
}
43+
}

Utilities/Flex/Model/FlexToken.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Collections.Generic;
2+
3+
namespace CyberSource.Utilities.Flex.Model
4+
{
5+
/// <summary>
6+
/// Represents the Flex API token response.
7+
/// </summary>
8+
public class FlexToken
9+
{
10+
/// <summary>
11+
/// Gets or sets the ID of the key associated with the token.
12+
/// </summary>
13+
public string keyId { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the generated token. The token replaces card data and is used as the Subscription ID in the CyberSource Simple Order API or SCMP API.
17+
/// </summary>
18+
public string token { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the masked card number.
22+
/// </summary>
23+
public string maskedPan { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the card type.
27+
/// </summary>
28+
public string cardType { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the UTC date and time in milliseconds at which the signature was generated.
32+
/// </summary>
33+
public long timestamp { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the list of follow-on services with which the token may be used.
37+
/// </summary>
38+
public IDictionary<string, object> discoverableServices { get; set; }
39+
40+
/// <summary>
41+
/// Gets or sets which fields from the response make up the data that is used when verifying the response signature.
42+
/// </summary>
43+
public string signedFields { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets the Flex-generated digital signature. To ensure the values have not been tampered with while passing through the client, verify the signature server-side using the associated Flex API public key.
47+
/// </summary>
48+
public string signature { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the embedded object(s).
52+
/// </summary>
53+
public IDictionary<string, object> _embedded { get; set; }
54+
55+
/// <summary>
56+
/// Constructs a FlexToken instance.
57+
/// </summary>
58+
public FlexToken() { }
59+
}
60+
}

Utilities/Flex/Model/JsonWebKey.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace CyberSource.Utilities.Flex.Model
2+
{
3+
/// <summary>
4+
/// Represents a JSON Web Key for use with the Web Crypto API.
5+
/// </summary>
6+
public class JsonWebKey
7+
{
8+
/// <summary>
9+
/// Gets or sets the key type.
10+
/// </summary>
11+
public string kty { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the key use.
15+
/// </summary>
16+
public string use { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the key ID.
20+
/// </summary>
21+
public string kid { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the modulus.
25+
/// </summary>
26+
public string n { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the exponent.
30+
/// </summary>
31+
public string e { get; set; }
32+
33+
/// <summary>
34+
/// Default constructor. Use if you wish to set properties individually.
35+
/// </summary>
36+
public JsonWebKey() { }
37+
38+
/// <summary>
39+
/// Constructs a JsonWebKey instance with the specified key type, key use, key ID, modulus and exponent.
40+
/// </summary>
41+
/// <param name="keyType">The key type</param>
42+
/// <param name="keyUse">The key use</param>
43+
/// <param name="keyId">The key ID</param>
44+
/// <param name="modulus">The modulus</param>
45+
/// <param name="exponent">The exponent</param>
46+
public JsonWebKey(string keyType, string keyUse, string keyId, string modulus, string exponent)
47+
{
48+
this.kty = keyType;
49+
this.use = keyUse;
50+
this.kid = keyId;
51+
this.n = modulus;
52+
this.e = exponent;
53+
}
54+
}
55+
}

Utilities/Flex/TokenVerification/TokenVerificationUtility.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
using FlexServerSDK.Exception;
2-
using FlexServerSDK.Model;
1+
using CyberSource.Utilities.Flex.Exception;
2+
using CyberSource.Utilities.Flex.Model;
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6-
using System.Linq;
76
using System.Security.Cryptography;
87
using System.Text;
9-
using System.Threading.Tasks;
108

119
namespace CyberSource.Utilities.Flex.TokenVerification
1210
{
13-
public static class TokenVerificationUtility
11+
public class TokenVerificationUtility
1412
{
15-
public static bool Verify(FlexPublicKey flexKey, IDictionary<string, string> postParameters)
13+
public bool Verify(FlexPublicKey flexKey, IDictionary<string, string> postParameters)
1614
{
1715
var publicKeyStr = flexKey.der.publicKey;
1816
RSAParameters publicKey = DecodePublicKey(Convert.FromBase64String(publicKeyStr)).ExportParameters(false);
@@ -52,11 +50,11 @@ private static bool ValidateTokenSignature(RSAParameters publicKey, string signe
5250
}
5351
catch (CryptographicException e)
5452
{
55-
throw new FlexSDKInternalException("Error validating signature", e);
53+
throw new FlexInternalException("Error validating signature", e);
5654
}
5755
catch (System.Exception e)
5856
{
59-
throw new FlexSDKInternalException("Error validating signature", e);
57+
throw new FlexInternalException("Error validating signature", e);
6058
}
6159
finally
6260
{
@@ -196,7 +194,7 @@ private static RSACryptoServiceProvider DecodePublicKey(byte[] x509key)
196194
rsa.ImportParameters(rsaKeyInfo);
197195
return rsa;
198196
}
199-
catch (Exception)
197+
catch (System.Exception)
200198
{
201199
return null;
202200
}

0 commit comments

Comments
 (0)