-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJwtCreateForm.cs
131 lines (108 loc) · 4.72 KB
/
JwtCreateForm.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Text.Json;
using Microsoft.IdentityModel.Tokens;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
namespace DoorDashAPIWinFormsApp
{
public partial class JwtCreateForm : Form
{
readonly Dictionary<string, string> JwtHeaderTemplate;
readonly Dictionary<string, string> JwtPayloadTemplate;
readonly JsonSerializerOptions JsonSerializerOptionsDefault;
public JwtCreateForm()
{
InitializeComponent();
JwtHeaderTemplate = new Dictionary<string, string>
{
{"alg", "HS256"},
{"typ", "JWT"},
{"dd-ver", "DD-JWT-V1"}
};
JwtPayloadTemplate = new Dictionary<string, string>
{
{"aud", "doordash"},
{"iss", "DEVELOPER_ID"},
{"kid", "KEY_ID"},
{"iat", "ISSUE_AT"},
{"exp", "EXPIRATION"}
};
JsonSerializerOptionsDefault = new JsonSerializerOptions
{
WriteIndented = true
};
}
private void JwtCreateForm_Load(object sender, EventArgs e)
{
SetDefaultValues();
UpdateJwtButton.Select();
}
private void SetDefaultValues()
{
JwtHeaderTextBox.Text = JsonSerializer.Serialize(JwtHeaderTemplate, JsonSerializerOptionsDefault);
JwtPayloadTextBox.Text = JsonSerializer.Serialize(JwtPayloadTemplate, JsonSerializerOptionsDefault);
JwtTextBox.Text = string.Empty;
}
private void UpdateJWT()
{
// Do not run update if user provided values are not valid.
// Note, function doesn't test if values provided are correct size.
if (!UserProvidedValuesValid())
{
SetDefaultValues();
return;
}
var decodedSecret = Base64UrlEncoder.DecodeBytes(SigningSecretTextBox.Text.Trim());
var securityKey = new SymmetricSecurityKey(decodedSecret);
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var algorithmMap = new Dictionary<string, string> { { "alg", "HS256" } };
var tokenType = "JWT";
var doorDashHeader = new Dictionary<string, object> { { "dd-ver", "DD-JWT-V1" } };
var jwtHeader = new JwtHeader(credentials, algorithmMap, tokenType, doorDashHeader);
JwtHeaderTextBox.Text = JsonSerializer.Serialize(jwtHeader, JsonSerializerOptionsDefault);
// creates new JWT Payload that expires in 30 minutes
var jwtPayload = new JwtPayload(
issuer: DeveloperIdTextBox.Text.Trim(),
audience: "doordash",
claims: new List<Claim> { new Claim("kid", KeyIdTextBox.Text.Trim()) },
notBefore: null,
expires: System.DateTime.UtcNow.AddSeconds(1800),
issuedAt: System.DateTime.UtcNow);
JwtPayloadTextBox.Text = JsonSerializer.Serialize(jwtPayload, JsonSerializerOptionsDefault);
// sign and create new token
var securityToken = new JwtSecurityToken(jwtHeader, jwtPayload);
var jwt = new JwtSecurityTokenHandler().WriteToken(securityToken);
JwtSignatureTextBox.Text = JwtSignature(jwt);
JwtTextBox.Text = jwt;
}
private void UpdateJwtButton_Click(object sender, EventArgs e)
{
UpdateJWT();
}
// Returns true if 3 user provided values are not null, empty or just whitespace chars.
private bool UserProvidedValuesValid() =>
!string.IsNullOrWhiteSpace(KeyIdTextBox.Text) &&
!string.IsNullOrWhiteSpace(DeveloperIdTextBox.Text) &&
!string.IsNullOrWhiteSpace(SigningSecretTextBox.Text);
// Returns signauture from JWT string provided.
// Empty string returned if null, empty, or invalid JWT provided.
private string JwtSignature(string jwt)
{
if (jwt.IsNullOrEmpty()) return string.Empty;
string[] parts = jwt.Split('.');
if (parts.Length != 3) return string.Empty;
return parts[2];
}
private void DeveloperIdTextBox_Leave(object sender, EventArgs e)
{
UpdateJWT();
}
private void KeyIdTextBox_Leave(object sender, EventArgs e)
{
UpdateJWT();
}
private void SigningSecretTextBox_Leave(object sender, EventArgs e)
{
UpdateJWT();
}
}
}