-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
ApiResourceBuilder.cs
139 lines (122 loc) · 4.82 KB
/
ApiResourceBuilder.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using Duende.IdentityServer.Models;
namespace Microsoft.AspNetCore.ApiAuthorization.IdentityServer
{
/// <summary>
/// A builder for API resources
/// </summary>
public class ApiResourceBuilder
{
private readonly ApiResource _apiResource;
private bool _built;
/// <summary>
/// Creates a new builder for an externally registered API.
/// </summary>
/// <param name="name">The name of the API.</param>
/// <returns>An <see cref="ApiResourceBuilder"/>.</returns>
public static ApiResourceBuilder ApiResource(string name)
{
var apiResource = new ApiResource(name);
return new ApiResourceBuilder(apiResource)
.WithApplicationProfile(ApplicationProfiles.API)
.WithScopes(name);
}
/// <summary>
/// Creates a new builder for an API that coexists with an authorization server.
/// </summary>
/// <param name="name">The name of the API.</param>
/// <returns>An <see cref="ApiResourceBuilder"/>.</returns>
public static ApiResourceBuilder IdentityServerJwt(string name)
{
var apiResource = new ApiResource(name);
return new ApiResourceBuilder(apiResource)
.WithApplicationProfile(ApplicationProfiles.IdentityServerJwt);
}
/// <summary>
/// Initializes a new instance of <see cref="ApiResourceBuilder"/>.
/// </summary>
public ApiResourceBuilder() : this(new ApiResource())
{
}
/// <summary>
/// Initializes a new instance of <see cref="ApiResourceBuilder"/>.
/// </summary>
/// <param name="resource">A preconfigured resource.</param>
public ApiResourceBuilder(ApiResource resource)
{
_apiResource = resource;
}
/// <summary>
/// Sets the application profile for the resource.
/// </summary>
/// <param name="profile">The the profile for the application from <see cref="ApplicationProfiles"/>.</param>
/// <returns>The <see cref="ApiResourceBuilder"/>.</returns>
public ApiResourceBuilder WithApplicationProfile(string profile)
{
_apiResource.Properties.Add(ApplicationProfilesPropertyNames.Profile, profile);
return this;
}
/// <summary>
/// Adds additional scopes to the API resource.
/// </summary>
/// <param name="resourceScopes">The list of scopes.</param>
/// <returns>The <see cref="ApiResourceBuilder"/>.</returns>
public ApiResourceBuilder WithScopes(params string[] resourceScopes)
{
foreach (var scope in resourceScopes)
{
if (_apiResource.Scopes.Any(s => s == scope))
{
continue;
}
_apiResource.Scopes.Add(scope);
}
return this;
}
/// <summary>
/// Replaces the scopes defined for the application with a new set of scopes.
/// </summary>
/// <param name="resourceScopes">The list of scopes.</param>
/// <returns>The <see cref="ApiResourceBuilder"/>.</returns>
public ApiResourceBuilder ReplaceScopes(params string[] resourceScopes)
{
_apiResource.Scopes.Clear();
return WithScopes(resourceScopes);
}
/// <summary>
/// Configures the API resource to allow all clients to access it.
/// </summary>
/// <returns>The <see cref="ApiResourceBuilder"/>.</returns>
public ApiResourceBuilder AllowAllClients()
{
_apiResource.Properties[ApplicationProfilesPropertyNames.Clients] = ApplicationProfilesPropertyValues.AllowAllApplications;
return this;
}
/// <summary>
/// Builds the API resource.
/// </summary>
/// <returns>The built <see cref="Duende.IdentityServer.Models.ApiResource"/>.</returns>
public ApiResource Build()
{
if (_built)
{
throw new InvalidOperationException("ApiResource already built.");
}
_built = true;
return _apiResource;
}
internal ApiResourceBuilder WithAllowedClients(string clientList)
{
_apiResource.Properties[ApplicationProfilesPropertyNames.Clients] = clientList;
return this;
}
internal ApiResourceBuilder FromConfiguration()
{
_apiResource.Properties[ApplicationProfilesPropertyNames.Source] = ApplicationProfilesPropertyValues.Configuration;
return this;
}
}
}