Skip to content

Commit ee033a6

Browse files
committed
fix(provider): proper check of protection property (nextauthjs#1694)
* fix(provider): proper check of protection property * chore: add comment
1 parent 9849176 commit ee033a6

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/server/index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ async function NextAuthHandler (req, res, userOptions) {
7272
const providers = parseProviders({ providers: userOptions.providers, baseUrl, basePath })
7373
const provider = providers.find(({ id }) => id === providerId)
7474

75-
if (provider &&
76-
provider.type === 'oauth' && provider.version?.startsWith('2') &&
77-
(!provider.protection && provider.state !== false)
78-
) {
79-
provider.protection = 'state' // Default to state, as we did in 3.1 REVIEW: should we use "pkce" or "none" as default?
80-
}
81-
82-
if (typeof provider?.protection === 'string') {
83-
provider.protection = [provider.protection]
75+
// Protection only works on OAuth 2.x providers
76+
if (provider?.type === 'oauth' && provider.version?.startsWith('2')) {
77+
// When provider.state is undefined, we still want this to pass
78+
if (!provider.protection && provider.state !== false) {
79+
// Default to state, as we did in 3.1 REVIEW: should we use "pkce" or "none" as default?
80+
provider.protection = ['state']
81+
} else if (typeof provider.protection === 'string') {
82+
provider.protection = [provider.protection]
83+
}
8484
}
8585

8686
const maxAge = 30 * 24 * 60 * 60 // Sessions expire after 30 days of being idle

src/server/lib/oauth/pkce-handler.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
1616
export async function handleCallback (req, res) {
1717
const { cookies, provider, baseUrl, basePath } = req.options
1818
try {
19-
if (!provider.protection.includes('pkce')) { // Provider does not support PKCE, nothing to do.
19+
// Provider does not support PKCE, nothing to do.
20+
if (!provider.protection?.includes('pkce')) {
2021
return
2122
}
2223

@@ -50,7 +51,7 @@ export async function handleCallback (req, res) {
5051
export async function handleSignin (req, res) {
5152
const { cookies, provider, baseUrl, basePath } = req.options
5253
try {
53-
if (!provider.protection.includes('pkce')) { // Provider does not support PKCE, nothing to do.
54+
if (!provider.protection?.includes('pkce')) { // Provider does not support PKCE, nothing to do.
5455
return
5556
}
5657
// Started login flow, add generated pkce to req.options and (encrypted) code_verifier to a cookie

src/server/lib/oauth/state-handler.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { OAuthCallbackError } from '../../../lib/errors'
1212
export async function handleCallback (req, res) {
1313
const { csrfToken, provider, baseUrl, basePath } = req.options
1414
try {
15-
if (!provider.protection.includes('state')) { // Provider does not support state, nothing to do.
15+
// Provider does not support state, nothing to do.
16+
if (!provider.protection?.includes('state')) {
1617
return
1718
}
1819

@@ -41,7 +42,7 @@ export async function handleCallback (req, res) {
4142
export async function handleSignin (req, res) {
4243
const { provider, baseUrl, basePath, csrfToken } = req.options
4344
try {
44-
if (!provider.protection.includes('state')) { // Provider does not support state, nothing to do.
45+
if (!provider.protection?.includes('state')) { // Provider does not support state, nothing to do.
4546
return
4647
}
4748

0 commit comments

Comments
 (0)