-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorizationDetails.cs
118 lines (108 loc) · 3.93 KB
/
AuthorizationDetails.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
using Kerb.AuthTester.Authorization;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace Kerb.AuthTester
{
public partial class AuthorizationDetails : Form
{
private const string AUTH_START = "Authorization: ";
private string auth;
private AuthorizationMessage? AuthMsg;
public AuthorizationDetails(string authHeader)
{
auth = authHeader;
InitializeComponent();
Cursor = Cursors.WaitCursor;
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
private void AuthorizationDetails_Load(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(auth) || !auth.StartsWith(AUTH_START))
{
MessageBox.Show($"Message should be in correct format, ({AUTH_START}) is missing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
Close();
}
AuthMsg = AuthorizationMessageFactory.Build(auth.Split(AUTH_START)[1]);
try
{
var ty = AuthMsg.GetType();
var b = JsonSerializer.SerializeToDocument(AuthMsg, ty);
FillTree(b);
treeDetails.SelectedNode = treeDetails.Nodes[0];
}
catch (Exception ex)
{
MessageBox.Show("Error parsing ticket. Details window will be closed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
Close();
}
Cursor = Cursors.Default;
}
private void EnumerateJson(TreeNodeCollection targetTree, JsonElement jsonElement)
{
if (jsonElement.ValueKind != JsonValueKind.Array && jsonElement.ValueKind != JsonValueKind.Object)
{
targetTree.Add(new TreeNode($"Value: {jsonElement}"));
return;
}
if (jsonElement.ValueKind == JsonValueKind.Array)
{
foreach (var item in jsonElement.EnumerateArray())
{
var newTree = new TreeNode("Values");
targetTree.Add(newTree);
EnumerateJson(newTree.Nodes, item);
}
return;
}
foreach (var item in jsonElement.EnumerateObject())
{
var newTree = new TreeNode($"{item.Name}");
targetTree.Add(newTree);
EnumerateJson(newTree.Nodes, item.Value);
}
}
private void FillTree(JsonDocument doc)
{
treeDetails.Nodes.Clear();
EnumerateJson(treeDetails.Nodes, doc.RootElement);
treeDetails.ExpandAll();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (AuthMsg == null)
{
return;
}
try
{
var saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.FileName = string.Format("{1}_{0}.json", AuthMsg.AuthorizationType.ToString(), DateTime.Now.ToString("yyyymmddhhmmss"));
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using StreamWriter streamWriter = File.CreateText(saveFileDialog1.FileName);
var ty = AuthMsg.GetType();
var a = JsonSerializer.Serialize(AuthMsg, ty);
streamWriter.Write(a);
}
}
catch (Exception ex)
{
MessageBox.Show("Saving file failed.\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
}