/source/Tests/UnitTests/Plumbing/TestSettings.cs

https://github.com/emed795/Thinktecture.IdentityServer.v3 · C# · 234 lines · 207 code · 23 blank · 4 comment · 2 complexity · 21754345192517cbb1dc8fb356f7b183 MD5 · raw file

  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.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Security.Claims;
  11. using System.Security.Cryptography.X509Certificates;
  12. using System.Threading.Tasks;
  13. using Thinktecture.IdentityServer.Core;
  14. using Thinktecture.IdentityServer.Core.Connect.Models;
  15. using Thinktecture.IdentityServer.Core.Models;
  16. using Thinktecture.IdentityServer.Core.Services;
  17. namespace UnitTests.Plumbing
  18. {
  19. class TestSettings : ICoreSettings
  20. {
  21. static X509Certificate2 _certificate;
  22. static TestSettings()
  23. {
  24. var assembly = typeof(TestSettings).Assembly;
  25. using (var stream = assembly.GetManifestResourceStream("Thinktecture.IdentityServer.Tests.Plumbing.idsrv3test.pfx"))
  26. {
  27. _certificate = new X509Certificate2(ReadStream(stream));
  28. }
  29. }
  30. List<Client> _clients = new List<Client>
  31. {
  32. new Client
  33. {
  34. ClientName = "Code Client",
  35. Enabled = true,
  36. ClientId = "codeclient",
  37. ClientSecret = "secret",
  38. Flow = Flows.Code,
  39. ApplicationType = ApplicationTypes.Web,
  40. RequireConsent = false,
  41. RedirectUris = new List<Uri>
  42. {
  43. new Uri("https://server/cb"),
  44. },
  45. AuthorizationCodeLifetime = 60
  46. },
  47. new Client
  48. {
  49. ClientName = "Implicit Client",
  50. Enabled = true,
  51. ClientId = "implicitclient",
  52. ClientSecret = "secret",
  53. Flow = Flows.Implicit,
  54. ApplicationType = ApplicationTypes.Native,
  55. RequireConsent = false,
  56. RedirectUris = new List<Uri>
  57. {
  58. new Uri("oob://implicit/cb")
  59. },
  60. },
  61. new Client
  62. {
  63. ClientName = "Code Client with Scope Restrictions",
  64. Enabled = true,
  65. ClientId = "codeclient_restricted",
  66. ClientSecret = "secret",
  67. Flow = Flows.Code,
  68. ApplicationType = ApplicationTypes.Web,
  69. RequireConsent = false,
  70. ScopeRestrictions = new List<string>
  71. {
  72. "openid"
  73. },
  74. RedirectUris = new List<Uri>
  75. {
  76. new Uri("https://server/cb"),
  77. },
  78. },
  79. new Client
  80. {
  81. ClientName = "Client Credentials Client",
  82. Enabled = true,
  83. ClientId = "client",
  84. ClientSecret = "secret",
  85. Flow = Flows.ClientCredentials,
  86. AccessTokenType = AccessTokenType.JWT
  87. },
  88. new Client
  89. {
  90. ClientName = "Client Credentials Client (restricted)",
  91. Enabled = true,
  92. ClientId = "client_restricted",
  93. ClientSecret = "secret",
  94. Flow = Flows.ClientCredentials,
  95. ScopeRestrictions = new List<string>
  96. {
  97. "resource"
  98. },
  99. },
  100. new Client
  101. {
  102. ClientName = "Resource Owner Client",
  103. Enabled = true,
  104. ClientId = "roclient",
  105. ClientSecret = "secret",
  106. Flow = Flows.ResourceOwner,
  107. },
  108. new Client
  109. {
  110. ClientName = "Resource Owner Client (restricted)",
  111. Enabled = true,
  112. ClientId = "roclient_restricted",
  113. ClientSecret = "secret",
  114. Flow = Flows.ResourceOwner,
  115. ScopeRestrictions = new List<string>
  116. {
  117. "resource"
  118. },
  119. },
  120. new Client
  121. {
  122. ClientName = "Assertion Flow Client",
  123. Enabled = true,
  124. ClientId = "assertionclient",
  125. ClientSecret = "secret",
  126. Flow = Flows.Assertion,
  127. },
  128. new Client
  129. {
  130. ClientName = "Disabled Client",
  131. Enabled = false,
  132. ClientId = "disabled",
  133. ClientSecret = "invalid",
  134. Flow = Flows.Assertion,
  135. }
  136. };
  137. public Task<IEnumerable<Scope>> GetScopesAsync()
  138. {
  139. return Task.FromResult<IEnumerable<Scope>>(new Scope[]
  140. {
  141. new Scope
  142. {
  143. Name = Constants.StandardScopes.OpenId,
  144. Description = "User identifier",
  145. IsOpenIdScope = true,
  146. Claims = (Constants.ScopeToClaimsMapping[Constants.StandardScopes.OpenId].Select(x=>new ScopeClaim{Name = x}))
  147. },
  148. new Scope
  149. {
  150. Name = Constants.StandardScopes.Profile,
  151. Description = "User profile",
  152. IsOpenIdScope = true,
  153. Claims = (Constants.ScopeToClaimsMapping[Constants.StandardScopes.Profile].Select(x=>new ScopeClaim{Name = x}))
  154. },
  155. new Scope
  156. {
  157. Name = "resource",
  158. Description = "resource scope",
  159. IsOpenIdScope = false
  160. },
  161. new Scope
  162. {
  163. Name = "resource2",
  164. Description = "resource scope",
  165. IsOpenIdScope = false
  166. },
  167. });
  168. }
  169. public Task<Client> FindClientByIdAsync(string clientId)
  170. {
  171. return Task.FromResult(_clients.FirstOrDefault(c => c.ClientId == clientId));
  172. }
  173. public bool RequiresConsent(string clientId, ClaimsPrincipal user, IEnumerable<string> scopes)
  174. {
  175. return false;
  176. }
  177. public X509Certificate2 GetSigningCertificate()
  178. {
  179. return _certificate;
  180. }
  181. public string GetIssuerUri()
  182. {
  183. return "https://idsrv3.test";
  184. }
  185. public string GetSiteName()
  186. {
  187. throw new NotImplementedException();
  188. }
  189. public InternalProtectionSettings GetInternalProtectionSettings()
  190. {
  191. throw new NotImplementedException();
  192. }
  193. public string GetPublicHost()
  194. {
  195. throw new NotImplementedException();
  196. }
  197. private static byte[] ReadStream(Stream input)
  198. {
  199. byte[] buffer = new byte[16 * 1024];
  200. using (MemoryStream ms = new MemoryStream())
  201. {
  202. int read;
  203. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  204. {
  205. ms.Write(buffer, 0, read);
  206. }
  207. return ms.ToArray();
  208. }
  209. }
  210. }
  211. }