15
15
using Microsoft . AspNetCore . Mvc ;
16
16
using Microsoft . EntityFrameworkCore ;
17
17
18
- namespace Backend . Controllers {
19
- public class AuthorizationController : Controller {
18
+ namespace Backend . Controllers
19
+ {
20
+ public class AuthorizationController : Controller
21
+ {
20
22
private readonly ApplicationContext database ;
21
23
22
- public AuthorizationController ( ApplicationContext database ) {
24
+ public AuthorizationController ( ApplicationContext database )
25
+ {
23
26
this . database = database ;
24
27
}
25
-
28
+
26
29
[ Authorize , HttpGet ( "~/connect/authorize" ) ]
27
- public async Task < IActionResult > Authorize ( CancellationToken cancellationToken ) {
30
+ public async Task < IActionResult > Authorize ( CancellationToken cancellationToken )
31
+ {
28
32
// Note: when a fatal error occurs during the request processing, an OpenID Connect response
29
33
// is prematurely forged and added to the ASP.NET context by OpenIdConnectServerHandler.
30
34
// You can safely remove this part and let ASOS automatically handle the unrecoverable errors
31
35
// by switching ApplicationCanDisplayErrors to false in Startup.cs.
32
36
var response = HttpContext . GetOpenIdConnectResponse ( ) ;
33
- if ( response != null ) {
37
+ if ( response != null )
38
+ {
34
39
return View ( "Error" , response ) ;
35
40
}
36
41
37
42
// Extract the authorization request from the ASP.NET environment.
38
43
var request = HttpContext . GetOpenIdConnectRequest ( ) ;
39
- if ( request == null ) {
40
- return View ( "Error" , new OpenIdConnectResponse {
44
+ if ( request == null )
45
+ {
46
+ return View ( "Error" , new OpenIdConnectResponse
47
+ {
41
48
Error = OpenIdConnectConstants . Errors . ServerError ,
42
49
ErrorDescription = "An internal error has occurred"
43
50
} ) ;
@@ -48,8 +55,10 @@ public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
48
55
// In theory, this null check shouldn't be needed, but a race condition could occur if you
49
56
// manually removed the application details from the database after the initial check made by ASOS.
50
57
var application = await GetApplicationAsync ( request . ClientId , cancellationToken ) ;
51
- if ( application == null ) {
52
- return View ( "Error" , new OpenIdConnectResponse {
58
+ if ( application == null )
59
+ {
60
+ return View ( "Error" , new OpenIdConnectResponse
61
+ {
53
62
Error = OpenIdConnectConstants . Errors . InvalidClient ,
54
63
ErrorDescription = "Details concerning the calling client application cannot be found in the database"
55
64
} ) ;
@@ -61,15 +70,19 @@ public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
61
70
62
71
[ Authorize , FormValueRequired ( "submit.Accept" ) ]
63
72
[ HttpPost ( "~/connect/authorize" ) , ValidateAntiForgeryToken ]
64
- public async Task < IActionResult > Accept ( CancellationToken cancellationToken ) {
73
+ public async Task < IActionResult > Accept ( CancellationToken cancellationToken )
74
+ {
65
75
var response = HttpContext . GetOpenIdConnectResponse ( ) ;
66
- if ( response != null ) {
76
+ if ( response != null )
77
+ {
67
78
return View ( "Error" , response ) ;
68
79
}
69
80
70
81
var request = HttpContext . GetOpenIdConnectRequest ( ) ;
71
- if ( request == null ) {
72
- return View ( "Error" , new OpenIdConnectResponse {
82
+ if ( request == null )
83
+ {
84
+ return View ( "Error" , new OpenIdConnectResponse
85
+ {
73
86
Error = OpenIdConnectConstants . Errors . ServerError ,
74
87
ErrorDescription = "An internal error has occurred"
75
88
} ) ;
@@ -81,12 +94,14 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
81
94
82
95
// Copy the claims retrieved from the external identity provider
83
96
// (e.g Google, Facebook, a WS-Fed provider or another OIDC server).
84
- foreach ( var claim in HttpContext . User . Claims ) {
97
+ foreach ( var claim in HttpContext . User . Claims )
98
+ {
85
99
// Allow ClaimTypes.Name to be added in the id_token.
86
100
// ClaimTypes.NameIdentifier is automatically added, even if its
87
101
// destination is not defined or doesn't include "id_token".
88
102
// The other claims won't be visible for the client application.
89
- if ( claim . Type == ClaimTypes . Name ) {
103
+ if ( claim . Type == ClaimTypes . Name )
104
+ {
90
105
claim . SetDestinations ( OpenIdConnectConstants . Destinations . AccessToken ,
91
106
OpenIdConnectConstants . Destinations . IdentityToken ) ;
92
107
}
@@ -95,8 +110,10 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
95
110
}
96
111
97
112
var application = await GetApplicationAsync ( request . ClientId , cancellationToken ) ;
98
- if ( application == null ) {
99
- return View ( "Error" , new OpenIdConnectResponse {
113
+ if ( application == null )
114
+ {
115
+ return View ( "Error" , new OpenIdConnectResponse
116
+ {
100
117
Error = OpenIdConnectConstants . Errors . InvalidClient ,
101
118
ErrorDescription = "Details concerning the calling client application cannot be found in the database"
102
119
} ) ;
@@ -130,15 +147,19 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
130
147
131
148
[ Authorize , FormValueRequired ( "submit.Deny" ) ]
132
149
[ HttpPost ( "~/connect/authorize" ) , ValidateAntiForgeryToken ]
133
- public IActionResult Deny ( CancellationToken cancellationToken ) {
150
+ public IActionResult Deny ( CancellationToken cancellationToken )
151
+ {
134
152
var response = HttpContext . GetOpenIdConnectResponse ( ) ;
135
- if ( response != null ) {
153
+ if ( response != null )
154
+ {
136
155
return View ( "Error" , response ) ;
137
156
}
138
157
139
158
var request = HttpContext . GetOpenIdConnectRequest ( ) ;
140
- if ( request == null ) {
141
- return View ( "Error" , new OpenIdConnectResponse {
159
+ if ( request == null )
160
+ {
161
+ return View ( "Error" , new OpenIdConnectResponse
162
+ {
142
163
Error = OpenIdConnectConstants . Errors . ServerError ,
143
164
ErrorDescription = "An internal error has occurred"
144
165
} ) ;
@@ -151,9 +172,11 @@ public IActionResult Deny(CancellationToken cancellationToken) {
151
172
}
152
173
153
174
[ HttpGet ( "~/connect/logout" ) ]
154
- public async Task < ActionResult > Logout ( CancellationToken cancellationToken ) {
175
+ public async Task < ActionResult > Logout ( CancellationToken cancellationToken )
176
+ {
155
177
var response = HttpContext . GetOpenIdConnectResponse ( ) ;
156
- if ( response != null ) {
178
+ if ( response != null )
179
+ {
157
180
return View ( "Error" , response ) ;
158
181
}
159
182
@@ -163,8 +186,10 @@ public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
163
186
var identity = await HttpContext . Authentication . AuthenticateAsync ( OpenIdConnectServerDefaults . AuthenticationScheme ) ;
164
187
165
188
var request = HttpContext . GetOpenIdConnectRequest ( ) ;
166
- if ( request == null ) {
167
- return View ( "Error" , new OpenIdConnectResponse {
189
+ if ( request == null )
190
+ {
191
+ return View ( "Error" , new OpenIdConnectResponse
192
+ {
168
193
Error = OpenIdConnectConstants . Errors . ServerError ,
169
194
ErrorDescription = "An internal error has occurred"
170
195
} ) ;
@@ -175,14 +200,16 @@ public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
175
200
176
201
[ HttpPost ( "~/connect/logout" ) ]
177
202
[ ValidateAntiForgeryToken ]
178
- public ActionResult Logout ( ) {
203
+ public ActionResult Logout ( )
204
+ {
179
205
// Returning a SignOutResult will ask the cookies middleware to delete the local cookie created when
180
206
// the user agent is redirected from the external identity provider after a successful authentication flow
181
207
// and will redirect the user agent to the post_logout_redirect_uri specified by the client application.
182
208
return SignOut ( "ServerCookie" , OpenIdConnectServerDefaults . AuthenticationScheme ) ;
183
209
}
184
-
185
- protected virtual Task < Application > GetApplicationAsync ( string identifier , CancellationToken cancellationToken ) {
210
+
211
+ protected virtual Task < Application > GetApplicationAsync ( string identifier , CancellationToken cancellationToken )
212
+ {
186
213
// Retrieve the application details corresponding to the requested client_id.
187
214
return ( from application in database . Applications
188
215
where application . ApplicationID == identifier
0 commit comments