forked from Sustainsys/Saml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaml2AuthExtensions.cs
82 lines (74 loc) · 3.11 KB
/
Saml2AuthExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Sustainsys.Saml2.AspNetCore2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extensions methods for adding Saml2 authentication
/// </summary>
public static class Saml2AuthExtensions
{
/// <summary>
/// Register Saml2 Authentication with default scheme name.
/// </summary>
/// <param name="builder">Authentication Builder</param>
/// <param name="configureOptions">Action that configures the Saml2 Options</param>
/// <returns></returns>
public static AuthenticationBuilder AddSaml2(
this AuthenticationBuilder builder,
Action<Saml2Options> configureOptions)
=> builder.AddSaml2(Saml2Defaults.Scheme, configureOptions);
/// <summary>
/// Register Saml2 Authentication with a custom scheme name.
/// </summary>
/// <param name="builder">Authentication Builder</param>
/// <param name="scheme">Name of the authentication scheme</param>
/// <param name="configureOptions">Action that configures Saml2 Options</param>
/// <returns>Authentication Builder</returns>
public static AuthenticationBuilder AddSaml2(
this AuthenticationBuilder builder,
string scheme,
Action<Saml2Options> configureOptions)
=> builder.AddSaml2(scheme, Saml2Defaults.DisplayName, configureOptions);
/// <summary>
/// Register Saml2 Authentication with a custom scheme name.
/// </summary>
/// <param name="builder">Authentication Builder</param>
/// <param name="scheme">Name of the authentication scheme</param>
/// <param name="configureOptions">Action that configures Saml2 Options</param>
/// <param name="displayName">Display name of scheme</param>
/// <returns>Authentication Builder</returns>
public static AuthenticationBuilder AddSaml2(
this AuthenticationBuilder builder,
string scheme,
string displayName,
Action<Saml2Options> configureOptions)
{
if(builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<Saml2Options>, PostConfigureSaml2Options>());
builder.Services.Configure<AuthenticationOptions>(o =>
{
o.AddScheme(scheme, s =>
{
s.HandlerType = typeof(Saml2Handler);
s.DisplayName = displayName;
});
});
if (configureOptions != null)
{
builder.Services.Configure(scheme, configureOptions);
}
builder.Services.AddTransient<Saml2Handler>();
return builder;
}
}
}