Skip to content

Commit 81a0b4c

Browse files
authored
Merge pull request #79 from Zyano/master
Support for saml2:conditions on AuthnRequest
2 parents 13f08d8 + 98241bd commit 81a0b4c

File tree

9 files changed

+220
-1
lines changed

9 files changed

+220
-1
lines changed

src/ITfoxtec.Identity.Saml2/Request/Saml2AuthnRequest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ public class Saml2AuthnRequest : Saml2Request
8484
/// </summary>
8585
public RequestedAuthnContext RequestedAuthnContext { get; set; }
8686

87+
/// <summary>
88+
/// [Optional]
89+
/// If present, specifies an Audience
90+
/// Part of the OIOSAML standard used for conditions on request.
91+
/// </summary>
92+
public Condition Conditions { get; set; }
93+
8794
public Saml2AuthnRequest(Saml2Configuration config) : base(config)
8895
{
8996
if (config == null) throw new ArgumentNullException(nameof(config));
@@ -124,6 +131,11 @@ protected override IEnumerable<XObject> GetXContent()
124131
yield return new XAttribute(Saml2Constants.Message.ProtocolBinding, ProtocolBinding);
125132
}
126133

134+
if (Conditions != null)
135+
{
136+
yield return Conditions.ToXElement();
137+
}
138+
127139
if (Subject != null)
128140
{
129141
yield return Subject.ToXElement();

src/ITfoxtec.Identity.Saml2/Request/Saml2Request.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Xml;
88
using System.Xml.Linq;
99
using System.Security.Cryptography.Xml;
10+
using System.Diagnostics;
1011
#if NETFULL
1112
using System.IdentityModel.Tokens;
1213
#else
@@ -113,7 +114,7 @@ public string IdAsString
113114

114115
internal Saml2IdentityConfiguration IdentityConfiguration { get; private set; }
115116

116-
public Saml2Request(Saml2Configuration config)
117+
protected Saml2Request(Saml2Configuration config)
117118
{
118119
if (config == null) throw new ArgumentNullException(nameof(config));
119120

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Xml.Linq;
5+
using ITfoxtec.Identity.Saml2.Schemas.Conditions;
6+
7+
namespace ITfoxtec.Identity.Saml2.Schemas
8+
{
9+
/// <summary>
10+
/// Implementation of Saml2:Condition
11+
/// </summary>
12+
public class Condition
13+
{
14+
/// <summary>
15+
/// The XML Element name of this class
16+
/// </summary>
17+
public const string elementName = Saml2Constants.Message.Conditions;
18+
19+
public List<ConditionAbstract> Items { get; set; }
20+
21+
public DateTimeOffset? NotOnOrAfter { get; set; }
22+
23+
public DateTimeOffset? NotBefore { get; set; }
24+
25+
public XElement ToXElement()
26+
{
27+
var envelope = new XElement(Saml2Constants.AssertionNamespaceX + elementName);
28+
29+
envelope.Add(GetXContent());
30+
31+
return envelope;
32+
}
33+
34+
protected virtual IEnumerable<XObject> GetXContent()
35+
{
36+
yield return new XAttribute(Saml2Constants.AssertionNamespaceNameX, Saml2Constants.AssertionNamespaceX);
37+
if (NotOnOrAfter.HasValue)
38+
{
39+
yield return new XAttribute(Saml2Constants.Message.NotOnOrAfter,
40+
NotOnOrAfter.Value.UtcDateTime.ToString(Schemas.Saml2Constants.DateTimeFormat,
41+
CultureInfo.InvariantCulture));
42+
}
43+
44+
if (NotBefore.HasValue)
45+
{
46+
yield return new XAttribute(Saml2Constants.Message.NotBefore,
47+
NotBefore.Value.UtcDateTime.ToString(Schemas.Saml2Constants.DateTimeFormat,
48+
CultureInfo.InvariantCulture));
49+
}
50+
if (Items != null)
51+
{
52+
foreach (var condition in Items)
53+
{
54+
yield return condition.ToXElement();
55+
}
56+
}
57+
}
58+
}
59+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
using System.Xml.Linq;
3+
4+
namespace ITfoxtec.Identity.Saml2.Schemas.Conditions
5+
{
6+
public class Audience
7+
{
8+
/// <summary>
9+
/// The XML Element name of this class
10+
/// </summary>
11+
const string elementName = Saml2Constants.Message.Audience;
12+
13+
public string Uri { get; set; }
14+
15+
public XElement ToXElement()
16+
{
17+
var envelope = new XElement(Saml2Constants.AssertionNamespaceX + elementName);
18+
19+
envelope.Add(GetXContent());
20+
21+
return envelope;
22+
}
23+
24+
protected IEnumerable<XObject> GetXContent()
25+
{
26+
if (!string.IsNullOrEmpty(Uri))
27+
{
28+
yield return new XText(Uri);
29+
}
30+
}
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using System.Xml.Linq;
3+
4+
namespace ITfoxtec.Identity.Saml2.Schemas.Conditions
5+
{
6+
public class AudienceRestriction : ConditionAbstract
7+
{
8+
/// <summary>
9+
/// The XML Element name of this class
10+
/// </summary>
11+
const string elementName = Saml2Constants.Message.AudienceRestriction;
12+
13+
public List<Audience> Audiences { get; set; }
14+
15+
public override XElement ToXElement()
16+
{
17+
var envelope = new XElement(Saml2Constants.AssertionNamespaceX + elementName);
18+
19+
envelope.Add(GetXContent());
20+
21+
return envelope;
22+
}
23+
24+
protected IEnumerable<XObject> GetXContent()
25+
{
26+
if (Audiences != null)
27+
{
28+
foreach (var audience in Audiences)
29+
{
30+
yield return audience.ToXElement();
31+
}
32+
}
33+
}
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Xml.Linq;
2+
3+
namespace ITfoxtec.Identity.Saml2.Schemas.Conditions
4+
{
5+
public abstract class ConditionAbstract
6+
{
7+
public abstract XElement ToXElement();
8+
}
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Xml.Linq;
4+
5+
namespace ITfoxtec.Identity.Saml2.Schemas.Conditions
6+
{
7+
public class OneTimeUse : ConditionAbstract
8+
{
9+
/// <summary>
10+
/// The XML Element name of this class
11+
/// </summary>
12+
const string elementName = Saml2Constants.Message.OneTimeUse;
13+
14+
public override XElement ToXElement()
15+
{
16+
var envelope = new XElement(Saml2Constants.AssertionNamespaceX + elementName);
17+
18+
return envelope;
19+
}
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
2+
using System.Xml.Linq;
3+
4+
namespace ITfoxtec.Identity.Saml2.Schemas.Conditions
5+
{
6+
public class ProxyRestriction : ConditionAbstract
7+
{
8+
/// <summary>
9+
/// The XML Element name of this class
10+
/// </summary>
11+
const string elementName = Saml2Constants.Message.ProxyRestriction;
12+
13+
public List<Audience> Audiences { get; set; }
14+
15+
public uint? Count { get; set; }
16+
17+
public override XElement ToXElement()
18+
{
19+
var envelope = new XElement(Saml2Constants.AssertionNamespaceX + elementName);
20+
21+
envelope.Add(GetXContent());
22+
23+
return envelope;
24+
}
25+
26+
protected IEnumerable<XObject> GetXContent()
27+
{
28+
if (Audiences != null)
29+
{
30+
foreach (var audience in Audiences)
31+
{
32+
yield return audience.ToXElement();
33+
}
34+
}
35+
36+
if (Count.HasValue)
37+
{
38+
yield return new XAttribute(Saml2Constants.Message.Count, Count.Value);
39+
}
40+
}
41+
}
42+
}

src/ITfoxtec.Identity.Saml2/Schemas/Saml2Constants.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public static class Message
119119

120120
internal const string NotOnOrAfter = "NotOnOrAfter";
121121

122+
internal const string NotBefore = "NotBefore";
123+
122124
internal const string Reason = "Reason";
123125

124126
internal const string NameIdPolicy = "NameIDPolicy";
@@ -142,6 +144,12 @@ public static class Message
142144
internal const string SubjectConfirmation = "SubjectConfirmation";
143145

144146
internal const string SubjectConfirmationData = "SubjectConfirmationData";
147+
148+
internal const string OneTimeUse = "OneTimeUse";
149+
150+
internal const string ProxyRestriction = "ProxyRestriction";
151+
152+
internal const string Count = "Count";
145153
}
146154
}
147155
}

0 commit comments

Comments
 (0)