22using Serilog ;
33using System ;
44using System . Collections . Generic ;
5+ using System . IO ;
56using System . Net . Http ;
67using System . Text . Json ;
78using 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