-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathTokenResponseExtensions.cs
More file actions
55 lines (48 loc) · 1.74 KB
/
TokenResponseExtensions.cs
File metadata and controls
55 lines (48 loc) · 1.74 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
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Buffers.Text;
using System.Text;
using System.Text.Json;
using Duende.IdentityModel;
using Duende.IdentityModel.Client;
namespace Clients;
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 payload = parts[1];
Console.WriteLine(PrettyPrintJson(Encoding.UTF8.GetString(Base64Url.DecodeFromChars(header))));
Console.WriteLine(PrettyPrintJson(Encoding.UTF8.GetString(Base64Url.DecodeFromChars(payload))));
}
}
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 });
}
}