PageRenderTime 163ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/source/Core/Connect/Endpoints/DiscoveryEndpointController.cs

https://github.com/AaronLS/Thinktecture.IdentityServer.v3
C# | 106 lines | 88 code | 14 blank | 4 comment | 4 complexity | 33678f781b68c9c815c45e6e38ff4a5b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) Dominick Baier, Brock Allen. All rights reserved.
  3. * see license
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Web.Http;
  10. using Thinktecture.IdentityModel;
  11. using Thinktecture.IdentityServer.Core.Configuration;
  12. using Thinktecture.IdentityServer.Core.Extensions;
  13. using Thinktecture.IdentityServer.Core.Logging;
  14. using Thinktecture.IdentityServer.Core.Services;
  15. namespace Thinktecture.IdentityServer.Core.Connect
  16. {
  17. public class DiscoveryEndpointController : ApiController
  18. {
  19. private readonly static ILog Logger = LogProvider.GetCurrentClassLogger();
  20. private readonly IdentityServerOptions _options;
  21. private readonly IScopeStore _scopes;
  22. public DiscoveryEndpointController(IdentityServerOptions options, IScopeStore scopes)
  23. {
  24. _options = options;
  25. _scopes = scopes;
  26. }
  27. [Route(Constants.RoutePaths.Oidc.DiscoveryConfiguration)]
  28. public async Task<IHttpActionResult> GetConfiguration()
  29. {
  30. Logger.Info("Start discovery request");
  31. if (!_options.DiscoveryEndpoint.IsEnabled)
  32. {
  33. Logger.Warn("Endpoint is disabled. Aborting");
  34. return NotFound();
  35. }
  36. var baseUrl = Request.GetIdentityServerBaseUrl();
  37. var scopes = await _scopes.GetScopesAsync();
  38. return Json(new
  39. {
  40. issuer = _options.IssuerUri,
  41. jwks_uri = baseUrl + Constants.RoutePaths.Oidc.DiscoveryWebKeys,
  42. authorization_endpoint = baseUrl + Constants.RoutePaths.Oidc.Authorize,
  43. token_endpoint = baseUrl + Constants.RoutePaths.Oidc.Token,
  44. userinfo_endpoint = baseUrl + Constants.RoutePaths.Oidc.UserInfo,
  45. end_session_endpoint = baseUrl + Constants.RoutePaths.Oidc.EndSession,
  46. scopes_supported = scopes.Select(s => s.Name),
  47. response_types_supported = Constants.SupportedResponseTypes,
  48. response_modes_supported = Constants.SupportedResponseModes,
  49. grant_types_supported = Constants.SupportedGrantTypes,
  50. subject_types_support = new[] { "pairwise", "public" },
  51. id_token_signing_alg_values_supported = "RS256"
  52. });
  53. }
  54. [Route(Constants.RoutePaths.Oidc.DiscoveryWebKeys)]
  55. public IHttpActionResult GetKeyData()
  56. {
  57. Logger.Info("Start key discovery request");
  58. if (!_options.DiscoveryEndpoint.IsEnabled)
  59. {
  60. Logger.Warn("Endpoint is disabled. Aborting");
  61. return NotFound();
  62. }
  63. var webKeys = new List<JsonWebKeyDto>();
  64. foreach (var pubKey in _options.PublicKeysForMetadata)
  65. {
  66. if (pubKey != null)
  67. {
  68. var cert64 = Convert.ToBase64String(pubKey.RawData);
  69. var thumbprint = Base64Url.Encode(pubKey.GetCertHash());
  70. var webKey = new JsonWebKeyDto
  71. {
  72. kty = "RSA",
  73. use = "sig",
  74. kid = thumbprint,
  75. x5t = thumbprint,
  76. x5c = new[] { cert64 }
  77. };
  78. webKeys.Add(webKey);
  79. }
  80. }
  81. return Json(new { keys = webKeys });
  82. }
  83. private class JsonWebKeyDto
  84. {
  85. public string kty { get; set; }
  86. public string use { get; set; }
  87. public string kid { get; set; }
  88. public string x5t { get; set; }
  89. public string[] x5c { get; set; }
  90. }
  91. }
  92. }