1- namespace OAuthAuthorizationServer . Controllers
2- {
3- using DotNetOpenAuth . Messaging ;
4- using DotNetOpenAuth . OAuth2 ;
5- using OAuthAuthorizationServer . Code ;
6- using OAuthAuthorizationServer . Models ;
1+ namespace OAuthAuthorizationServer . Controllers {
72 using System ;
3+ using System . Collections . Generic ;
84 using System . Linq ;
95 using System . Net ;
6+ using System . Security . Cryptography ;
107 using System . Threading . Tasks ;
118 using System . Web ;
129 using System . Web . Mvc ;
10+ using DotNetOpenAuth . Messaging ;
11+ using DotNetOpenAuth . OAuth2 ;
12+ using OAuthAuthorizationServer . Code ;
13+ using OAuthAuthorizationServer . Models ;
1314
14- public class OAuthController : Controller
15- {
15+ public class OAuthController : Controller {
1616 private readonly AuthorizationServer authorizationServer = new AuthorizationServer ( new OAuth2AuthorizationServer ( ) ) ;
1717
1818 /// <summary>
1919 /// The OAuth 2.0 token endpoint.
2020 /// </summary>
2121 /// <returns>The response to the Client.</returns>
22- public async Task < ActionResult > Token ( )
23- {
22+ public async Task < ActionResult > Token ( ) {
2423 var request = await this . authorizationServer . HandleTokenRequestAsync ( this . Request , this . Response . ClientDisconnectedToken ) ;
2524 Response . ContentType = request . Content . Headers . ContentType . ToString ( ) ;
2625 return request . AsActionResult ( ) ;
@@ -32,27 +31,23 @@ public async Task<ActionResult> Token()
3231 /// <returns>The browser HTML response that prompts the user to authorize the client.</returns>
3332 [ Authorize , AcceptVerbs ( HttpVerbs . Get | HttpVerbs . Post ) ]
3433 [ HttpHeader ( "x-frame-options" , "SAMEORIGIN" ) ] // mitigates clickjacking
35- public async Task < ActionResult > Authorize ( )
36- {
34+ public async Task < ActionResult > Authorize ( ) {
3735 var pendingRequest = await this . authorizationServer . ReadAuthorizationRequestAsync ( Request , Response . ClientDisconnectedToken ) ;
38- if ( pendingRequest == null )
39- {
36+ if ( pendingRequest == null ) {
4037 throw new HttpException ( ( int ) HttpStatusCode . BadRequest , "Missing authorization request." ) ;
4138 }
4239
4340 var requestingClient = MvcApplication . DataContext . Clients . First ( c => c . ClientIdentifier == pendingRequest . ClientIdentifier ) ;
4441
4542 // Consider auto-approving if safe to do so.
46- if ( ( ( OAuth2AuthorizationServer ) this . authorizationServer . AuthorizationServerServices ) . CanBeAutoApproved ( pendingRequest ) )
47- {
43+ if ( ( ( OAuth2AuthorizationServer ) this . authorizationServer . AuthorizationServerServices ) . CanBeAutoApproved ( pendingRequest ) ) {
4844 var approval = this . authorizationServer . PrepareApproveAuthorizationRequest ( pendingRequest , HttpContext . User . Identity . Name ) ;
4945 var response = await this . authorizationServer . Channel . PrepareResponseAsync ( approval , Response . ClientDisconnectedToken ) ;
5046 Response . ContentType = response . Content . Headers . ContentType . ToString ( ) ;
5147 return response . AsActionResult ( ) ;
5248 }
5349
54- var model = new AccountAuthorizeModel
55- {
50+ var model = new AccountAuthorizeModel {
5651 ClientApp = requestingClient . Name ,
5752 Scope = pendingRequest . Scope ,
5853 AuthorizationRequest = pendingRequest ,
@@ -67,24 +62,20 @@ public async Task<ActionResult> Authorize()
6762 /// <param name="isApproved">if set to <c>true</c>, the user has authorized the Client; <c>false</c> otherwise.</param>
6863 /// <returns>HTML response that redirects the browser to the Client.</returns>
6964 [ Authorize , HttpPost , ValidateAntiForgeryToken ]
70- public async Task < ActionResult > AuthorizeResponse ( bool isApproved )
71- {
65+ public async Task < ActionResult > AuthorizeResponse ( bool isApproved ) {
7266 var pendingRequest = await this . authorizationServer . ReadAuthorizationRequestAsync ( Request , Response . ClientDisconnectedToken ) ;
73- if ( pendingRequest == null )
74- {
67+ if ( pendingRequest == null ) {
7568 throw new HttpException ( ( int ) HttpStatusCode . BadRequest , "Missing authorization request." ) ;
7669 }
7770
7871 IDirectedProtocolMessage response ;
79- if ( isApproved )
80- {
72+ if ( isApproved ) {
8173 // The authorization we file in our database lasts until the user explicitly revokes it.
8274 // You can cause the authorization to expire by setting the ExpirationDateUTC
8375 // property in the below created ClientAuthorization.
8476 var client = MvcApplication . DataContext . Clients . First ( c => c . ClientIdentifier == pendingRequest . ClientIdentifier ) ;
8577 client . ClientAuthorizations . Add (
86- new ClientAuthorization
87- {
78+ new ClientAuthorization {
8879 Scope = OAuthUtilities . JoinScopes ( pendingRequest . Scope ) ,
8980 User = MvcApplication . LoggedInUser ,
9081 CreatedOnUtc = DateTime . UtcNow ,
@@ -94,9 +85,7 @@ public async Task<ActionResult> AuthorizeResponse(bool isApproved)
9485 // In this simple sample, the user either agrees to the entire scope requested by the client or none of it.
9586 // But in a real app, you could grant a reduced scope of access to the client by passing a scope parameter to this method.
9687 response = this . authorizationServer . PrepareApproveAuthorizationRequest ( pendingRequest , User . Identity . Name ) ;
97- }
98- else
99- {
88+ } else {
10089 response = this . authorizationServer . PrepareRejectAuthorizationRequest ( pendingRequest ) ;
10190 }
10291
0 commit comments