33using System . Reflection ;
44using System . Text . Json ;
55using System . Text . Json . Serialization ;
6- using Microsoft . Extensions . Configuration . UserSecrets ;
6+
77
88public class UserSecretsManager
99{
@@ -85,7 +85,7 @@ public static async Task StoreDefaultProject(UserDefaultedProject? value, Cancel
8585 }
8686 else
8787 {
88- secrets [ "DefaultProject" ] = JsonSerializer . Serialize ( value ) ;
88+ secrets [ "DefaultProject" ] = JsonSerializer . Serialize ( value , UserSecretsJsonSerializerContext . Default . UserDefaultedProject ) ;
8989 }
9090
9191 var secretsJson = ToJson ( secrets ) ;
@@ -103,7 +103,7 @@ public static async Task StoreDefaultProject(UserDefaultedProject? value, Cancel
103103
104104 try
105105 {
106- if ( JsonSerializer . Deserialize < UserDefaultedProject > ( projectJson ) is { } project )
106+ if ( JsonSerializer . Deserialize < UserDefaultedProject > ( projectJson , UserSecretsJsonSerializerContext . Default . UserDefaultedProject ) is { } project )
107107 {
108108 return project ;
109109 }
@@ -144,7 +144,7 @@ public static string GetAppDataPath()
144144 Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ,
145145 "blank" ,
146146 "tim" ,
147- Assembly . GetExecutingAssembly ( ) . GetCustomAttribute < UserSecretsIdAttribute > ( ) ! . UserSecretsId ,
147+ "tim-deploy" ,
148148 "secrets.json" ) ;
149149
150150 return path ;
@@ -198,15 +198,15 @@ private static string EscapeJsonString(string value)
198198 }
199199
200200 var response = await AuthClient . PostAsJsonAsync ( "/login/oauth/refresh" ,
201- new { refresh_token = currentSession . RefreshToken } , token ) ;
201+ new RefreshTokenRequest ( currentSession . RefreshToken ) , UserSecretsJsonSerializerContext . Default . RefreshTokenRequest , token ) ;
202202
203203 if ( ! response . IsSuccessStatusCode )
204204 {
205205 Console . WriteLine ( "Unable to refresh session" ) ;
206206 return null ;
207207 }
208208
209- var refreshResponse = await response . Content . ReadFromJsonAsync < RefreshTokenResponse > ( token ) ;
209+ var refreshResponse = await response . Content . ReadFromJsonAsync < RefreshTokenResponse > ( UserSecretsJsonSerializerContext . Default . RefreshTokenResponse , token ) ;
210210 if ( refreshResponse == null )
211211 {
212212 return null ;
@@ -226,12 +226,6 @@ private static string EscapeJsonString(string value)
226226 return await GetFloqSession ( token ) ;
227227 }
228228
229- private record RefreshTokenResponse (
230- [ property: JsonPropertyName ( "access_token" ) ]
231- string AccessToken ,
232- [ property: JsonPropertyName ( "expiry_date" ) ]
233- string ExpiryDate ) ;
234-
235229 public static async Task RemoveFloqSession ( CancellationToken token )
236230 {
237231 var secretsPath = GetAppDataPath ( ) ;
@@ -283,7 +277,28 @@ public string ExpireInFriendly
283277 }
284278}
285279
280+ public record RefreshTokenRequest (
281+ [ property: JsonPropertyName ( "refresh_token" ) ]
282+ string RefreshToken ) ;
283+
284+ public record RefreshTokenResponse (
285+ [ property: JsonPropertyName ( "access_token" ) ]
286+ string AccessToken ,
287+ [ property: JsonPropertyName ( "expiry_date" ) ]
288+ string ExpiryDate ) ;
289+
286290// Stored as JSON in file, nb, be backwards compatitble
287291public record UserDefaultedProject ( string Id , string Project , string Customer , string CustomerId ) ;
288292
289293public record ImplicitCallbackData ( string AccessToken , string ExpireDate , string RefreshToken , string UserEmail ) ;
294+
295+ [ JsonSourceGenerationOptions (
296+ PropertyNameCaseInsensitive = true ,
297+ PropertyNamingPolicy = JsonKnownNamingPolicy . CamelCase
298+ ) ]
299+ [ JsonSerializable ( typeof ( UserDefaultedProject ) ) ]
300+ [ JsonSerializable ( typeof ( RefreshTokenRequest ) ) ]
301+ [ JsonSerializable ( typeof ( RefreshTokenResponse ) ) ]
302+ internal partial class UserSecretsJsonSerializerContext : JsonSerializerContext
303+ {
304+ }
0 commit comments