forked from Soluto/oidc-server-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
188 lines (175 loc) · 8.19 KB
/
Config.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using Duende.IdentityServer.Configuration;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Test;
using OpenIdConnectServer.Helpers;
using OpenIdConnectServer.YamlConverters;
using YamlDotNet.Serialization;
namespace OpenIdConnectServer
{
public static class Config
{
public static AspNetServicesOptions GetAspNetServicesOptions()
{
string aspNetServicesOptionsStr = Environment.GetEnvironmentVariable("ASPNET_SERVICES_OPTIONS_INLINE");
if (string.IsNullOrWhiteSpace(aspNetServicesOptionsStr))
{
var aspNetServicesOptionsPath = Environment.GetEnvironmentVariable("ASPNET_SERVICES_OPTIONS_PATH");
if (string.IsNullOrWhiteSpace(aspNetServicesOptionsPath))
{
return new AspNetServicesOptions();
}
aspNetServicesOptionsStr = File.ReadAllText(aspNetServicesOptionsPath);
}
var aspNetServicesOptions = DeserializeObject<AspNetServicesOptions>(aspNetServicesOptionsStr);
return aspNetServicesOptions;
}
public static IdentityServerOptions GetServerOptions()
{
string serverOptionsStr = Environment.GetEnvironmentVariable("SERVER_OPTIONS_INLINE");
if (string.IsNullOrWhiteSpace(serverOptionsStr))
{
var serverOptionsFilePath = Environment.GetEnvironmentVariable("SERVER_OPTIONS_PATH");
if (string.IsNullOrWhiteSpace(serverOptionsFilePath))
{
return new IdentityServerOptions();
}
serverOptionsStr = File.ReadAllText(serverOptionsFilePath);
}
var serverOptions = DeserializeObject<IdentityServerOptions>(serverOptionsStr);
return serverOptions;
}
public static void ConfigureOptions<T>(string optionsName)
{
string optionsStr = Environment.GetEnvironmentVariable($"{optionsName.ToUpper()}_OPTIONS_INLINE");
if (string.IsNullOrWhiteSpace(optionsStr))
{
var optionsFilePath = Environment.GetEnvironmentVariable($"{optionsName.ToUpper()}_OPTIONS_PATH");
if (string.IsNullOrWhiteSpace(optionsFilePath))
{
return;
}
optionsStr = File.ReadAllText(optionsFilePath);
}
OptionsHelper.ConfigureOptions<T>(optionsStr);
}
public static IEnumerable<string> GetServerCorsAllowedOrigins()
{
string allowedOriginsStr = Environment.GetEnvironmentVariable("SERVER_CORS_ALLOWED_ORIGINS_INLINE");
if (string.IsNullOrWhiteSpace(allowedOriginsStr))
{
var allowedOriginsFilePath = Environment.GetEnvironmentVariable("SERVER_CORS_ALLOWED_ORIGINS_PATH");
if (string.IsNullOrWhiteSpace(allowedOriginsFilePath))
{
return null;
}
allowedOriginsStr = File.ReadAllText(allowedOriginsFilePath);
}
var allowedOrigins = DeserializeObject<IEnumerable<string>>(allowedOriginsStr);
return allowedOrigins;
}
public static IEnumerable<ApiScope> GetApiScopes()
{
string apiScopesStr = Environment.GetEnvironmentVariable("API_SCOPES_INLINE");
if (string.IsNullOrWhiteSpace(apiScopesStr))
{
var apiScopesFilePath = Environment.GetEnvironmentVariable("API_SCOPES_PATH");
if (string.IsNullOrWhiteSpace(apiScopesFilePath))
{
return new List<ApiScope>();
}
apiScopesStr = File.ReadAllText(apiScopesFilePath);
}
var apiScopes = DeserializeObject<IEnumerable<ApiScope>>(apiScopesStr);
return apiScopes;
}
public static IEnumerable<ApiResource> GetApiResources()
{
string apiResourcesStr = Environment.GetEnvironmentVariable("API_RESOURCES_INLINE");
if (string.IsNullOrWhiteSpace(apiResourcesStr))
{
var apiResourcesFilePath = Environment.GetEnvironmentVariable("API_RESOURCES_PATH");
if (string.IsNullOrWhiteSpace(apiResourcesFilePath))
{
return new List<ApiResource>();
}
apiResourcesStr = File.ReadAllText(apiResourcesFilePath);
}
var apiResources = DeserializeObject<IEnumerable<ApiResource>>(apiResourcesStr);
return apiResources;
}
public static IEnumerable<Client> GetClients()
{
string configStr = Environment.GetEnvironmentVariable("CLIENTS_CONFIGURATION_INLINE");
if (string.IsNullOrWhiteSpace(configStr))
{
var configFilePath = Environment.GetEnvironmentVariable("CLIENTS_CONFIGURATION_PATH");
if (string.IsNullOrWhiteSpace(configFilePath))
{
throw new ArgumentNullException("You must set either CLIENTS_CONFIGURATION_INLINE or CLIENTS_CONFIGURATION_PATH env variable");
}
configStr = File.ReadAllText(configFilePath);
}
var configClients = DeserializeObject<IEnumerable<Client>>(configStr);
return configClients;
}
public static IEnumerable<IdentityResource> GetIdentityResources()
{
IEnumerable<IdentityResource> identityResources = new List<IdentityResource>();
var overrideStandardResources = Environment.GetEnvironmentVariable("OVERRIDE_STANDARD_IDENTITY_RESOURCES");
if (string.IsNullOrEmpty(overrideStandardResources) || Boolean.Parse(overrideStandardResources) != true)
{
var standardResources = new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email()
};
identityResources = identityResources.Union(standardResources);
}
return identityResources.Union(GetCustomIdentityResources());
}
public static List<TestUser> GetUsers()
{
string configStr = Environment.GetEnvironmentVariable("USERS_CONFIGURATION_INLINE");
if (string.IsNullOrWhiteSpace(configStr))
{
var configFilePath = Environment.GetEnvironmentVariable("USERS_CONFIGURATION_PATH");
if (string.IsNullOrWhiteSpace(configFilePath))
{
return new List<TestUser>();
}
configStr = File.ReadAllText(configFilePath);
}
var configUsers = DeserializeObject<List<TestUser>>(configStr);
return configUsers;
}
private static IEnumerable<IdentityResource> GetCustomIdentityResources()
{
string identityResourcesStr = Environment.GetEnvironmentVariable("IDENTITY_RESOURCES_INLINE");
if (string.IsNullOrWhiteSpace(identityResourcesStr))
{
var identityResourcesFilePath = Environment.GetEnvironmentVariable("IDENTITY_RESOURCES_PATH");
if (string.IsNullOrWhiteSpace(identityResourcesFilePath))
{
return new List<IdentityResource>();
}
identityResourcesStr = File.ReadAllText(identityResourcesFilePath);
}
var identityResourceConfig = DeserializeObject<IdentityResourceConfig[]>(identityResourcesStr);
return identityResourceConfig.Select(c => new IdentityResource(c.Name, c.ClaimTypes));
}
private static T DeserializeObject<T>(string value)
{
var deserializer = new DeserializerBuilder()
.WithTypeConverter(new ClaimYamlConverter())
.WithTypeConverter(new SecretYamlConverter())
.Build();
return deserializer.Deserialize<T>(value);
}
private class IdentityResourceConfig
{
public string Name { get; set; }
public IEnumerable<string> ClaimTypes { get; set; }
}
}
}