Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 0d8bdce

Browse files
committed
changes some properties of sso shortcircuiting
1 parent c63a827 commit 0d8bdce

File tree

7 files changed

+66
-40
lines changed

7 files changed

+66
-40
lines changed

src/Solid.Identity.Protocols.Saml2p/Authentication/Saml2pAuthenticationHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Solid.Identity.Protocols.Saml2p.Providers;
1818
using System.Security.Claims;
1919
using Solid.Identity.Protocols.Saml2p.Options;
20+
using Solid.Identity.Protocols.Saml2p.Exceptions;
2021

2122
namespace Solid.Identity.Protocols.Saml2p.Authentication
2223
{
@@ -43,7 +44,8 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
4344
try
4445
{
4546
var result = await Context.FinishSsoAsync();
46-
if(!result.IsSuccessful)
47+
if (!result.IsSuccessful)
48+
throw new SamlResponseException(result.PartnerId, result.Status, result.SubStatus);
4749

4850
var properties = new AuthenticationProperties
4951
{

src/Solid.Identity.Protocols.Saml2p/Cache/Saml2pCache.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ public Task CacheRequestAsync(string key, AuthnRequest request)
2525
return _inner.SetAsync(key, json);
2626
}
2727

28-
public Task CacheStatusAsync(string key, SamlResponseStatus status)
29-
=> CacheStatusAsync(key, status, null);
30-
31-
public Task CacheStatusAsync(string key, SamlResponseStatus status, SamlResponseStatus? subStatus)
32-
=> CacheStatusAsync(key, (Status: status, SubStatus: subStatus));
33-
34-
private Task CacheStatusAsync(string key, (SamlResponseStatus Status, SamlResponseStatus? SubStatus) status)
28+
public Task CacheStatusAsync(string key, Status status)
3529
{
3630
var json = JsonSerializer.SerializeToUtf8Bytes(status);
3731
return _inner.SetAsync($"{key}_status", json);
@@ -45,12 +39,12 @@ public async Task<AuthnRequest> FetchRequestAsync(string key)
4539
return JsonSerializer.Deserialize<AuthnRequest>(json);
4640
}
4741

48-
public async Task<(SamlResponseStatus Status, SamlResponseStatus? SubStatus)?> FetchStatusAsync(string key)
42+
public async Task<Status> FetchStatusAsync(string key)
4943
{
5044
var json = await _inner.GetAsync($"{key}_status");
5145
if (json == null) return null;
5246

53-
return JsonSerializer.Deserialize<(SamlResponseStatus Status, SamlResponseStatus? SubStatus)>(json);
47+
return JsonSerializer.Deserialize<Status>(json);
5448
}
5549

5650
public async Task RemoveAsync(string key)

src/Solid.Identity.Protocols.Saml2p/Extensions/EnumExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Solid.Identity.Protocols.Saml2p.Models.Protocol;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

@@ -17,6 +18,23 @@ public static string ToProtocolBindingString(this BindingType bindingType)
1718
throw new ArgumentException($"Unsupported binding type: {bindingType}");
1819
}
1920

21+
public static Status ToStatus(this SamlResponseStatus status, SamlResponseStatus? subStatus = null)
22+
{
23+
var s = new Status
24+
{
25+
StatusCode = new StatusCode
26+
{
27+
Value = status.ToStatusUri()
28+
}
29+
};
30+
if (subStatus != null)
31+
s.StatusCode.SubCode = new StatusCode
32+
{
33+
Value = subStatus.Value.ToStatusUri()
34+
};
35+
return s;
36+
}
37+
2038
public static Uri ToStatusUri(this SamlResponseStatus status)
2139
{
2240
switch (status)

src/Solid.Identity.Protocols.Saml2p/Factories/SamlResponseFactory.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public SamlResponseFactory(IOptions<Saml2pOptions> options)
2020
}
2121

2222
public SamlResponse Create(ISaml2pServiceProvider partner, string authnRequestId = null, string relayState = null, SamlResponseStatus status = SamlResponseStatus.Success, SamlResponseStatus? subStatus = null, Saml2SecurityToken token = null)
23+
=> Create(partner, status.ToStatus(subStatus), authnRequestId: authnRequestId, relayState: relayState, token: token);
24+
25+
public SamlResponse Create(ISaml2pServiceProvider partner, Status status, string authnRequestId = null, string relayState = null, Saml2SecurityToken token = null)
2326
{
2427
var destination = new Uri(partner.BaseUrl, partner.AssertionConsumerServiceEndpoint);
2528
if (token != null)
@@ -34,42 +37,42 @@ public SamlResponse Create(ISaml2pServiceProvider partner, string authnRequestId
3437
var response = new SamlResponse
3538
{
3639
Id = $"_{Guid.NewGuid()}", // TODO: create id factory
37-
SecurityToken = token,
40+
SecurityToken = token,
3841
Destination = destination,
3942
IssueInstant = token?.Assertion.IssueInstant,
4043
Issuer = partner.ExpectedIssuer ?? _options.DefaultIssuer,
41-
Status = Convert(status, subStatus),
44+
Status = status,
4245
InResponseTo = authnRequestId,
4346
RelayState = relayState
4447
};
4548

4649
return response;
4750
}
4851

49-
private Status Convert(SamlResponseStatus status, SamlResponseStatus? subStatus)
50-
{
51-
var converted = new Status
52-
{
53-
StatusCode = new StatusCode
54-
{
55-
Value = Convert(status)
56-
}
57-
};
52+
//private Status Convert(SamlResponseStatus status, SamlResponseStatus? subStatus)
53+
//{
54+
// var converted = new Status
55+
// {
56+
// StatusCode = new StatusCode
57+
// {
58+
// Value = Convert(status)
59+
// }
60+
// };
5861

59-
var sub = Convert(subStatus);
60-
if (sub != null)
61-
converted.StatusCode.SubCode = new StatusCode
62-
{
63-
Value = sub
64-
};
62+
// var sub = Convert(subStatus);
63+
// if (sub != null)
64+
// converted.StatusCode.SubCode = new StatusCode
65+
// {
66+
// Value = sub
67+
// };
6568

66-
return converted;
67-
}
69+
// return converted;
70+
//}
6871

69-
private Uri Convert(SamlResponseStatus? status)
70-
{
71-
if (status.HasValue) return status.Value.ToStatusUri();
72-
return null;
73-
}
72+
//private Uri Convert(SamlResponseStatus? status)
73+
//{
74+
// if (status.HasValue) return status.Value.ToStatusUri();
75+
// return null;
76+
//}
7477
}
7578
}

src/Solid.Identity.Protocols.Saml2p/Middleware/Idp/AcceptSsoEndpointMiddleware.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public override async Task InvokeAsync(HttpContext context)
6767

6868
if (!IsValid(ssoContext, out var status, out var subStatus) && status.HasValue)
6969
{
70-
await Cache.CacheStatusAsync(request.Id, status.Value, subStatus);
70+
Logger.LogWarning($"SAMLRequest failed validation. Resulting error: '{(subStatus ?? status)}'");
71+
await Cache.CacheStatusAsync(request.Id, status.Value.ToStatus(subStatus));
7172
context.Response.Redirect(ssoContext.ReturnUrl);
7273
}
7374
else if (ssoContext.AuthenticationScheme != null)

src/Solid.Identity.Protocols.Saml2p/Middleware/Idp/CompleteSsoEndpointMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public override async Task InvokeAsync(HttpContext context)
6262

6363
var response = null as SamlResponse;
6464

65-
var status = await Cache.FetchStatusAsync(id);
6665
var user = context.User;
6766
var request = await Cache.FetchRequestAsync(id);
6867
if (request == null)
@@ -79,10 +78,11 @@ public override async Task InvokeAsync(HttpContext context)
7978
//if (!partner.Enabled)
8079
// throw new SecurityException($"Partner '{partnerId}' is disabled.");
8180

82-
if (status.HasValue)
81+
var status = await Cache.FetchStatusAsync(id);
82+
if (status != null)
8383
{
84-
var tuple = status.Value;
85-
response = _responseFactory.Create(partner, authnRequestId: request.Id, relayState: request.RelayState, status: tuple.Status, subStatus: tuple.SubStatus);
84+
Trace("Found cached Status.", request.RelayState, status);
85+
response = _responseFactory.Create(partner, status, authnRequestId: request.Id, relayState: request.RelayState);
8686
}
8787
else if (user.Identity.IsAuthenticated)
8888
{

src/Solid.Identity.Protocols.Saml2p/Middleware/Saml2pEndpointMiddleware.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Options;
55
using Microsoft.Extensions.Primitives;
66
using Solid.Identity.Protocols.Saml2p.Cache;
7+
using Solid.Identity.Protocols.Saml2p.Logging;
78
using Solid.Identity.Protocols.Saml2p.Models;
89
using Solid.Identity.Protocols.Saml2p.Models.Protocol;
910
using Solid.Identity.Protocols.Saml2p.Options;
@@ -195,6 +196,13 @@ private string GetRelayState(HttpContext context, BindingType binding)
195196
return value.ToString();
196197
}
197198

199+
protected void Trace(string prefix, string relayState, Status status)
200+
{
201+
if (!Logger.IsEnabled(LogLevel.Trace)) return;
202+
var format = $"{prefix} | RelayState: '{{relayState}}'" + Environment.NewLine + "{request}";
203+
Logger.LogTrace(format, relayState, new WrappedLogMessageState(status));
204+
}
205+
198206
protected void Trace(string prefix, AuthnRequest request)
199207
{
200208
if (!Logger.IsEnabled(LogLevel.Trace)) return;

0 commit comments

Comments
 (0)