Skip to content

Commit

Permalink
fix(provider): proper check of protection property (#1694)
Browse files Browse the repository at this point in the history
* fix(provider): proper check of protection property

* chore: add comment
  • Loading branch information
balazsorban44 authored Apr 11, 2021
1 parent d1dbfe1 commit 3dedf6c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ async function NextAuthHandler (req, res, userOptions) {
const providers = parseProviders({ providers: userOptions.providers, baseUrl, basePath })
const provider = providers.find(({ id }) => id === providerId)

if (provider &&
provider.type === 'oauth' && provider.version?.startsWith('2') &&
(!provider.protection && provider.state !== false)
) {
provider.protection = 'state' // Default to state, as we did in 3.1 REVIEW: should we use "pkce" or "none" as default?
}

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

const maxAge = 30 * 24 * 60 * 60 // Sessions expire after 30 days of being idle
Expand Down
5 changes: 3 additions & 2 deletions src/server/lib/oauth/pkce-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
export async function handleCallback (req, res) {
const { cookies, provider, baseUrl, basePath } = req.options
try {
if (!provider.protection.includes('pkce')) { // Provider does not support PKCE, nothing to do.
// Provider does not support PKCE, nothing to do.
if (!provider.protection?.includes('pkce')) {
return
}

Expand Down Expand Up @@ -50,7 +51,7 @@ export async function handleCallback (req, res) {
export async function handleSignin (req, res) {
const { cookies, provider, baseUrl, basePath } = req.options
try {
if (!provider.protection.includes('pkce')) { // Provider does not support PKCE, nothing to do.
if (!provider.protection?.includes('pkce')) { // Provider does not support PKCE, nothing to do.
return
}
// Started login flow, add generated pkce to req.options and (encrypted) code_verifier to a cookie
Expand Down
5 changes: 3 additions & 2 deletions src/server/lib/oauth/state-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { OAuthCallbackError } from '../../../lib/errors'
export async function handleCallback (req, res) {
const { csrfToken, provider, baseUrl, basePath } = req.options
try {
if (!provider.protection.includes('state')) { // Provider does not support state, nothing to do.
// Provider does not support state, nothing to do.
if (!provider.protection?.includes('state')) {
return
}

Expand Down Expand Up @@ -41,7 +42,7 @@ export async function handleCallback (req, res) {
export async function handleSignin (req, res) {
const { provider, baseUrl, basePath, csrfToken } = req.options
try {
if (!provider.protection.includes('state')) { // Provider does not support state, nothing to do.
if (!provider.protection?.includes('state')) { // Provider does not support state, nothing to do.
return
}

Expand Down

1 comment on commit 3dedf6c

@vercel
Copy link

@vercel vercel bot commented on 3dedf6c Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.