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

feat(extensions): Add possibility to add scheme names for local fake auth. #15

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
81 changes: 79 additions & 2 deletions src/Zitadel/Authentication/AuthenticationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,48 @@ public static AuthenticationBuilder AddZitadelAuthenticationHandler(
public static AuthenticationBuilder AddFakeZitadel(
this AuthenticationBuilder builder,
Action<LocalFakeZitadelOptions>? configureOptions)
=> builder.AddFakeZitadel(
ZitadelDefaults.FakeAuthenticationScheme,
configureOptions);

/// <summary>
/// Add a "fake" zitadel authentication. This should only be used for local
/// development to fake an authentication/authorization. All calls are authenticated
/// by default. If (e.g. for testing reasons) a specific call should NOT be authenticated,
/// attach the header "x-zitadel-fake-auth" with the value "false" to the request.
/// This specific request will then fail to authenticate.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to configure.</param>
/// <param name="authenticationScheme">The name for the authentication scheme to be used.</param>
/// <param name="configureOptions">Action to configure the <see cref="LocalFakeZitadelOptions"/>.</param>
/// <returns>The configured <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddFakeZitadel(
this AuthenticationBuilder builder,
string authenticationScheme,
Action<LocalFakeZitadelOptions>? configureOptions)
=> builder.AddFakeZitadel(authenticationScheme, ZitadelDefaults.FakeDisplayName, configureOptions);

/// <summary>
/// Add a "fake" zitadel authentication. This should only be used for local
/// development to fake an authentication/authorization. All calls are authenticated
/// by default. If (e.g. for testing reasons) a specific call should NOT be authenticated,
/// attach the header "x-zitadel-fake-auth" with the value "false" to the request.
/// This specific request will then fail to authenticate.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to configure.</param>
/// <param name="authenticationScheme">The name for the authentication scheme to be used.</param>
/// <param name="displayName">The display name for the authentication scheme.</param>
/// <param name="configureOptions">Action to configure the <see cref="LocalFakeZitadelOptions"/>.</param>
/// <returns>The configured <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddFakeZitadel(
this AuthenticationBuilder builder,
string authenticationScheme,
string displayName,
Action<LocalFakeZitadelOptions>? configureOptions)
{
var options = new LocalFakeZitadelOptions();
configureOptions?.Invoke(options);
return builder.AddFakeZitadel(options);
return builder.AddFakeZitadel(authenticationScheme, displayName, options);
}

/// <summary>
Expand All @@ -299,8 +337,47 @@ public static AuthenticationBuilder AddFakeZitadel(
public static AuthenticationBuilder AddFakeZitadel(
this AuthenticationBuilder builder,
LocalFakeZitadelOptions options)
=> builder.AddScheme<LocalFakeZitadelSchemeOptions, LocalFakeZitadelHandler>(
=> builder.AddFakeZitadel(
ZitadelDefaults.FakeAuthenticationScheme,
options);

/// <summary>
/// Add a "fake" zitadel authentication. This should only be used for local
/// development to fake an authentication/authorization. All calls are authenticated
/// by default. If (e.g. for testing reasons) a specific call should NOT be authenticated,
/// attach the header "x-zitadel-fake-auth" with the value "false" to the request.
/// This specific request will then fail to authenticate.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to configure.</param>
/// <param name="authenticationScheme">The name for the authentication scheme to be used.</param>
/// <param name="options">The <see cref="LocalFakeZitadelOptions"/> to use.</param>
/// <returns>The configured <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddFakeZitadel(
this AuthenticationBuilder builder,
string authenticationScheme,
LocalFakeZitadelOptions options)
=> builder.AddFakeZitadel(authenticationScheme, ZitadelDefaults.FakeDisplayName, options);

/// <summary>
/// Add a "fake" zitadel authentication. This should only be used for local
/// development to fake an authentication/authorization. All calls are authenticated
/// by default. If (e.g. for testing reasons) a specific call should NOT be authenticated,
/// attach the header "x-zitadel-fake-auth" with the value "false" to the request.
/// This specific request will then fail to authenticate.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to configure.</param>
/// <param name="authenticationScheme">The name for the authentication scheme to be used.</param>
/// <param name="displayName">The display name for the authentication scheme.</param>
/// <param name="options">The <see cref="LocalFakeZitadelOptions"/> to use.</param>
/// <returns>The configured <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddFakeZitadel(
this AuthenticationBuilder builder,
string authenticationScheme,
string displayName,
LocalFakeZitadelOptions options)
=> builder.AddScheme<LocalFakeZitadelSchemeOptions, LocalFakeZitadelHandler>(
authenticationScheme,
displayName,
o => o.FakeZitadelOptions = options);
}
}
5 changes: 5 additions & 0 deletions src/Zitadel/Authentication/ZitadelDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public static class ZitadelDefaults
/// </summary>
public const string DisplayName = "Zitadel";

/// <summary>
/// Default display name of the fake handler.
/// </summary>
public const string FakeDisplayName = "ZitadelLocalFake";

/// <summary>
/// Default authentication scheme name for AddZitadel() and
/// AddZitadelWithSession().
Expand Down