This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 764
/
DefaultViewService.cs
312 lines (279 loc) · 11.1 KB
/
DefaultViewService.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright 2014, 2015 Dominick Baier, Brock Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using IdentityServer3.Core.Extensions;
using IdentityServer3.Core.Models;
using IdentityServer3.Core.Validation;
using IdentityServer3.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IdentityServer3.Core.Services.Default
{
/// <summary>
/// Default view service.
/// </summary>
public class DefaultViewService : IViewService
{
/// <summary>
/// The login view
/// </summary>
public const string LoginView = "login";
/// <summary>
/// The logout view
/// </summary>
public const string LogoutView = "logout";
/// <summary>
/// The logged out view
/// </summary>
public const string LoggedOutView = "loggedOut";
/// <summary>
/// The consent view
/// </summary>
public const string ConsentView = "consent";
/// <summary>
/// The client permissions view
/// </summary>
public const string ClientPermissionsView = "permissions";
/// <summary>
/// The error view
/// </summary>
public const string ErrorView = "error";
/// <summary>
/// The authorize response view
/// </summary>
public const string AuthorizeResponseView = "authorizeresponse";
static readonly Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings()
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
};
/// <summary>
/// The configuration
/// </summary>
protected readonly DefaultViewServiceOptions config;
/// <summary>
/// The view loader
/// </summary>
protected readonly IViewLoader viewLoader;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultViewService"/> class.
/// </summary>
public DefaultViewService(DefaultViewServiceOptions config, IViewLoader viewLoader)
{
if (config == null) throw new ArgumentNullException("config");
if (viewLoader == null) throw new ArgumentNullException("viewLoader");
this.config = config;
this.viewLoader = viewLoader;
}
/// <summary>
/// Loads the HTML for the login page.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="message">The message.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public virtual Task<Stream> Login(LoginViewModel model, SignInMessage message)
{
return Render(model, LoginView);
}
/// <summary>
/// Loads the HTML for the logout prompt page.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="message">The message.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public virtual Task<Stream> Logout(LogoutViewModel model, SignOutMessage message)
{
return Render(model, LogoutView);
}
/// <summary>
/// Loads the HTML for the logged out page informing the user that they have successfully logged out.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="message">The message.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public virtual Task<Stream> LoggedOut(LoggedOutViewModel model, SignOutMessage message)
{
return Render(model, LoggedOutView);
}
/// <summary>
/// Loads the HTML for the user consent page.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="authorizeRequest">The validated authorize request.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public virtual Task<Stream> Consent(ConsentViewModel model, ValidatedAuthorizeRequest authorizeRequest)
{
return Render(model, ConsentView);
}
/// <summary>
/// Loads the HTML for the client permissions page.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public Task<Stream> ClientPermissions(ClientPermissionsViewModel model)
{
return Render(model, ClientPermissionsView);
}
/// <summary>
/// Loads the HTML for the error page.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public virtual Task<Stream> Error(ErrorViewModel model)
{
return Render(model, ErrorView);
}
/// <summary>
/// Loads the HTML for the authorize response page.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>
/// Stream for the HTML
/// </returns>
public virtual async Task<Stream> AuthorizeResponse(AuthorizeResponseViewModel model)
{
var newModel = new CommonViewModel
{
SiteName = model.SiteName,
SiteUrl = model.SiteUrl,
Custom = model.Custom
};
var scripts = new List<string>();
scripts.AddRange(config.Scripts ?? Enumerable.Empty<string>());
scripts.Add("~/assets/app.FormPostResponse.js");
var data = BuildModelDictionary(newModel, AuthorizeResponseView, config.Stylesheets, scripts);
data.Add("responseUri", model.ResponseFormUri);
data.Add("responseFields", model.ResponseFormFields);
string html = await LoadHtmlTemplate(AuthorizeResponseView);
html = FormatHtmlTemplate(html, data);
return html.ToStream();
}
/// <summary>
/// Renders the specified page.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="page">The page.</param>
/// <returns></returns>
protected virtual Task<Stream> Render(CommonViewModel model, string page)
{
return Render(model, page, config.Stylesheets, config.Scripts);
}
/// <summary>
/// Renders the specified page.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="page">The page.</param>
/// <param name="stylesheets">The stylesheets.</param>
/// <param name="scripts">The scripts.</param>
/// <returns></returns>
protected virtual async Task<Stream> Render(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
{
var data = BuildModelDictionary(model, page, stylesheets, scripts);
string html = await LoadHtmlTemplate(page);
if (html == null) return null;
html = FormatHtmlTemplate(html, data);
return html.ToStream();
}
/// <summary>
/// Loads the HTML template.
/// </summary>
/// <param name="page">The page.</param>
/// <returns></returns>
protected virtual Task<string> LoadHtmlTemplate(string page)
{
return this.viewLoader.LoadAsync(page);
}
/// <summary>
/// Formats the specified HTML template.
/// </summary>
/// <param name="htmlTemplate">The HTML template.</param>
/// <param name="model">The model.</param>
/// <returns></returns>
protected string FormatHtmlTemplate(string htmlTemplate, object model)
{
return AssetManager.Format(htmlTemplate, model);
}
/// <summary>
/// Builds the model.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="page">The page.</param>
/// <param name="stylesheets">The stylesheets.</param>
/// <param name="scripts">The scripts.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// model
/// or
/// stylesheets
/// or
/// scripts
/// </exception>
protected object BuildModel(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
{
return BuildModelDictionary(model, page, stylesheets, scripts);
}
Dictionary<string, object> BuildModelDictionary(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
{
if (model == null) throw new ArgumentNullException("model");
if (stylesheets == null) throw new ArgumentNullException("stylesheets");
if (scripts == null) throw new ArgumentNullException("scripts");
var applicationPath = new Uri(model.SiteUrl).AbsolutePath;
if (applicationPath.EndsWith("/")) applicationPath = applicationPath.Substring(0, applicationPath.Length - 1);
var json = Newtonsoft.Json.JsonConvert.SerializeObject(model, Newtonsoft.Json.Formatting.None, settings);
var additionalStylesheets = BuildTags("<link href='{0}' rel='stylesheet'>", applicationPath, stylesheets);
var additionalScripts = BuildTags("<script src='{0}'></script>", applicationPath, scripts);
return new Dictionary<string, object>
{
{ "siteName" , Microsoft.Security.Application.Encoder.HtmlEncode(model.SiteName) },
{ "applicationPath", applicationPath },
{ "model", Microsoft.Security.Application.Encoder.HtmlEncode(json) },
{ "page", page },
{ "stylesheets", additionalStylesheets },
{ "scripts", additionalScripts }
};
}
string BuildTags(string tagFormat, string basePath, IEnumerable<string> values)
{
if (values == null || !values.Any()) return String.Empty;
var sb = new StringBuilder();
foreach (var value in values)
{
var path = value;
if (path.StartsWith("~/"))
{
path = basePath + path.Substring(1);
}
sb.AppendFormat(tagFormat, path);
sb.AppendLine();
}
return sb.ToString();
}
}
}