-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFakeOrderCloudToken.cs
69 lines (60 loc) · 2.02 KB
/
FakeOrderCloudToken.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.IdentityModel.Tokens;
using OrderCloud.SDK;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
namespace OrderCloud.Catalyst
{
public class FakeOrderCloudToken
{
/// <summary>
/// Create a fake token for unit testing. (Grants no access to the api).
/// </summary>
public static string Create(
string clientID,
List<string> roles = null,
DateTime? expiresUTC = null,
DateTime? notValidBeforeUTC = null,
string username = null,
string keyID = null,
string anonOrderID = null,
string authUrl = null,
string apiUrl = null,
CommerceRole userType = CommerceRole.Seller,
string userDatabaseID = null,
string impersonatingUserDatabaseID = null
)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("blahblahblahblahblahblahblahblahblahblah"));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var header = new JwtHeader(creds);
if (keyID != null)
{
header["kid"] = keyID;
}
var claims = (roles ?? new List<string>()).Select(r => new Claim("role", r)).ToList();
AddClaimIfNotNull(claims, "orderid", anonOrderID);
AddClaimIfNotNull(claims, "usr", username);
AddClaimIfNotNull(claims, "usrtype", DecodedToken.GetUserType(userType));
AddClaimIfNotNull(claims, "cid", clientID);
AddClaimIfNotNull(claims, "u", userDatabaseID);
AddClaimIfNotNull(claims, "imp", impersonatingUserDatabaseID);
var payload = new JwtPayload(
issuer: authUrl ?? "mockdomain.com",
audience: apiUrl ?? "mockdomain.com",
claims: claims,
expires: expiresUTC ?? DateTime.UtcNow.AddMinutes(30),
notBefore: notValidBeforeUTC ?? DateTime.UtcNow
);
var token = new JwtSecurityToken(header, payload);
return new JwtSecurityTokenHandler().WriteToken(token);
}
private static void AddClaimIfNotNull(List<Claim> claims, string type, string value)
{
if (value != null) { claims.Add(new Claim(type, value)); }
}
}
}