1 // Copyright (c) .NET Foundation. All rights reserved. 2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 3 4 using IdentityServer4.Models; 5 using System; 6 using System.Collections.Generic; 7 using System.Collections.ObjectModel; 8 9 namespace Microsoft.AspNetCore.ApiAuthorization.IdentityServer 10 { 11 /// <summary> 12 /// A collection of <see cref="Client"/>. 13 /// </summary> 14 public class ClientCollection : Collection<Client> 15 { 16 /// <summary> 17 /// Initializes a new instance of <see cref="ClientCollection"/>. 18 /// </summary> ClientCollection()19 public ClientCollection() 20 { 21 } 22 23 /// <summary> 24 /// Initializes a new instance of <see cref="ClientCollection"/> with the given 25 /// clients in <paramref name="list"/>. 26 /// </summary> 27 /// <param name="list">The initial list of <see cref="Client"/>.</param> ClientCollection(IList<Client> list)28 public ClientCollection(IList<Client> list) : base(list) 29 { 30 } 31 32 /// <summary> 33 /// Gets a client given its client id. 34 /// </summary> 35 /// <param name="key">The name of the <see cref="Client"/>.</param> 36 /// <returns>The <see cref="Client"/>.</returns> 37 public Client this[string key] 38 { 39 get 40 { 41 for (var i = 0; i < Items.Count; i++) 42 { 43 var candidate = Items[i]; 44 if (string.Equals(candidate.ClientId, key, StringComparison.Ordinal)) 45 { 46 return candidate; 47 } 48 } 49 50 throw new InvalidOperationException($"Client '{key}' not found."); 51 } 52 } 53 54 /// <summary> 55 /// Adds the clients in <paramref name="clients"/> to the collection. 56 /// </summary> 57 /// <param name="clients">The list of <see cref="Client"/> to add.</param> AddRange(IEnumerable<Client> clients)58 public void AddRange(IEnumerable<Client> clients) 59 { 60 foreach (var client in clients) 61 { 62 Add(client); 63 } 64 } 65 66 /// <summary> 67 /// Adds the clients in <paramref name="clients"/> to the collection. 68 /// </summary> 69 /// <param name="clients">The list of <see cref="Client"/> to add.</param> AddRange(params Client[] clients)70 public void AddRange(params Client[] clients) 71 { 72 foreach (var client in clients) 73 { 74 Add(client); 75 } 76 } 77 78 /// <summary> 79 /// Adds a single page application that coexists with an authorization server. 80 /// </summary> 81 /// <param name="clientId">The client id for the single page application.</param> 82 /// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the default single page application.</param> AddIdentityServerSPA(string clientId, Action<ClientBuilder> configure)83 public void AddIdentityServerSPA(string clientId, Action<ClientBuilder> configure) 84 { 85 var app = ClientBuilder.IdentityServerSPA(clientId); 86 configure(app); 87 Add(app.Build()); 88 } 89 90 /// <summary> 91 /// Adds an externally registered single page application. 92 /// </summary> 93 /// <param name="clientId">The client id for the single page application.</param> 94 /// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the default single page application.</param> AddSPA(string clientId, Action<ClientBuilder> configure)95 public void AddSPA(string clientId, Action<ClientBuilder> configure) 96 { 97 var app = ClientBuilder.SPA(clientId); 98 configure(app); 99 Add(app.Build()); 100 } 101 102 /// <summary> 103 /// Adds an externally registered native application.. 104 /// </summary> 105 /// <param name="clientId">The client id for the single page application.</param> 106 /// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the native application.</param> AddNativeApp(string clientId, Action<ClientBuilder> configure)107 public void AddNativeApp(string clientId, Action<ClientBuilder> configure) 108 { 109 var app = ClientBuilder.NativeApp(clientId); 110 configure(app); 111 Add(app.Build()); 112 } 113 114 /// <summary> 115 /// Adds an externally registered web application.. 116 /// </summary> 117 /// <param name="clientId">The client id for the web application.</param> 118 /// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the web application.</param> AddWebApplication(string clientId, Action<ClientBuilder> configure)119 public void AddWebApplication(string clientId, Action<ClientBuilder> configure) 120 { 121 var app = ClientBuilder.WebApplication(clientId); 122 configure(app); 123 Add(app.Build()); 124 } 125 } 126 } 127