@@ -11,11 +11,13 @@ namespace Backend.Providers {
11
11
public sealed class AuthorizationProvider : OpenIdConnectServerProvider {
12
12
public override async Task ValidateAuthorizationRequest ( ValidateAuthorizationRequestContext context ) {
13
13
// Note: the OpenID Connect server middleware supports the authorization code, implicit and hybrid flows
14
- // but this authorization provider only accepts response_type=token authorization/authentication requests.
15
- if ( ! context . Request . IsImplicitFlow ( ) ) {
14
+ // but this authorization provider only accepts response_type=code authorization/authentication requests.
15
+ // You may consider relaxing it to support the implicit or hybrid flows. In this case, consider adding
16
+ // checks rejecting implicit/hybrid authorization requests when the client is a confidential application.
17
+ if ( ! context . Request . IsAuthorizationCodeFlow ( ) ) {
16
18
context . Reject (
17
19
error : OpenIdConnectConstants . Errors . UnsupportedResponseType ,
18
- description : "Only the implicit flow is supported by this authorization server." ) ;
20
+ description : "Only the authorization code flow is supported by this authorization server." ) ;
19
21
20
22
return ;
21
23
}
@@ -43,7 +45,7 @@ public override async Task ValidateAuthorizationRequest(ValidateAuthorizationReq
43
45
if ( application == null ) {
44
46
context . Reject (
45
47
error : OpenIdConnectConstants . Errors . InvalidClient ,
46
- description : "Application not found in the database: ensure that your client_id is correct" ) ;
48
+ description : "Application not found in the database: ensure that your client_id is correct. " ) ;
47
49
48
50
return ;
49
51
}
@@ -52,14 +54,56 @@ public override async Task ValidateAuthorizationRequest(ValidateAuthorizationReq
52
54
! string . Equals ( context . RedirectUri , application . RedirectUri , StringComparison . Ordinal ) ) {
53
55
context . Reject (
54
56
error : OpenIdConnectConstants . Errors . InvalidClient ,
55
- description : "Invalid redirect_uri" ) ;
57
+ description : "Invalid redirect_uri. " ) ;
56
58
57
59
return ;
58
60
}
59
61
60
62
context . Validate ( application . RedirectUri ) ;
61
63
}
62
64
65
+ public override async Task ValidateTokenRequest ( ValidateTokenRequestContext context ) {
66
+ // Note: the OpenID Connect server middleware supports authorization code, refresh token, client credentials
67
+ // and resource owner password credentials grant types but this authorization provider uses a safer policy
68
+ // rejecting the last two ones. You may consider relaxing it to support the ROPC or client credentials grant types.
69
+ if ( ! context . Request . IsAuthorizationCodeGrantType ( ) && ! context . Request . IsRefreshTokenGrantType ( ) ) {
70
+ context . Reject (
71
+ error : OpenIdConnectConstants . Errors . UnsupportedGrantType ,
72
+ description : "Only authorization code and refresh token grant types " +
73
+ "are accepted by this authorization server." ) ;
74
+
75
+ return ;
76
+ }
77
+
78
+ // Reject the request if the client identifier is missing.
79
+ if ( string . IsNullOrEmpty ( context . ClientId ) ) {
80
+ context . Reject (
81
+ error : OpenIdConnectConstants . Errors . InvalidRequest ,
82
+ description : "The mandatory client_id parameter was missing" ) ;
83
+
84
+ return ;
85
+ }
86
+
87
+ var database = context . HttpContext . RequestServices . GetRequiredService < ApplicationContext > ( ) ;
88
+
89
+ // Retrieve the application details corresponding to the requested client_id.
90
+ var application = await ( from entity in database . Applications
91
+ where entity . ApplicationID == context . ClientId
92
+ select entity ) . SingleOrDefaultAsync ( context . HttpContext . RequestAborted ) ;
93
+
94
+ if ( application == null ) {
95
+ context . Reject (
96
+ error : OpenIdConnectConstants . Errors . InvalidClient ,
97
+ description : "Application not found in the database: ensure that your client_id is correct." ) ;
98
+
99
+ return ;
100
+ }
101
+
102
+ // Note: the client credentials cannot be safely stored in the Cordova application.
103
+ // In this case, context.Skip() is called to inform the OIDC server that the client is not trusted.
104
+ context . Skip ( ) ;
105
+ }
106
+
63
107
public override async Task ValidateLogoutRequest ( ValidateLogoutRequestContext context ) {
64
108
var database = context . HttpContext . RequestServices . GetRequiredService < ApplicationContext > ( ) ;
65
109
@@ -74,7 +118,7 @@ public override async Task ValidateLogoutRequest(ValidateLogoutRequestContext co
74
118
if ( ! await database . Applications . AnyAsync ( application => application . LogoutRedirectUri == context . PostLogoutRedirectUri ) ) {
75
119
context . Reject (
76
120
error : OpenIdConnectConstants . Errors . InvalidClient ,
77
- description : "Invalid post_logout_redirect_uri" ) ;
121
+ description : "Invalid post_logout_redirect_uri. " ) ;
78
122
79
123
return ;
80
124
}
0 commit comments