Skip to content

Commit d5cb518

Browse files
authored
Merge pull request #13 from jokk-itu/feature/get-subject
Feature/get subject
2 parents e64275f + a647b35 commit d5cb518

File tree

12 files changed

+64
-26
lines changed

12 files changed

+64
-26
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ The following grant types are supported:
1010

1111
## Pipeline runs
1212

13-
TODO
13+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jokk-itu_authserver-framework&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jokk-itu_authserver-framework)
14+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jokk-itu_authserver-framework&metric=coverage)](https://sonarcloud.io/summary/new_code?id=jokk-itu_authserver-framework)
15+
[![CI](https://github.com/jokk-itu/authserver-framework/actions/workflows/build.yml/badge.svg)](https://github.com/jokk-itu/authserver-framework/actions/workflows/build.yml)
1416

1517
## Documentation
1618

17-
Take a look at [authserver.dk](https://www.authserver.dk).
19+
Take a look at [authserver.dk](https://www.authserver.dk).
20+
21+
## Demo
22+
23+
Take a look at [authserver demo](https://github.com/jokk-itu/authserver-demo).

src/AuthServer.TestIdentityProvider/Pages/Consent.cshtml.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ namespace AuthServer.TestIdentityProvider.Pages;
1111
[ValidateAntiForgeryToken]
1212
public class ConsentModel : PageModel
1313
{
14-
private readonly IAuthorizeUserAccessor _authorizeUserAccessor;
1514
private readonly IAuthorizeService _authorizeService;
1615

17-
public ConsentModel(
18-
IAuthorizeUserAccessor authorizeUserAccessor,
19-
IAuthorizeService authorizeService)
16+
public ConsentModel(IAuthorizeService authorizeService)
2017
{
21-
_authorizeUserAccessor = authorizeUserAccessor;
2218
_authorizeService = authorizeService;
2319
}
2420

@@ -29,10 +25,10 @@ public ConsentModel(
2925

3026
public class InputModel
3127
{
32-
public required string ClientName { get; set; }
28+
public string? ClientName { get; set; }
3329
public string? ClientUri { get; set; }
3430
public string? ClientLogoUri { get; set; }
35-
public required string Username { get; set; }
31+
public string? Username { get; set; }
3632
public List<string> RequestedScope { get; set; } = [];
3733
public List<ClaimDto> RequestedClaims { get; set; } = [];
3834
public List<string> ConsentedScope { get; set; } = [];
@@ -49,13 +45,15 @@ public async Task OnGet(string returnUrl, CancellationToken cancellationToken)
4945
{
5046
ReturnUrl = returnUrl ?? Url.Content("~/");
5147

52-
var user = _authorizeUserAccessor.GetUser();
5348
var query = HttpUtility.ParseQueryString(new Uri(ReturnUrl).Query);
5449
var requestUri = query.Get(Parameter.RequestUri)!;
5550
var clientId = query.Get(Parameter.ClientId)!;
56-
var consentGrantDto = await _authorizeService.GetConsentGrantDto(user.SubjectIdentifier, clientId, cancellationToken);
5751

5852
var request = (await _authorizeService.GetRequest(requestUri, clientId, cancellationToken))!;
53+
54+
var subject = await _authorizeService.GetSubject(request);
55+
var consentGrantDto = await _authorizeService.GetConsentGrantDto(subject, clientId, cancellationToken);
56+
5957
var requestedScope = request.Scope.ToList();
6058

6159
// Display requested claims, also if they are already consented. This makes sure the end-user can change their full consent.
@@ -82,11 +80,12 @@ public async Task<IActionResult> OnPostAccept(string returnUrl, CancellationToke
8280
{
8381
ReturnUrl = returnUrl ?? Url.Content("~/");
8482

85-
var user = _authorizeUserAccessor.GetUser();
8683
var query = HttpUtility.ParseQueryString(new Uri(ReturnUrl).Query);
8784
var clientId = query.Get(Parameter.ClientId)!;
88-
89-
await _authorizeService.CreateOrUpdateConsentGrant(user.SubjectIdentifier, clientId, Input.ConsentedScope, Input.ConsentedClaims, cancellationToken);
85+
var requestUri = query.Get(Parameter.RequestUri)!;
86+
var request = (await _authorizeService.GetRequest(requestUri, clientId, cancellationToken))!;
87+
var subject = await _authorizeService.GetSubject(request);
88+
await _authorizeService.CreateOrUpdateConsentGrant(subject, clientId, Input.ConsentedScope, Input.ConsentedClaims, cancellationToken);
9089

9190
return Redirect(ReturnUrl);
9291
}

src/AuthServer/AuthServer.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<ItemGroup>
3434
<FrameworkReference Include="Microsoft.AspNetCore.App" />
35+
<None Include="README.md" Pack="true" PackagePath="\" />
3536
</ItemGroup>
3637

3738
<ItemGroup>
@@ -42,8 +43,4 @@
4243
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
4344
</ItemGroup>
4445

45-
<ItemGroup>
46-
<None Include="README.md" Pack="true" PackagePath="\" />
47-
</ItemGroup>
48-
4946
</Project>

src/AuthServer/Authentication/Abstractions/IUserClaimService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ public interface IUserClaimService
1717
/// <param name="subjectIdentifier"></param>
1818
/// <param name="cancellationToken"></param>
1919
/// <returns></returns>
20-
Task<string> GetUserName(string subjectIdentifier, CancellationToken cancellationToken);
20+
Task<string> GetUsername(string subjectIdentifier, CancellationToken cancellationToken);
2121
}

src/AuthServer/Authentication/OAuthToken/OAuthTokenAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
9999
}
100100
else
101101
{
102-
throw new InvalidOperationException("Challenge must happened from failure or none");
102+
throw new InvalidOperationException("Challenge must happen from failure or none");
103103
}
104104

105105
Response.StatusCode = 401;

src/AuthServer/Authorize/Abstractions/IAuthorizeService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@ public interface IAuthorizeService
6161
/// <param name="cancellationToken"></param>
6262
/// <returns></returns>
6363
Task<IActionResult> GetErrorResult(string requestUri, string clientId, OAuthError oauthError, HttpContext httpContext, CancellationToken cancellationToken);
64+
65+
/// <summary>
66+
/// Get the subject from AuthorizeUser, IdTokenHint or AuthenticatedUser.
67+
/// </summary>
68+
/// <returns></returns>
69+
Task<string> GetSubject(AuthorizeRequestDto authorizeRequestDto);
6470
}

src/AuthServer/Authorize/AuthorizeService.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using AuthServer.Endpoints.Responses;
88
using AuthServer.Repositories.Abstractions;
99
using AuthServer.RequestAccessors.Authorize;
10+
using AuthServer.TokenDecoders;
11+
using AuthServer.TokenDecoders.Abstractions;
1012
using Microsoft.AspNetCore.Http;
1113
using Microsoft.AspNetCore.Mvc;
1214

@@ -20,6 +22,9 @@ internal class AuthorizeService : IAuthorizeService
2022
private readonly IAuthenticationContextReferenceResolver _authenticationContextResolver;
2123
private readonly ISecureRequestService _secureRequestService;
2224
private readonly IAuthorizeResponseBuilder _authorizeResponseBuilder;
25+
private readonly IAuthenticatedUserAccessor _authenticatedUserAccessor;
26+
private readonly IAuthorizeUserAccessor _authorizeUserAccessor;
27+
private readonly ITokenDecoder<ServerIssuedTokenDecodeArguments> _tokenDecoder;
2328

2429
public AuthorizeService(
2530
IConsentGrantRepository consentGrantRepository,
@@ -28,7 +33,10 @@ public AuthorizeService(
2833
IUserClaimService userClaimService,
2934
IAuthenticationContextReferenceResolver authenticationContextResolver,
3035
ISecureRequestService secureRequestService,
31-
IAuthorizeResponseBuilder authorizeResponseBuilder)
36+
IAuthorizeResponseBuilder authorizeResponseBuilder,
37+
IAuthenticatedUserAccessor authenticatedUserAccessor,
38+
IAuthorizeUserAccessor authorizeUserAccessor,
39+
ITokenDecoder<ServerIssuedTokenDecodeArguments> tokenDecoder)
3240
{
3341
_consentGrantRepository = consentGrantRepository;
3442
_authorizationGrantRepository = authorizationGrantRepository;
@@ -37,6 +45,9 @@ public AuthorizeService(
3745
_authenticationContextResolver = authenticationContextResolver;
3846
_secureRequestService = secureRequestService;
3947
_authorizeResponseBuilder = authorizeResponseBuilder;
48+
_authenticatedUserAccessor = authenticatedUserAccessor;
49+
_authorizeUserAccessor = authorizeUserAccessor;
50+
_tokenDecoder = tokenDecoder;
4051
}
4152

4253
/// <inheritdoc/>
@@ -59,7 +70,7 @@ public async Task<ConsentGrantDto> GetConsentGrantDto(string subjectIdentifier,
5970
{
6071
var consentGrant = await _consentGrantRepository.GetConsentGrant(subjectIdentifier, clientId, cancellationToken);
6172
var cachedClient = await _cachedClientStore.Get(clientId, cancellationToken);
62-
var username = await _userClaimService.GetUserName(subjectIdentifier, cancellationToken);
73+
var username = await _userClaimService.GetUsername(subjectIdentifier, cancellationToken);
6374

6475
return new ConsentGrantDto
6576
{
@@ -95,4 +106,23 @@ public async Task<IActionResult> GetErrorResult(string requestUri, string client
95106
};
96107
return await _authorizeResponseBuilder.BuildResponse(request, errorParameters, cancellationToken);
97108
}
109+
110+
/// <inheritdoc/>
111+
public async Task<string> GetSubject(AuthorizeRequestDto authorizeRequestDto)
112+
{
113+
var authorizeUser = _authorizeUserAccessor.TryGetUser();
114+
if (authorizeUser is not null)
115+
{
116+
return authorizeUser.SubjectIdentifier;
117+
}
118+
119+
if (authorizeRequestDto.IdTokenHint is not null)
120+
{
121+
var idToken = await _tokenDecoder.Read(authorizeRequestDto.IdTokenHint);
122+
return idToken.Subject;
123+
}
124+
125+
var authenticatedUser = await _authenticatedUserAccessor.GetAuthenticatedUser();
126+
return authenticatedUser?.SubjectIdentifier ?? throw new InvalidOperationException("subject cannot be deduced");
127+
}
98128
}

src/AuthServer/Introspection/IntrospectionEndpointHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using AuthServer.Core.Abstractions;
2-
using AuthServer.Core.Request;
32
using AuthServer.Endpoints.Responses;
43
using AuthServer.Extensions;
54
using AuthServer.RequestAccessors.Introspection;

src/AuthServer/Introspection/IntrospectionRequestProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task<IntrospectionResponse> Process(IntrospectionValidatedRequest r
6262
string? username = null;
6363
if (query.SubjectIdentifier is not null)
6464
{
65-
username = await _userClaimService.GetUserName(query.SubjectIdentifier, cancellationToken);
65+
username = await _userClaimService.GetUsername(query.SubjectIdentifier, cancellationToken);
6666
}
6767

6868
var subject = query.SubjectFromGrantToken ?? query.SubjectFromClientToken;

tests/AuthServer.Tests.Core/UserClaimService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Task<IEnumerable<Claim>> GetClaims(string subjectIdentifier, Cancellation
3333
]);
3434
}
3535

36-
public Task<string> GetUserName(string subjectIdentifier, CancellationToken cancellationToken)
36+
public Task<string> GetUsername(string subjectIdentifier, CancellationToken cancellationToken)
3737
{
3838
return Task.FromResult(UserConstants.Username);
3939
}

0 commit comments

Comments
 (0)