Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit c93f018

Browse files
Add persisted key and refresh token
1 parent 3c64bc4 commit c93f018

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

clients/ConsoleClientWithBrowserAndDPoP/Program.cs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Serilog;
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Net.Http;
67
using System.Text.Json;
78
using System.Threading.Tasks;
@@ -12,14 +13,11 @@ namespace ConsoleClientWithBrowserAndDPoP
1213
{
1314
public class Program
1415
{
15-
static string _api = "https://demo.duendesoftware.com/api/dpop/test";
16-
//static string _api = "https://localhost:5002/api/dpop/test";
17-
18-
static string authority = "https://demo.duendesoftware.com";
19-
//static string authority = "https://localhost:5001";
16+
static readonly string Api = "https://demo.duendesoftware.com/api/dpop/test";
17+
static readonly string Authority = "https://demo.duendesoftware.com";
2018

21-
static OidcClient _oidcClient;
22-
static HttpClient _apiClient = new HttpClient { BaseAddress = new Uri(_api) };
19+
private static OidcClient _oidcClient;
20+
private static HttpClient _apiClient = new HttpClient { BaseAddress = new Uri(Api) };
2321

2422
public static async Task Main()
2523
{
@@ -35,18 +33,17 @@ public static async Task Main()
3533

3634
private static async Task SignIn()
3735
{
38-
// create a redirect URI using an available port on the loopback address.
39-
// requires the OP to allow random ports on 127.0.0.1 - otherwise set a static port
4036
var browser = new SystemBrowser();
4137
string redirectUri = string.Format($"http://127.0.0.1:{browser.Port}");
4238

43-
var proofKey = JsonWebKeys.CreateRsaJson();
39+
var proofKey = GetProofKey();
40+
4441
var tokenDpopHandler = new ProofTokenMessageHandler(proofKey, new SocketsHttpHandler());
4542
var apiDpopHandler = new ProofTokenMessageHandler(proofKey, new SocketsHttpHandler());
4643

4744
var options = new OidcClientOptions
4845
{
49-
Authority = authority,
46+
Authority = Authority,
5047
ClientId = "native.dpop",
5148
RedirectUri = redirectUri,
5249
Scope = "openid profile api offline_access",
@@ -66,15 +63,53 @@ private static async Task SignIn()
6663
options.LoggerFactory.AddSerilog(serilog);
6764

6865
_oidcClient = new OidcClient(options);
69-
var result = await _oidcClient.LoginAsync(new LoginRequest());
7066

71-
_apiClient = new HttpClient(result.RefreshTokenHandler)
67+
LoginResult result = null;
68+
if (File.Exists("refresh_token"))
7269
{
73-
BaseAddress = new Uri(_api)
74-
};
70+
var refreshToken = File.ReadAllText("refresh_token");
71+
72+
var handler = new RefreshTokenDelegatingHandler(
73+
_oidcClient,
74+
null,
75+
refreshToken,
76+
"DPoP",
77+
apiDpopHandler);
78+
79+
_apiClient = new HttpClient(handler)
80+
{
81+
BaseAddress = new Uri(Api)
82+
};
83+
84+
await NextSteps();
85+
}
86+
else
87+
{
88+
result = await _oidcClient.LoginAsync(new LoginRequest());
89+
File.WriteAllText("refresh_token", result.TokenResponse.RefreshToken);
90+
91+
_apiClient = new HttpClient(result.RefreshTokenHandler)
92+
{
93+
BaseAddress = new Uri(Api)
94+
};
95+
}
96+
97+
7598

7699
ShowResult(result);
77-
await NextSteps(result);
100+
await NextSteps();
101+
}
102+
103+
private static string GetProofKey()
104+
{
105+
if (File.Exists("proofkey"))
106+
{
107+
return File.ReadAllText("proofkey");
108+
}
109+
110+
var proofKey = JsonWebKeys.CreateRsaJson();
111+
File.WriteAllText("proofkey", proofKey);
112+
return proofKey;
78113
}
79114

80115
private static void ShowResult(LoginResult result)
@@ -100,7 +135,7 @@ private static void ShowResult(LoginResult result)
100135
}
101136
}
102137

103-
private static async Task NextSteps(LoginResult result)
138+
private static async Task NextSteps()
104139
{
105140
var menu = " x...exit c...call api ";
106141

0 commit comments

Comments
 (0)