Skip to content

Commit

Permalink
feat(auth): add policy helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 6, 2023
1 parent e45fec8 commit 6e97444
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Sitko.Core.Auth/AuthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ public abstract class AuthOptions : BaseModuleOptions
public int CookieExpireInMinutes { get; set; } = 30 * 24 * 60;
public string UserIdClaimName { get; set; } = ClaimTypes.NameIdentifier;
[JsonIgnore] public Action<CookieBuilder>? ConfigureCookie { get; set; }

public AuthOptions AddPolicy(string name, Func<AuthorizationPolicyBuilder, AuthorizationPolicyBuilder> policyBuilder,
bool forcePolicy = false)
{
var builder = new AuthorizationPolicyBuilder();
var policy = policyBuilder(builder).Build();
return AddPolicy(name, policy, forcePolicy);
}

public AuthOptions AddPolicy(string name, AuthorizationPolicy policy, bool forcePolicy = false)
{
Policies.Add(name, policy);
if (forcePolicy)
{
ForcePolicy = name;
}

return this;
}
}

public abstract class AuthOptionsValidator<TOptions> : AbstractValidator<TOptions> where TOptions : AuthOptions
Expand All @@ -37,4 +56,3 @@ protected AuthOptionsValidator()
.WithMessage("Can't use -1 database for data protection");
}
}

0 comments on commit 6e97444

Please sign in to comment.