1515using Microsoft . AspNetCore . Mvc ;
1616using Microsoft . EntityFrameworkCore ;
1717
18- namespace Backend . Controllers {
19- public class AuthorizationController : Controller {
18+ namespace Backend . Controllers
19+ {
20+ public class AuthorizationController : Controller
21+ {
2022 private readonly ApplicationContext database ;
2123
22- public AuthorizationController ( ApplicationContext database ) {
24+ public AuthorizationController ( ApplicationContext database )
25+ {
2326 this . database = database ;
2427 }
25-
28+
2629 [ Authorize , HttpGet ( "~/connect/authorize" ) ]
27- public async Task < IActionResult > Authorize ( CancellationToken cancellationToken ) {
30+ public async Task < IActionResult > Authorize ( CancellationToken cancellationToken )
31+ {
2832 // Note: when a fatal error occurs during the request processing, an OpenID Connect response
2933 // is prematurely forged and added to the ASP.NET context by OpenIdConnectServerHandler.
3034 // You can safely remove this part and let ASOS automatically handle the unrecoverable errors
3135 // by switching ApplicationCanDisplayErrors to false in Startup.cs.
3236 var response = HttpContext . GetOpenIdConnectResponse ( ) ;
33- if ( response != null ) {
37+ if ( response != null )
38+ {
3439 return View ( "Error" , response ) ;
3540 }
3641
3742 // Extract the authorization request from the ASP.NET environment.
3843 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+ {
4148 Error = OpenIdConnectConstants . Errors . ServerError ,
4249 ErrorDescription = "An internal error has occurred"
4350 } ) ;
@@ -48,8 +55,10 @@ public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
4855 // In theory, this null check shouldn't be needed, but a race condition could occur if you
4956 // manually removed the application details from the database after the initial check made by ASOS.
5057 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+ {
5362 Error = OpenIdConnectConstants . Errors . InvalidClient ,
5463 ErrorDescription = "Details concerning the calling client application cannot be found in the database"
5564 } ) ;
@@ -61,15 +70,19 @@ public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
6170
6271 [ Authorize , FormValueRequired ( "submit.Accept" ) ]
6372 [ HttpPost ( "~/connect/authorize" ) , ValidateAntiForgeryToken ]
64- public async Task < IActionResult > Accept ( CancellationToken cancellationToken ) {
73+ public async Task < IActionResult > Accept ( CancellationToken cancellationToken )
74+ {
6575 var response = HttpContext . GetOpenIdConnectResponse ( ) ;
66- if ( response != null ) {
76+ if ( response != null )
77+ {
6778 return View ( "Error" , response ) ;
6879 }
6980
7081 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+ {
7386 Error = OpenIdConnectConstants . Errors . ServerError ,
7487 ErrorDescription = "An internal error has occurred"
7588 } ) ;
@@ -81,12 +94,14 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
8194
8295 // Copy the claims retrieved from the external identity provider
8396 // (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+ {
8599 // Allow ClaimTypes.Name to be added in the id_token.
86100 // ClaimTypes.NameIdentifier is automatically added, even if its
87101 // destination is not defined or doesn't include "id_token".
88102 // The other claims won't be visible for the client application.
89- if ( claim . Type == ClaimTypes . Name ) {
103+ if ( claim . Type == ClaimTypes . Name )
104+ {
90105 claim . SetDestinations ( OpenIdConnectConstants . Destinations . AccessToken ,
91106 OpenIdConnectConstants . Destinations . IdentityToken ) ;
92107 }
@@ -95,8 +110,10 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
95110 }
96111
97112 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+ {
100117 Error = OpenIdConnectConstants . Errors . InvalidClient ,
101118 ErrorDescription = "Details concerning the calling client application cannot be found in the database"
102119 } ) ;
@@ -130,15 +147,19 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
130147
131148 [ Authorize , FormValueRequired ( "submit.Deny" ) ]
132149 [ HttpPost ( "~/connect/authorize" ) , ValidateAntiForgeryToken ]
133- public IActionResult Deny ( CancellationToken cancellationToken ) {
150+ public IActionResult Deny ( CancellationToken cancellationToken )
151+ {
134152 var response = HttpContext . GetOpenIdConnectResponse ( ) ;
135- if ( response != null ) {
153+ if ( response != null )
154+ {
136155 return View ( "Error" , response ) ;
137156 }
138157
139158 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+ {
142163 Error = OpenIdConnectConstants . Errors . ServerError ,
143164 ErrorDescription = "An internal error has occurred"
144165 } ) ;
@@ -151,9 +172,11 @@ public IActionResult Deny(CancellationToken cancellationToken) {
151172 }
152173
153174 [ HttpGet ( "~/connect/logout" ) ]
154- public async Task < ActionResult > Logout ( CancellationToken cancellationToken ) {
175+ public async Task < ActionResult > Logout ( CancellationToken cancellationToken )
176+ {
155177 var response = HttpContext . GetOpenIdConnectResponse ( ) ;
156- if ( response != null ) {
178+ if ( response != null )
179+ {
157180 return View ( "Error" , response ) ;
158181 }
159182
@@ -163,8 +186,10 @@ public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
163186 var identity = await HttpContext . Authentication . AuthenticateAsync ( OpenIdConnectServerDefaults . AuthenticationScheme ) ;
164187
165188 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+ {
168193 Error = OpenIdConnectConstants . Errors . ServerError ,
169194 ErrorDescription = "An internal error has occurred"
170195 } ) ;
@@ -175,14 +200,16 @@ public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
175200
176201 [ HttpPost ( "~/connect/logout" ) ]
177202 [ ValidateAntiForgeryToken ]
178- public ActionResult Logout ( ) {
203+ public ActionResult Logout ( )
204+ {
179205 // Returning a SignOutResult will ask the cookies middleware to delete the local cookie created when
180206 // the user agent is redirected from the external identity provider after a successful authentication flow
181207 // and will redirect the user agent to the post_logout_redirect_uri specified by the client application.
182208 return SignOut ( "ServerCookie" , OpenIdConnectServerDefaults . AuthenticationScheme ) ;
183209 }
184-
185- protected virtual Task < Application > GetApplicationAsync ( string identifier , CancellationToken cancellationToken ) {
210+
211+ protected virtual Task < Application > GetApplicationAsync ( string identifier , CancellationToken cancellationToken )
212+ {
186213 // Retrieve the application details corresponding to the requested client_id.
187214 return ( from application in database . Applications
188215 where application . ApplicationID == identifier
0 commit comments