@@ -10,12 +10,11 @@ namespace OneIdentity.SafeguardDotNet.PkceNoninteractiveLogin;
1010using System . Net . Http ;
1111using System . Security ;
1212using System . Text ;
13+ using System . Text . Json ;
1314using System . Threading ;
1415using System . Threading . Tasks ;
1516using System . Web ;
1617
17- using Newtonsoft . Json . Linq ;
18-
1918using Serilog ;
2019
2120/// <summary>
@@ -217,30 +216,34 @@ private static async Task HandleSecondaryAuthenticationAsync(
217216 SecureString secondaryPassword ,
218217 CancellationToken cancellationToken )
219218 {
220- JObject primaryResponse ;
219+ JsonDocument primaryResponse ;
221220 try
222221 {
223- primaryResponse = JObject . Parse ( primaryAuthBody ) ;
222+ primaryResponse = JsonDocument . Parse ( primaryAuthBody ) ;
224223 }
225224 catch
226225 {
227226 return ; // Non-JSON response means no secondary auth info
228227 }
229228
230- var secondaryProviderId = primaryResponse [ "SecondaryProviderID" ] ? . ToString ( ) ;
231-
232- if ( string . IsNullOrEmpty ( secondaryProviderId ) )
229+ using ( primaryResponse )
233230 {
234- return ; // No MFA required
235- }
231+ var root = primaryResponse . RootElement ;
232+ var secondaryProviderId = root . TryGetProperty ( "SecondaryProviderID" , out var spId ) ? spId . GetString ( ) : null ;
236233
237- Log . Debug ( "Secondary authentication required, provider: {SecondaryProviderId}" , secondaryProviderId ) ;
234+ if ( string . IsNullOrEmpty ( secondaryProviderId ) )
235+ {
236+ return ; // No MFA required
237+ }
238238
239- if ( secondaryPassword == null )
240- {
241- throw new SafeguardDotNetException (
242- $ "Multi-factor authentication is required (provider: { secondaryProviderId } ) " +
243- "but no secondary password was provided. Use the secondaryPassword parameter to supply the one-time code." ) ;
239+ Log . Debug ( "Secondary authentication required, provider: {SecondaryProviderId}" , secondaryProviderId ) ;
240+
241+ if ( secondaryPassword == null )
242+ {
243+ throw new SafeguardDotNetException (
244+ $ "Multi-factor authentication is required (provider: { secondaryProviderId } ) " +
245+ "but no secondary password was provided. Use the secondaryPassword parameter to supply the one-time code." ) ;
246+ }
244247 }
245248
246249 cancellationToken . ThrowIfCancellationRequested ( ) ;
@@ -255,9 +258,10 @@ private static async Task HandleSecondaryAuthenticationAsync(
255258 {
256259 try
257260 {
258- var initResponse = JObject . Parse ( initBody ) ;
259- mfaState = initResponse [ "State" ] ? . ToString ( ) ?? string . Empty ;
260- var mfaMessage = initResponse [ "Message" ] ? . ToString ( ) ;
261+ using var initResponse = JsonDocument . Parse ( initBody ) ;
262+ var initRoot = initResponse . RootElement ;
263+ mfaState = initRoot . TryGetProperty ( "State" , out var stateEl ) ? stateEl . GetString ( ) ?? string . Empty : string . Empty ;
264+ var mfaMessage = initRoot . TryGetProperty ( "Message" , out var msgEl ) ? msgEl . GetString ( ) : null ;
261265 if ( ! string . IsNullOrEmpty ( mfaMessage ) )
262266 {
263267 Log . Debug ( "MFA prompt: {Message}" , mfaMessage ) ;
@@ -286,8 +290,8 @@ private static async Task HandleSecondaryAuthenticationAsync(
286290 var errorMessage = "Secondary authentication failed." ;
287291 try
288292 {
289- var mfaResponse = JObject . Parse ( mfaBody ) ;
290- errorMessage = mfaResponse [ "Message" ] ? . ToString ( ) ?? errorMessage ;
293+ using var mfaResponse = JsonDocument . Parse ( mfaBody ) ;
294+ errorMessage = mfaResponse . RootElement . TryGetProperty ( "Message" , out var mEl ) ? mEl . GetString ( ) ?? errorMessage : errorMessage ;
291295 }
292296 catch
293297 {
@@ -315,8 +319,8 @@ private static string ExtractAuthorizationCode(string response)
315319 string authorizationCode ;
316320 try
317321 {
318- var jsonObject = JObject . Parse ( response ) ;
319- var relyingPartyUrl = jsonObject [ "RelyingPartyUrl" ] ? . ToString ( ) ;
322+ using var jsonDoc = JsonDocument . Parse ( response ) ;
323+ var relyingPartyUrl = jsonDoc . RootElement . TryGetProperty ( "RelyingPartyUrl" , out var rpEl ) ? rpEl . GetString ( ) : null ;
320324
321325 if ( string . IsNullOrEmpty ( relyingPartyUrl ) )
322326 {
@@ -364,11 +368,16 @@ private static async Task<string> ResolveIdentityProviderAsync(
364368 cancellationToken )
365369 . ConfigureAwait ( false ) ;
366370
367- var jProviders = JArray . Parse ( response ) ;
368371 var knownScopes = new List < ( string RstsProviderId , string Name , string RstsProviderScope ) > ( ) ;
369- if ( jProviders != null )
372+ using ( var jProviders = JsonDocument . Parse ( response ) )
370373 {
371- knownScopes = jProviders . Select ( s => ( Id : s [ "RstsProviderId" ] . ToString ( ) , Name : s [ "Name" ] . ToString ( ) , Scope : s [ "RstsProviderScope" ] . ToString ( ) ) ) . ToList ( ) ;
374+ foreach ( var item in jProviders . RootElement . EnumerateArray ( ) )
375+ {
376+ var id = item . TryGetProperty ( "RstsProviderId" , out var idEl ) ? idEl . GetString ( ) : null ;
377+ var name = item . TryGetProperty ( "Name" , out var nameEl ) ? nameEl . GetString ( ) : null ;
378+ var providerScope = item . TryGetProperty ( "RstsProviderScope" , out var scopeEl ) ? scopeEl . GetString ( ) : null ;
379+ knownScopes . Add ( ( id , name , providerScope ) ) ;
380+ }
372381 }
373382
374383 // try to match what the user typed for provider to an rSTS ID
0 commit comments