33using Web . Services ;
44using Microsoft . AspNetCore . Mvc ;
55using Octokit ;
6+ using System . Security . Cryptography ;
67using System . Text ;
78
89namespace Web . Controllers
@@ -14,18 +15,38 @@ public class SyncController : Controller
1415 private readonly string _privateKeyPem ;
1516 private readonly string _clientId ;
1617 private readonly string _appId ;
18+ private readonly string ? _syncTriggerKey ;
1719 private readonly MicrosoftGraphService _microsoftGraph ;
1820 private readonly ILogger _logger ;
1921 public SyncController ( IConfiguration configuration , MicrosoftGraphService microsoftGraph , ILoggerFactory loggerFactory )
2022 {
2123 _privateKeyPem = Encoding . UTF8 . GetString ( Convert . FromBase64String ( configuration [ "GitHubProvisioning:PrivateKey" ] ) ) ;
2224 _clientId = configuration [ "GitHubProvisioning:ClientId" ] ;
2325 _appId = configuration [ "GitHubProvisioning:AppId" ] ;
26+ _syncTriggerKey = configuration [ "SyncTriggerKey" ] ;
2427 _microsoftGraph = microsoftGraph ;
2528 _logger = loggerFactory . CreateLogger < SyncController > ( ) ;
2629 }
2730 public async Task < IActionResult > Index ( )
2831 {
32+ if ( string . IsNullOrWhiteSpace ( _syncTriggerKey ) )
33+ {
34+ _logger . LogError ( "SyncTriggerKey is not configured. The /api/sync endpoint cannot be used." ) ;
35+ return new StatusCodeResult ( StatusCodes . Status500InternalServerError ) ;
36+ }
37+
38+ if ( ! Request . Headers . TryGetValue ( "X-Sync-Trigger-Key" , out var providedKey ) )
39+ {
40+ return new UnauthorizedResult ( ) ;
41+ }
42+
43+ if ( ! CryptographicOperations . FixedTimeEquals (
44+ Encoding . UTF8 . GetBytes ( providedKey . FirstOrDefault ( ) ?? string . Empty ) ,
45+ Encoding . UTF8 . GetBytes ( _syncTriggerKey ) ) )
46+ {
47+ return new UnauthorizedResult ( ) ;
48+ }
49+
2950 var appClient = new GitHubClient ( new ProductHeaderValue ( Constants . UserAgent ) , new GitHubAppCredentialStore ( long . Parse ( _appId ) , _privateKeyPem ) ) ; ;
3051 var installations = await appClient . GitHubApps . GetAllInstallationsForCurrent ( ) ;
3152
0 commit comments