Skip to content

Commit f014ddb

Browse files
Use AdditionalAuthorizationParameters
- Ensure that overrides of `BuildChallengeUrl()` that do not call the base implementation add the values from the new `AdditionalAuthorizationParameters` property. - Move all static challenge parameters into the `AdditionalAuthorizationParameters` property on the options. - Remove `BuildChallengeUrl()` overrides, where possible, to just use `AdditionalAuthorizationParameters` instead.
1 parent 4409f9e commit f014ddb

File tree

15 files changed

+71
-25
lines changed

15 files changed

+71
-25
lines changed

src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationHandler.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,17 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
210210
{
211211
["app_id"] = Options.ClientId, // Used instead of "client_id"
212212
["scope"] = scope,
213-
["response_type"] = "code",
214213
["redirect_uri"] = redirectUri,
215214
};
216215

216+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
217+
{
218+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
219+
{
220+
parameters[parameter.Key] = parameter.Value;
221+
}
222+
}
223+
217224
if (Options.UsePkce)
218225
{
219226
var bytes = RandomNumberGenerator.GetBytes(256 / 8);

src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public AlipayAuthenticationOptions()
2222
TokenEndpoint = AlipayAuthenticationDefaults.TokenEndpoint;
2323
UserInformationEndpoint = AlipayAuthenticationDefaults.UserInformationEndpoint;
2424

25+
AdditionalAuthorizationParameters["response_type"] = "code";
26+
2527
Scope.Add("auth_user");
2628

2729
ClaimActions.MapJsonKey(Claims.Avatar, "avatar");

src/AspNet.Security.OAuth.Apple/AppleAuthenticationHandler.cs

-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Text;
1010
using System.Text.Encodings.Web;
1111
using System.Text.Json;
12-
using Microsoft.AspNetCore.WebUtilities;
1312
using Microsoft.Extensions.Logging;
1413
using Microsoft.Extensions.Options;
1514
using Microsoft.Extensions.Primitives;
@@ -37,17 +36,6 @@ public partial class AppleAuthenticationHandler(
3736
set { base.Events = value; }
3837
}
3938

40-
/// <inheritdoc />
41-
protected override string BuildChallengeUrl(
42-
[NotNull] AuthenticationProperties properties,
43-
[NotNull] string redirectUri)
44-
{
45-
var challengeUrl = base.BuildChallengeUrl(properties, redirectUri);
46-
47-
// Apple requires the response mode to be form_post when the email or name scopes are requested
48-
return QueryHelpers.AddQueryString(challengeUrl, "response_mode", "form_post");
49-
}
50-
5139
/// <inheritdoc />
5240
protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new AppleAuthenticationEvents());
5341

src/AspNet.Security.OAuth.Apple/AppleAuthenticationOptions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public AppleAuthenticationOptions()
2929

3030
Events = new AppleAuthenticationEvents();
3131

32+
// Apple requires the response mode to be form_post when the email or name scopes are requested
33+
AdditionalAuthorizationParameters["response_mode"] = "form_post";
34+
3235
Scope.Add("openid");
3336
Scope.Add("name");
3437
Scope.Add("email");

src/AspNet.Security.OAuth.Deezer/DeezerAuthenticationHandler.cs

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
9898
["perms"] = scopes,
9999
};
100100

101+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
102+
{
103+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
104+
{
105+
parameters[parameter.Key] = parameter.Value;
106+
}
107+
}
108+
101109
if (Options.UsePkce)
102110
{
103111
var bytes = RandomNumberGenerator.GetBytes(256 / 8);

src/AspNet.Security.OAuth.Line/LineAuthenticationHandler.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
3535
{
3636
var tokenRequestParameters = new Dictionary<string, string>
3737
{
38-
["grant_type"] = "authorization_code",
3938
["code"] = context.Code,
4039
["redirect_uri"] = context.RedirectUri,
4140
["client_id"] = Options.ClientId,
4241
["client_secret"] = Options.ClientSecret,
4342
};
4443

44+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
45+
{
46+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
47+
{
48+
tokenRequestParameters[parameter.Key] = parameter.Value;
49+
}
50+
}
51+
4552
// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
4653
if (context.Properties.Items.TryGetValue(OAuthConstants.CodeVerifierKey, out var codeVerifier))
4754
{

src/AspNet.Security.OAuth.Line/LineAuthenticationOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public LineAuthenticationOptions()
2323
TokenEndpoint = LineAuthenticationDefaults.TokenEndpoint;
2424
UserInformationEndpoint = LineAuthenticationDefaults.UserInformationEndpoint;
2525

26+
AdditionalAuthorizationParameters["grant_type"] = "authorization_code";
27+
2628
Scope.Add("profile");
2729
Scope.Add("openid");
2830

src/AspNet.Security.OAuth.Mixcloud/MixcloudAuthenticationHandler.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
3535
{
3636
["client_id"] = Options.ClientId,
3737
["scope"] = scope,
38-
["response_type"] = "code",
3938
};
4039

40+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
41+
{
42+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
43+
{
44+
parameters[parameter.Key] = parameter.Value;
45+
}
46+
}
47+
4148
if (Options.UsePkce)
4249
{
4350
var bytes = RandomNumberGenerator.GetBytes(256 / 8);

src/AspNet.Security.OAuth.Mixcloud/MixcloudAuthenticationOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public MixcloudAuthenticationOptions()
2323
TokenEndpoint = MixcloudAuthenticationDefaults.TokenEndpoint;
2424
UserInformationEndpoint = MixcloudAuthenticationDefaults.UserInformationEndpoint;
2525

26+
AdditionalAuthorizationParameters["response_type"] = "code";
27+
2628
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "key");
2729
ClaimActions.MapJsonKey(ClaimTypes.Name, "username");
2830
ClaimActions.MapJsonKey(Claims.FullName, "name");

src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs

-9
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
5858
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
5959
}
6060

61-
protected override string BuildChallengeUrl([NotNull] AuthenticationProperties properties, [NotNull] string redirectUri)
62-
{
63-
var challengeUrl = base.BuildChallengeUrl(properties, redirectUri);
64-
65-
// Add duration=permanent to the authorization request to get an access token that doesn't expire after 1 hour.
66-
// See https://github.com/reddit/reddit/wiki/OAuth2#authorization for more information.
67-
return QueryHelpers.AddQueryString(challengeUrl, "duration", "permanent");
68-
}
69-
7061
/// <inheritdoc />
7162
protected override string FormatScope([NotNull] IEnumerable<string> scopes)
7263
{

src/AspNet.Security.OAuth.Reddit/RedditAuthenticationOptions.cs

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public RedditAuthenticationOptions()
2424
TokenEndpoint = RedditAuthenticationDefaults.TokenEndpoint;
2525
UserInformationEndpoint = RedditAuthenticationDefaults.UserInformationEndpoint;
2626

27+
// Add duration=permanent to the authorization request to get an access token that doesn't expire after 1 hour.
28+
// See https://github.com/reddit/reddit/wiki/OAuth2#authorization for more information.
29+
AdditionalAuthorizationParameters["duration"] = "permanent";
30+
2731
Scope.Add("identity");
2832

2933
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");

src/AspNet.Security.OAuth.Shopify/ShopifyAuthenticationHandler.cs

+8
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
116116
["redirect_uri"] = redirectUri,
117117
};
118118

119+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
120+
{
121+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
122+
{
123+
parameters[parameter.Key] = parameter.Value;
124+
}
125+
}
126+
119127
if (Options.UsePkce)
120128
{
121129
var bytes = RandomNumberGenerator.GetBytes(256 / 8);

src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationHandler.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,16 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
131131
{
132132
["appid"] = Options.ClientId,
133133
["scope"] = scope,
134-
["response_type"] = "code",
135134
};
136135

136+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
137+
{
138+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
139+
{
140+
parameters[parameter.Key] = parameter.Value;
141+
}
142+
}
143+
137144
if (Options.UsePkce)
138145
{
139146
var bytes = RandomNumberGenerator.GetBytes(256 / 8);

src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public WeixinAuthenticationOptions()
2323
TokenEndpoint = WeixinAuthenticationDefaults.TokenEndpoint;
2424
UserInformationEndpoint = WeixinAuthenticationDefaults.UserInformationEndpoint;
2525

26+
AdditionalAuthorizationParameters["response_type"] = "code";
27+
2628
Scope.Add("snsapi_login");
2729
Scope.Add("snsapi_userinfo");
2830

src/AspNet.Security.OAuth.WorkWeixin/WorkWeixinAuthenticationHandler.cs

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
110110
["redirect_uri"] = redirectUri,
111111
};
112112

113+
if (Options.AdditionalAuthorizationParameters?.Count > 0)
114+
{
115+
foreach (var parameter in Options.AdditionalAuthorizationParameters)
116+
{
117+
parameters[parameter.Key] = parameter.Value;
118+
}
119+
}
120+
113121
if (Options.UsePkce)
114122
{
115123
var bytes = RandomNumberGenerator.GetBytes(256 / 8);

0 commit comments

Comments
 (0)