This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
UserInfoEndpoint.cs
100 lines (86 loc) · 3.94 KB
/
UserInfoEndpoint.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System.Threading.Tasks;
using IdentityServer4.Validation;
using IdentityServer4.ResponseHandling;
using Microsoft.Extensions.Logging;
using IdentityServer4.Hosting;
using IdentityServer4.Endpoints.Results;
using IdentityModel;
using Microsoft.AspNetCore.Http;
using System.Net;
namespace IdentityServer4.Endpoints
{
/// <summary>
/// The userinfo endpoint
/// </summary>
/// <seealso cref="IdentityServer4.Hosting.IEndpointHandler" />
internal class UserInfoEndpoint : IEndpointHandler
{
private readonly BearerTokenUsageValidator _tokenUsageValidator;
private readonly IUserInfoRequestValidator _requestValidator;
private readonly IUserInfoResponseGenerator _responseGenerator;
private readonly ILogger _logger;
/// <summary>
/// Initializes a new instance of the <see cref="UserInfoEndpoint" /> class.
/// </summary>
/// <param name="tokenUsageValidator">The token usage validator.</param>
/// <param name="requestValidator">The request validator.</param>
/// <param name="responseGenerator">The response generator.</param>
/// <param name="logger">The logger.</param>
public UserInfoEndpoint(
BearerTokenUsageValidator tokenUsageValidator,
IUserInfoRequestValidator requestValidator,
IUserInfoResponseGenerator responseGenerator,
ILogger<UserInfoEndpoint> logger)
{
_tokenUsageValidator = tokenUsageValidator;
_requestValidator = requestValidator;
_responseGenerator = responseGenerator;
_logger = logger;
}
/// <summary>
/// Processes the request.
/// </summary>
/// <param name="context">The HTTP context.</param>
/// <returns></returns>
public async Task<IEndpointResult> ProcessAsync(HttpContext context)
{
if (!HttpMethods.IsGet(context.Request.Method) && !HttpMethods.IsPost(context.Request.Method))
{
_logger.LogWarning("Invalid HTTP method for userinfo endpoint.");
return new StatusCodeResult(HttpStatusCode.MethodNotAllowed);
}
return await ProcessUserInfoRequestAsync(context);
}
private async Task<IEndpointResult> ProcessUserInfoRequestAsync(HttpContext context)
{
_logger.LogDebug("Start userinfo request");
// userinfo requires an access token on the request
var tokenUsageResult = await _tokenUsageValidator.ValidateAsync(context);
if (tokenUsageResult.TokenFound == false)
{
var error = "No access token found.";
_logger.LogError(error);
return Error(OidcConstants.ProtectedResourceErrors.InvalidToken);
}
// validate the request
_logger.LogTrace("Calling into userinfo request validator: {type}", _requestValidator.GetType().FullName);
var validationResult = await _requestValidator.ValidateRequestAsync(tokenUsageResult.Token);
if (validationResult.IsError)
{
//_logger.LogError("Error validating validationResult.Error);
return Error(validationResult.Error);
}
// generate response
_logger.LogTrace("Calling into userinfo response generator: {type}", _responseGenerator.GetType().FullName);
var response = await _responseGenerator.ProcessAsync(validationResult);
_logger.LogDebug("End userinfo request");
return new UserInfoResult(response);
}
private IEndpointResult Error(string error, string description = null)
{
return new ProtectedResourceErrorResult(error, description);
}
}
}