-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add nonce check type * Update types import for nonce-handler.ts * Update packages/next-auth/src/core/lib/oauth/callback.ts Co-authored-by: Thang Vu <thvu@hey.com> * Add further info to debug msg as per PR suggestion * Cast OauthChecks as OpenIDCallbackChecks * Update order of imports as per PR suggestion Co-authored-by: Hamid Adelyar <hamid.adelyar@bjss.com> Co-authored-by: hamidbjss <98807568+hamidbjss@users.noreply.github.com> Co-authored-by: Thang Vu <thvu@hey.com>
- Loading branch information
1 parent
32f4d50
commit d349ae2
Showing
7 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as jwt from "../../../jwt" | ||
import { generators } from "openid-client" | ||
import type { InternalOptions } from "../../types" | ||
import type { Cookie } from "../cookie" | ||
|
||
const NONCE_MAX_AGE = 60 * 15 // 15 minutes in seconds | ||
|
||
/** | ||
* Returns nonce if the provider supports it | ||
* and saves it in a cookie */ | ||
export async function createNonce(options: InternalOptions<"oauth">): Promise< | ||
| undefined | ||
| { | ||
value: string | ||
cookie: Cookie | ||
} | ||
> { | ||
const { cookies, logger, provider } = options | ||
if (!provider.checks?.includes("nonce")) { | ||
// Provider does not support nonce, return nothing. | ||
return | ||
} | ||
|
||
const nonce = generators.nonce() | ||
|
||
const expires = new Date() | ||
expires.setTime(expires.getTime() + NONCE_MAX_AGE * 1000) | ||
|
||
// Encrypt nonce and save it to an encrypted cookie | ||
const encryptedNonce = await jwt.encode({ | ||
...options.jwt, | ||
maxAge: NONCE_MAX_AGE, | ||
token: { nonce }, | ||
}) | ||
|
||
logger.debug("CREATE_ENCRYPTED_NONCE", { | ||
nonce, | ||
maxAge: NONCE_MAX_AGE, | ||
}) | ||
|
||
return { | ||
cookie: { | ||
name: cookies.nonce.name, | ||
value: encryptedNonce, | ||
options: { ...cookies.nonce.options, expires }, | ||
}, | ||
value: nonce, | ||
} | ||
} | ||
|
||
/** | ||
* Returns nonce from if the provider supports nonce, | ||
* and clears the container cookie afterwards. | ||
*/ | ||
export async function useNonce( | ||
nonce: string | undefined, | ||
options: InternalOptions<"oauth"> | ||
): Promise<{ value: string; cookie: Cookie } | undefined> { | ||
const { cookies, provider } = options | ||
|
||
if (!provider?.checks?.includes("nonce") || !nonce) { | ||
return | ||
} | ||
|
||
const value = (await jwt.decode({...options.jwt, token: nonce })) as any | ||
|
||
return { | ||
value: value?.nonce ?? undefined, | ||
cookie: { | ||
name: cookies.nonce.name, | ||
value: "", | ||
options: { ...cookies.nonce.options, maxAge: 0 }, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d349ae2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-auth – ./
next-auth-git-main-nextauthjs.vercel.app
next-auth-nextauthjs.vercel.app
next-auth.js.org
www.next-auth.js.org
next-auth-phi-two.vercel.app