-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathTokenResponseExtensions.cs
More file actions
executable file
·85 lines (74 loc) · 2.48 KB
/
TokenResponseExtensions.cs
File metadata and controls
executable file
·85 lines (74 loc) · 2.48 KB
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
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Buffers.Text;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using Duende.IdentityModel;
using Duende.IdentityModel.Client;
namespace Client;
public static class TokenResponseExtensions
{
public static void Show(this TokenResponse response)
{
if (!response.IsError)
{
"Token response:".ConsoleGreen();
Console.WriteLine(response.Json);
if (response.AccessToken.Contains("."))
{
"\nAccess Token (decoded):".ConsoleGreen();
var parts = response.AccessToken.Split('.');
var header = parts[0];
var claims = parts[1];
Console.WriteLine(PrettyPrintJson(Encoding.UTF8.GetString(Base64Url.DecodeFromChars(header))));
Console.WriteLine(PrettyPrintJson(Encoding.UTF8.GetString(Base64Url.DecodeFromChars(claims))));
}
}
else
{
if (response.ErrorType == ResponseErrorType.Http)
{
"HTTP error: ".ConsoleGreen();
Console.WriteLine(response.Error);
"HTTP status code: ".ConsoleGreen();
Console.WriteLine(response.HttpStatusCode);
}
else
{
"Protocol error response:".ConsoleGreen();
Console.WriteLine(response.Raw);
}
}
}
public static string PrettyPrintJson(this string raw)
{
var doc = JsonDocument.Parse(raw).RootElement;
return JsonSerializer.Serialize(doc, new JsonSerializerOptions { WriteIndented = true });
}
}
public static class ConsoleExtensions
{
/// <summary>
/// Writes green text to the console.
/// </summary>
/// <param name="text">The text.</param>
[DebuggerStepThrough]
public static void ConsoleGreen(this string text)
{
text.ColoredWriteLine(ConsoleColor.Green);
}
/// <summary>
/// Writes out text with the specified ConsoleColor.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
[DebuggerStepThrough]
public static void ColoredWriteLine(this string text, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ResetColor();
}
}