Skip to content

Commit 10fb922

Browse files
committed
more logs added
1 parent 2888f51 commit 10fb922

File tree

3 files changed

+131
-23
lines changed

3 files changed

+131
-23
lines changed

Auth/AuthResponseService.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,54 @@ public static AuthResponse Build(SatResponse satResponse, ICredential credential
5757
if (satResponse.IsSuccessStatusCode)
5858
{
5959
var envelope = XmlSerializerService.Deserialize<AuthEnvelope>(satResponse.RawResponse!);
60-
return new AuthResponse
60+
var tokenValue = envelope?.Body?.AutenticaResponse?.AutenticaResult;
61+
62+
var authResponse = new AuthResponse
6163
{
6264
Succeeded = true,
6365
SatStatus = SatStatus.RequestSucceeded,
6466
SatStatusCode = SatStatus.RequestSucceeded.ToEnumCode(),
6567
SatMessage = "",
66-
TokenValue = envelope?.Body?.AutenticaResponse?.AutenticaResult,
68+
TokenValue = tokenValue,
6769
ValidFrom = envelope?.Header?.Security?.Timestamp?.Created,
6870
ValidTo = envelope?.Header?.Security?.Timestamp?.Expires,
6971
RawRequest = satResponse.RawRequest,
7072
RawResponse = satResponse.RawResponse,
7173
Tin = credential.Certificate.Rfc
7274
};
75+
76+
// Critical logging: Verify token was received
77+
if (string.IsNullOrWhiteSpace(tokenValue))
78+
{
79+
logger.LogWarning(
80+
"Authentication succeeded but token is missing. RFC: {Rfc}, HasEnvelope: {HasEnvelope}, HasBody: {HasBody}, HasAutenticaResponse: {HasAutenticaResponse}",
81+
credential.Certificate.Rfc,
82+
envelope != null,
83+
envelope?.Body != null,
84+
envelope?.Body?.AutenticaResponse != null);
85+
}
86+
else
87+
{
88+
logger.LogInformation(
89+
"Authentication succeeded. RFC: {Rfc}, TokenLength: {TokenLength}, ValidFrom: {ValidFrom}, ValidTo: {ValidTo}",
90+
credential.Certificate.Rfc,
91+
tokenValue.Length,
92+
authResponse.ValidFrom,
93+
authResponse.ValidTo);
94+
}
95+
96+
return authResponse;
7397
}
7498

7599
if (satResponse.RawResponse is not null && satResponse.RawResponse.ToLowerInvariant().Contains("fault"))
76100
{
77101
var faultEnvelope = XmlSerializerService.Deserialize<AuthFaultEnvelope>(satResponse.RawResponse);
102+
logger.LogError(
103+
"Authentication failed with SOAP fault. RFC: {Rfc}, FaultCode: {FaultCode}, FaultMessage: {FaultMessage}",
104+
credential.Certificate.Rfc,
105+
faultEnvelope?.Body?.Fault?.FaultCode,
106+
faultEnvelope?.Body?.Fault?.FaultMessage);
107+
78108
return new AuthResponse
79109
{
80110
Succeeded = false,
@@ -86,6 +116,12 @@ public static AuthResponse Build(SatResponse satResponse, ICredential credential
86116
};
87117
}
88118

119+
logger.LogError(
120+
"Authentication failed with unexpected status. RFC: {Rfc}, HttpStatusCode: {StatusCode}, ReasonPhrase: {ReasonPhrase}",
121+
credential.Certificate.Rfc,
122+
satResponse.HttpStatusCode,
123+
satResponse.ReasonPhrase);
124+
89125
return new AuthResponse
90126
{
91127
Succeeded = false,

Auth/AuthService.cs

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,63 @@ public AuthService(ILogger<AuthService> logger)
5353
public async Task<AuthResponse> AuthenticateAsync(ICredential credential,
5454
ILogger logger, CancellationToken cancellationToken = default)
5555
{
56-
// Generate Sat XML security token ID
57-
var uuid = CreateSecurityToken();
56+
logger.LogInformation("Starting SAT authentication process for RFC: {Rfc}", credential.Certificate.Rfc);
5857

59-
// Create digest and signature using unified template
60-
var digest = CreateDigest(credential);
61-
var signature = CreateSignature(digest, credential, uuid);
62-
63-
// Build SOAP envelope
64-
var authXml = BuildEnvelope(digest, uuid, credential.Certificate.RawDataBytes.ToBase64String(),
65-
signature);
66-
67-
// Send request
68-
var satResponse = await SendRequestAsync(
69-
url: SatUrl.AuthUrl,
70-
action: SatUrl.AuthAction,
71-
payload: authXml,
72-
cancellationToken: cancellationToken);
73-
74-
// Map response
75-
var authResponse = AuthResponseService.Build(satResponse, credential, logger);
76-
77-
return authResponse;
58+
try
59+
{
60+
// Generate Sat XML security token ID
61+
var uuid = CreateSecurityToken();
62+
logger.LogDebug("Generated security token UUID: {Uuid}", uuid);
63+
64+
// Create digest and signature using unified template
65+
var digest = CreateDigest(credential);
66+
var signature = CreateSignature(digest, credential, uuid);
67+
68+
// Build SOAP envelope
69+
var authXml = BuildEnvelope(digest, uuid, credential.Certificate.RawDataBytes.ToBase64String(),
70+
signature);
71+
72+
// Send request
73+
logger.LogInformation("Sending authentication request to SAT. URL: {Url}", SatUrl.AuthUrl);
74+
var satResponse = await SendRequestAsync(
75+
url: SatUrl.AuthUrl,
76+
action: SatUrl.AuthAction,
77+
payload: authXml,
78+
cancellationToken: cancellationToken);
79+
80+
logger.LogInformation("SAT authentication response received. Success: {IsSuccessStatusCode}, Status: {StatusCode}",
81+
satResponse.IsSuccessStatusCode,
82+
satResponse.HttpStatusCode);
83+
84+
// Log complete XML response for debugging
85+
if (!satResponse.IsSuccessStatusCode || string.IsNullOrWhiteSpace(satResponse.RawResponse))
86+
{
87+
logger.LogError(
88+
"SAT authentication failed. RFC: {Rfc}, StatusCode: {StatusCode}, ReasonPhrase: {ReasonPhrase}, RawResponse: {RawResponse}",
89+
credential.Certificate.Rfc,
90+
satResponse.HttpStatusCode,
91+
satResponse.ReasonPhrase,
92+
satResponse.RawResponse ?? "[Empty Response]");
93+
}
94+
else
95+
{
96+
logger.LogDebug(
97+
"SAT authentication raw response XML. RFC: {Rfc}, ResponseLength: {Length}, RawResponse: {RawResponse}",
98+
credential.Certificate.Rfc,
99+
satResponse.RawResponse?.Length ?? 0,
100+
satResponse.RawResponse);
101+
}
102+
103+
// Map response
104+
var authResponse = AuthResponseService.Build(satResponse, credential, logger);
105+
106+
return authResponse;
107+
}
108+
catch (Exception ex)
109+
{
110+
logger.LogError(ex, "Error during SAT authentication process for RFC: {Rfc}", credential.Certificate.Rfc);
111+
throw;
112+
}
78113
}
79114

80115
/// <summary>

XmlDownloaderService.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,36 @@ public async Task<AuthResponse> AuthenticateAsync(string base64Cer, string base6
130130
public async Task<AuthResponse> AuthenticateAsync(ICredential credential,
131131
CancellationToken cancellationToken = default)
132132
{
133+
_logger.LogInformation("Starting authentication for RFC: {Rfc}", credential.Certificate.Rfc);
134+
133135
var authResponse = await _authService.AuthenticateAsync(
134136
credential: credential,
135137
logger: _logger,
136138
cancellationToken: cancellationToken);
137139

138140
Token = authResponse.Token;
139141
Credential = credential;
142+
143+
// Critical logging: Authentication result
144+
if (!authResponse.Succeeded)
145+
{
146+
_logger.LogError("Authentication failed. RFC: {Rfc}, Status: {StatusCode}, Message: {Message}",
147+
credential.Certificate.Rfc,
148+
authResponse.SatStatusCode,
149+
authResponse.SatMessage);
150+
}
151+
else if (Token == null)
152+
{
153+
_logger.LogError(
154+
"Authentication succeeded but token is null. RFC: {Rfc}, TokenValue is null or empty: {IsNullOrEmpty}",
155+
credential.Certificate.Rfc,
156+
string.IsNullOrWhiteSpace(authResponse.TokenValue));
157+
}
158+
else
159+
{
160+
_logger.LogInformation("Authentication succeeded. RFC: {Rfc}, Token is valid", credential.Certificate.Rfc);
161+
}
162+
140163
return authResponse;
141164
}
142165

@@ -149,8 +172,18 @@ public async Task<AuthResponse> AuthenticateAsync(ICredential credential,
149172
public async Task<QueryResponse> CreateRequestAsync(QueryParameters parameters,
150173
CancellationToken cancellationToken = default)
151174
{
175+
// Critical logging: Check token before proceeding
176+
if (Token == null || Credential == null)
177+
{
178+
_logger.LogError("Cannot create download request: Token or Credential is null. Token is null: {TokenIsNull}, Credential is null: {CredentialIsNull}",
179+
Token == null,
180+
Credential == null);
181+
}
182+
152183
EnsureAuthToken();
153184

185+
_logger.LogInformation("Creating download request for RFC: {Rfc}", Credential!.Certificate.Rfc);
186+
154187
return await _queryService.CreateAsync(
155188
credential: Credential!,
156189
authToken: Token!,
@@ -470,11 +503,15 @@ private void EnsureAuthToken()
470503
{
471504
if (Token == null)
472505
{
506+
_logger.LogError("EnsureAuthToken failed: Token is null. Credential is null: {CredentialIsNull}",
507+
Credential == null);
473508
throw new InvalidOperationException("Authentication token is required. Please authenticate first.");
474509
}
475510

476511
if (Credential == null)
477512
{
513+
_logger.LogError("EnsureAuthToken failed: Credential is null. Token is null: {TokenIsNull}",
514+
Token == null);
478515
throw new InvalidOperationException("Credential is required. Please authenticate first.");
479516
}
480517
}

0 commit comments

Comments
 (0)