Skip to content

Commit e1367a3

Browse files
authored
Merge pull request #75 from microsoftgraph/validate-azp-claim
Added check for `azp` claim in validation token
2 parents feab41a + 6c19cfe commit e1367a3

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎src/Services/M365AppConfigService.cs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace GitHubConnector.Services;
2020
/// <param name="settings">The application settings.</param>
2121
public class M365AppConfigService(AppSettings settings)
2222
{
23+
private static readonly string GraphNotificationPublisherId = "0bf30f3b-4a52-48df-9a82-234910c4a086";
24+
2325
private readonly HttpListener listener = new();
2426
private readonly int port = settings.PortNumber;
2527
private readonly string tenantId = settings.TenantId ?? throw new ArgumentException("tenantId not set in app settings");
@@ -79,23 +81,31 @@ public async Task<bool> ValidateTokensAsync(List<string>? tokens)
7981
{
8082
try
8183
{
82-
handler.ValidateToken(
84+
var principal = handler.ValidateToken(
8385
token,
8486
new()
8587
{
8688
ValidateIssuer = true,
8789
ValidateAudience = true,
8890
ValidateIssuerSigningKey = true,
8991
ValidateLifetime = true,
90-
ValidIssuers = new[]
91-
{
92+
ValidIssuers =
93+
[
9294
$"https://login.microsoftonline.com/{tenantId}/v2.0",
9395
$"https://sts.windows.net/{tenantId}/",
94-
},
96+
],
9597
ValidAudience = clientId,
9698
IssuerSigningKeys = openIdConfig.SigningKeys,
9799
},
98100
out _);
101+
102+
var azpClaim = principal?.FindFirst("azp");
103+
if (azpClaim != null && string.Compare(azpClaim.Value, GraphNotificationPublisherId, StringComparison.OrdinalIgnoreCase) == 0)
104+
{
105+
continue;
106+
}
107+
108+
return false;
99109
}
100110
catch (SecurityTokenValidationException)
101111
{

0 commit comments

Comments
 (0)