/
ClientCollection.cs
102 lines (93 loc) · 3.71 KB
/
ClientCollection.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
101
102
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.AspNetCore.ApiAuthorization.IdentityServer
{
/// <summary>
/// A collection of <see cref="Client"/>.
/// </summary>
public class ClientCollection : Collection<Client>
{
/// <summary>
/// Initializes a new instance of <see cref="ClientCollection"/>.
/// </summary>
public ClientCollection()
{
}
/// <summary>
/// Initializes a new instance of <see cref="ClientCollection"/> with the given
/// clients in <paramref name="list"/>.
/// </summary>
/// <param name="list">The initial list of <see cref="Client"/>.</param>
public ClientCollection(IList<Client> list) : base(list)
{
}
/// <summary>
/// Gets a client given its client id.
/// </summary>
/// <param name="key">The name of the <see cref="Client"/>.</param>
/// <returns>The <see cref="Client"/>.</returns>
public Client this[string key]
{
get
{
for (var i = 0; i < Items.Count; i++)
{
var candidate = Items[i];
if (string.Equals(candidate.ClientId, key, StringComparison.Ordinal))
{
return candidate;
}
}
throw new InvalidOperationException($"Client '{key}' not found.");
}
}
/// <summary>
/// Adds the clients in <paramref name="clients"/> to the collection.
/// </summary>
/// <param name="clients">The list of <see cref="Client"/> to add.</param>
public void AddRange(params Client[] clients)
{
foreach (var client in clients)
{
Add(client);
}
}
/// <summary>
/// Adds a single page application that coexists with an authorization server.
/// </summary>
/// <param name="clientId">The client id for the single page application.</param>
/// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the default single page application.</param>
public void AddIdentityServerSPA(string clientId, Action<ClientBuilder> configure)
{
var app = ClientBuilder.IdentityServerSPA(clientId);
configure(app);
Add(app.Build());
}
/// <summary>
/// Adds an externally registered single page application.
/// </summary>
/// <param name="clientId">The client id for the single page application.</param>
/// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the default single page application.</param>
public void AddSPA(string clientId, Action<ClientBuilder> configure)
{
var app = ClientBuilder.SPA(clientId);
configure(app);
Add(app.Build());
}
/// <summary>
/// Adds an externally registered native application..
/// </summary>
/// <param name="clientId">The client id for the single page application.</param>
/// <param name="configure">The <see cref="Action{ClientBuilder}"/> to configure the native application.</param>
public void AddNativeApp(string clientId, Action<ClientBuilder> configure)
{
var app = ClientBuilder.NativeApp(clientId);
configure(app);
Add(app.Build());
}
}
}