Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overrides of token issuer from environment variables #41945

Merged
merged 26 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26ad4f9
Support custom token issures
HarmanDhunna Feb 8, 2024
10fc3ba
v2
HarmanDhunna Feb 8, 2024
7651d40
Comments and clean up
HarmanDhunna Feb 13, 2024
f66245c
Clean up
HarmanDhunna Feb 13, 2024
504f0a9
Add token issuer versions to the trigger attribute
HarmanDhunna Feb 13, 2024
43be0d3
api updates
HarmanDhunna Feb 13, 2024
1ae2b25
Added tests and changed variable name to AuthenticationEvents__OIDCMe…
HarmanDhunna Feb 13, 2024
389c782
Reminder changes for var name change
HarmanDhunna Feb 13, 2024
0e95b4a
API Updates
HarmanDhunna Feb 13, 2024
cf8cce9
SpellCheck fix to OidcMetadataUrl
HarmanDhunna Feb 13, 2024
587b156
Updating cspell.json for oidc
HarmanDhunna Feb 14, 2024
58e3f7b
PR Recommendations
HarmanDhunna Feb 14, 2024
5df0b70
Updated tests
HarmanDhunna Feb 21, 2024
5657ef9
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
HarmanDhunna Feb 21, 2024
a610a18
API Updates
HarmanDhunna Feb 21, 2024
386e001
Missed the file.
HarmanDhunna Feb 21, 2024
16a8875
Modified how token validation validates
HarmanDhunna Feb 22, 2024
d43091a
Changing sign to back to async
HarmanDhunna Feb 26, 2024
44d4d4b
Update API with new Attribute and remove the old one. Will be a break…
HarmanDhunna Feb 26, 2024
0f5da7d
checked in Test files
HarmanDhunna Feb 26, 2024
3a6bd32
Pull request review recommendations
HarmanDhunna Feb 26, 2024
78e8d82
Update ChangeLog
HarmanDhunna Feb 27, 2024
c173327
Added error Logging for invalid tokens.
HarmanDhunna Mar 2, 2024
5e0b207
Minor changes
HarmanDhunna Mar 2, 2024
bbc50f3
Missing a using statment
HarmanDhunna Mar 2, 2024
8dcc036
Switched to using IsUnsafeSupportLoggingEnabled
HarmanDhunna Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
<PackageReference Update="Microsoft.Identity.Client.Broker" Version="4.56.0" />

<!-- TODO: Make sure this package is arch-board approved -->
<PackageReference Update="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.35.0" />
<PackageReference Update="Microsoft.IdentityModel.Tokens" Version="6.35.0" />
<PackageReference Update="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

### Features Added

- Updated token internal validation to utlizie Microsoft.IdentityModel.Protocols.OpenIdConnect to get OIDC configuration.

### Breaking Changes

- Removing 'TeantId' from the AuthenticationEventTrigger Attribute API configuration
- Adding 'AuthorizedPartyAppId', 'AuthorityUrl' to the AuthenticationEventTrigger Attribute API configuration
- Adding 'AuthenticationEvents__ShowPIIDataInLogsKey` (defaults to true) to configuration manager for logging PIIData in logs

### Bugs Fixed

### Other Changes

- Cleaned out unused methods.

## 1.0.0-beta.5 (2023-12-07)

### Bugs Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public partial class AuthenticationEventsTriggerAttribute : System.Attribute
{
public AuthenticationEventsTriggerAttribute() { }
public string AudienceAppId { get { throw null; } set { } }
public string TenantId { get { throw null; } set { } }
public string AuthorityUrl { get { throw null; } set { } }
public string AuthorizedPartyAppId { get { throw null; } set { } }
}
public partial class AuthenticationEventWebJobsStartup : Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Framework;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Listeners;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Microsoft.Azure.WebJobs.Host.Triggers;
using Microsoft.Extensions.Logging;

using static Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Framework.EmptyResponse;

using AuthenticationEventMetadata = Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Framework.AuthenticationEventMetadata;

namespace Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents
Expand Down Expand Up @@ -195,7 +199,12 @@ private AuthenticationEventRequestBase GetRequestForEvent(HttpRequestMessage req
}
else if (requestEvent.GetType() != _parameterInfo.ParameterType && ex == null && _parameterInfo.ParameterType != typeof(string))
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, AuthenticationEventResource.Ex_Parm_Mismatch, requestEvent.GetType(), _parameterInfo.ParameterType));
throw new Exception(
string.Format(
provider: CultureInfo.CurrentCulture,
format: AuthenticationEventResource.Ex_Parm_Mismatch,
arg0: requestEvent.GetType(),
arg1: _parameterInfo.ParameterType));
}

requestEvent.StatusMessage = ex == null ? AuthenticationEventResource.Status_Good : ex.Message;
Expand Down Expand Up @@ -224,18 +233,16 @@ private async Task<Dictionary<string, string>> GetClaimsAndValidateRequest(HttpR
return null;
}

TokenValidator validator = ConfigurationManager.EZAuthEnabled && requestMessage.Headers.Matches(ConfigurationManager.HEADER_EZAUTH_ICP, ConfigurationManager.HEADER_EZAUTH_ICP_VERIFY) ?
(TokenValidator)new TokenValidatorEZAuth() :
new TokenValidatorInternal();
TokenValidator validator = TokenValidatorHelper.IsEzAuthValid(requestMessage.Headers) ? new TokenValidatorEZAuth() : new TokenValidatorInternal();

(bool valid, Dictionary<string, string> claims) = await validator.GetClaimsAndValidate(requestMessage, configurationManager).ConfigureAwait(false);
if (valid)
try
{
return claims;
return await validator.ValidateAndGetClaims(requestMessage, configurationManager).ConfigureAwait(false);
}
else
catch (Exception exceptionIfFailed)
{
throw new UnauthorizedAccessException();
_configuration.Log(exceptionIfFailed.Message, logLevel: LogLevel.Error);
throw;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
Expand All @@ -15,6 +11,11 @@
using System.Threading.Tasks;
using System.Web;

using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents
{
/// <summary>The main configuration provider, this also handles the initial HTTP requests and response via IAsyncConverter.</summary>
Expand Down Expand Up @@ -49,10 +50,10 @@ public void Initialize(ExtensionConfigContext context)
//LogInformation(string.Format(AuthenticationEventResource.Log_EventHandler_Url, Uri));
}

internal void LogInformation(string message)
internal void Log(string message, LogLevel logLevel = LogLevel.Information, params object[] args)
{
Console.WriteLine(message);
_logger.LogInformation(message);
_logger.Log(logLevel, message, args);
}

internal void DisplayAzureFunctionInfoToConsole(string functionName)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<data name="Ex_Invalid_Action" xml:space="preserve">
<value>The action '{0}' is invalid, please use one of the following actions: '{1}'</value>
</data>
<data name="Ex_Invalid_AuthorizedPartyApplicationId" xml:space="preserve">
<value>Authorized Party Application ID '{0}' from token does match Authentication Event Trigger configuration AuthorizedPartyAppId '{1}'.</value>
</data>
<data name="Ex_Invalid_Event" xml:space="preserve">
<value>Invalid event type.</value>
</data>
Expand All @@ -159,6 +162,9 @@
<data name="Ex_Invalid_JsonPayload" xml:space="preserve">
<value>Invalid Json Payload</value>
</data>
<data name="Ex_Invalid_OIDC" xml:space="preserve">
<value>Not able to retrive Open ID Configuration with configured Authority URL '{0}'. Please verify Authority URL.</value>
</data>
<data name="Ex_Invalid_Response" xml:space="preserve">
<value>Response validation failed, see inner exceptions.</value>
</data>
Expand All @@ -168,6 +174,9 @@
<data name="Ex_Invalid_SchemaVersion" xml:space="preserve">
<value>Invalid version on Schema</value>
</data>
<data name="Ex_Invalid_Token" xml:space="preserve">
<value>No Access Token in request found.</value>
</data>
<data name="Ex_Leg_payload" xml:space="preserve">
<value>Cannot assign legacy payload to cloud events.</value>
</data>
Expand Down Expand Up @@ -204,8 +213,14 @@
<data name="Ex_Token_Version" xml:space="preserve">
<value>Invalid token version {0}, supported versions are: {1}</value>
</data>
<data name="Ex_Trigger_Required_Attrs" xml:space="preserve">
<value>Please supply both the TenantId and AudienceAppId in variables in your binding configuration. (Or app settings {0} and {1})</value>
<data name="Ex_Trigger_ApplicationId_Required" xml:space="preserve">
<value>Please supply the ApplicationId {0} in variables in your binding configuration.</value>
</data>
<data name="Ex_Trigger_AuthorityUrl_Required" xml:space="preserve">
<value>Please supply the Authority URL {0} in variables in your binding configuration.</value>
</data>
<data name="Ex_Trigger_AuthorizedPartyApplicationId_Required" xml:space="preserve">
<value>Please supply the AuthorizedPartyApplicationId {0} in variables in your binding configuration.</value>
</data>
<data name="Log_EventHandler_Url" xml:space="preserve">
<value>Listener registered at: {0}</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ public AuthenticationEventsTriggerAttribute()
{
}

/// <summary>Gets or sets the tenant identifier.</summary>
/// <value>The tenant identifier.</value>
public string TenantId { get; set; }
/// <summary>Gets or sets the Authorized Party application identifier.</summary>
/// <value>The app id would default to public cloud id</value>
public string AuthorizedPartyAppId { get; set; } = "99045fe1-7639-4a75-9d4a-577b6ca3810f";

/// <summary>Gets or sets the audience application identifier.</summary>
/// <value>The audience application identifier.</value>
public string AudienceAppId { get; set; }

/// <summary>
/// The authority is a URL that indicates the directory where the token came from
/// </summary>
public string AuthorityUrl { get; set; }

internal bool IsParameterString { get; set; } = true;
}
}
Loading