@@ -255,44 +255,54 @@ public async Task<IActionResult> Token()
255255 }
256256
257257 // First, check system-level credentials (timing-safe comparison)
258- if ( Config . SecurityConfig . SystemLoginCredentials . ContainsKey ( request . ClientId ) &&
259- FixedTimeSecretEquals ( Config . SecurityConfig . SystemLoginCredentials [ request . ClientId ] , request . ClientSecret ) )
260- {
261- audit . Successful = true ;
262- await _systemAuditsService . SaveSystemAuditAsync ( audit ) ;
258+ if ( Config . SecurityConfig . SystemLoginCredentials . ContainsKey ( request . ClientId ) &&
259+ FixedTimeSecretEquals ( Config . SecurityConfig . SystemLoginCredentials [ request . ClientId ] , request . ClientSecret ) )
260+ {
261+ audit . Successful = true ;
262+ await _systemAuditsService . SaveSystemAuditAsync ( audit ) ;
263263
264- // Create a system-level service principal with all claims
265- var identity = new ClaimsIdentity (
266- OpenIddictServerAspNetCoreDefaults . AuthenticationScheme ,
267- Claims . Name ,
268- Claims . Role ) ;
264+ // Create a system-level service principal with all claims
265+ var identity = new ClaimsIdentity (
266+ OpenIddictServerAspNetCoreDefaults . AuthenticationScheme ,
267+ Claims . Name ,
268+ Claims . Role ) ;
269269
270- identity . AddClaim ( new Claim ( Claims . Subject , $ "system_{ request . ClientId } ")
271- . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
272- identity . AddClaim ( new Claim ( Claims . Name , request . ClientId )
273- . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
270+ identity . AddClaim ( new Claim ( Claims . Subject , $ "system_{ request . ClientId } ")
271+ . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
272+ identity . AddClaim ( new Claim ( Claims . Name , $ "System Account ({ request . ClientId } )")
273+ . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
274+ identity . AddClaim ( new Claim ( ClaimTypes . PrimarySid , $ "system_{ request . ClientId } ")
275+ . SetDestinations ( Destinations . AccessToken ) ) ;
276+ identity . AddClaim ( new Claim ( ClaimTypes . PrimaryGroupSid , "0" )
277+ . SetDestinations ( Destinations . AccessToken ) ) ;
278+ identity . AddClaim ( new Claim ( ClaimTypes . GivenName , "SMTP Relay System" )
279+ . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
280+ identity . AddClaim ( new Claim ( ResgridClaimTypes . Data . DisplayName , "SMTP Relay System" )
281+ . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
282+ identity . AddClaim ( new Claim ( ResgridClaimTypes . Data . ServiceAccount , "true" )
283+ . SetDestinations ( Destinations . AccessToken , Destinations . IdentityToken ) ) ;
274284
275- // Add all resource claims for full access
276- AddAllResourceClaims ( identity ) ;
285+ // Add all resource claims for full access
286+ AddAllResourceClaims ( identity ) ;
277287
278- var principal = new ClaimsPrincipal ( identity ) ;
288+ var principal = new ClaimsPrincipal ( identity ) ;
279289
280- principal . SetScopes ( new [ ]
281- {
282- Scopes . OpenId ,
283- Scopes . Email ,
284- Scopes . Profile
285- } . Intersect ( request . GetScopes ( ) ) ) ;
290+ principal . SetScopes ( new [ ]
291+ {
292+ Scopes . OpenId ,
293+ Scopes . Email ,
294+ Scopes . Profile
295+ } . Intersect ( request . GetScopes ( ) ) ) ;
286296
287- principal . SetAccessTokenLifetime ( TimeSpan . FromMinutes ( OidcConfig . AccessTokenExpiryMinutes ) ) ;
297+ principal . SetAccessTokenLifetime ( TimeSpan . FromMinutes ( OidcConfig . AccessTokenExpiryMinutes ) ) ;
288298
289- return SignIn ( principal , OpenIddictServerAspNetCoreDefaults . AuthenticationScheme ) ;
290- }
299+ return SignIn ( principal , OpenIddictServerAspNetCoreDefaults . AuthenticationScheme ) ;
300+ }
291301
292- // Second, try department-level credentials via department code + shared secret
293- var department = await _departmentsService . GetDepartmentByNameAsync ( request . ClientId ) ;
302+ // Second, try department-level credentials via department code + shared secret
303+ var department = await _departmentsService . GetDepartmentByNameAsync ( request . ClientId ) ;
294304
295- if ( department == null )
305+ if ( department == null )
296306 {
297307 await _systemAuditsService . SaveSystemAuditAsync ( audit ) ;
298308
0 commit comments