From f962d29eda293d5b8cd3360bfd2b44d1b8c6e809 Mon Sep 17 00:00:00 2001 From: mani Date: Mon, 24 Feb 2025 01:01:35 +0200 Subject: [PATCH] Pass optional state as parameter, so we can link other state info to it --- .changeset/eighty-pandas-talk.md | 5 +++++ .../oauth-providers/src/providers/discord/discordAuth.ts | 3 ++- .../oauth-providers/src/providers/facebook/facebookAuth.ts | 3 ++- packages/oauth-providers/src/providers/github/githubAuth.ts | 3 ++- .../oauth-providers/src/providers/linkedin/linkedinAuth.ts | 3 ++- packages/oauth-providers/src/providers/x/xAuth.ts | 3 ++- 6 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 .changeset/eighty-pandas-talk.md diff --git a/.changeset/eighty-pandas-talk.md b/.changeset/eighty-pandas-talk.md new file mode 100644 index 00000000..e6c10803 --- /dev/null +++ b/.changeset/eighty-pandas-talk.md @@ -0,0 +1,5 @@ +--- +'@hono/oauth-providers': minor +--- + +Optionally pass state as an option to oauth provider diff --git a/packages/oauth-providers/src/providers/discord/discordAuth.ts b/packages/oauth-providers/src/providers/discord/discordAuth.ts index 4d4972a6..f2bea942 100644 --- a/packages/oauth-providers/src/providers/discord/discordAuth.ts +++ b/packages/oauth-providers/src/providers/discord/discordAuth.ts @@ -12,10 +12,11 @@ export function discordAuth(options: { client_id?: string client_secret?: string redirect_uri?: string + state?: string }): MiddlewareHandler { return async (c, next) => { // Generate encoded "keys" - const newState = getRandomState() + const newState = options.state || getRandomState() // Create new Auth instance const auth = new AuthFlow({ diff --git a/packages/oauth-providers/src/providers/facebook/facebookAuth.ts b/packages/oauth-providers/src/providers/facebook/facebookAuth.ts index 75865d7a..d89eeb6f 100644 --- a/packages/oauth-providers/src/providers/facebook/facebookAuth.ts +++ b/packages/oauth-providers/src/providers/facebook/facebookAuth.ts @@ -13,9 +13,10 @@ export function facebookAuth(options: { client_id?: string client_secret?: string redirect_uri?: string + state?: string }): MiddlewareHandler { return async (c, next) => { - const newState = getRandomState() + const newState = options.state || getRandomState() // Create new Auth instance const auth = new AuthFlow({ client_id: options.client_id || (env(c).FACEBOOK_ID as string), diff --git a/packages/oauth-providers/src/providers/github/githubAuth.ts b/packages/oauth-providers/src/providers/github/githubAuth.ts index 3e0875ab..df69ab9e 100644 --- a/packages/oauth-providers/src/providers/github/githubAuth.ts +++ b/packages/oauth-providers/src/providers/github/githubAuth.ts @@ -13,9 +13,10 @@ export function githubAuth(options: { scope?: GitHubScope[] oauthApp?: boolean redirect_uri?: string + state?: string }): MiddlewareHandler { return async (c, next) => { - const newState = getRandomState() + const newState = options.state || getRandomState() // Create new Auth instance const auth = new AuthFlow({ client_id: options.client_id || (env(c).GITHUB_ID as string), diff --git a/packages/oauth-providers/src/providers/linkedin/linkedinAuth.ts b/packages/oauth-providers/src/providers/linkedin/linkedinAuth.ts index e0ac131f..598e43f8 100644 --- a/packages/oauth-providers/src/providers/linkedin/linkedinAuth.ts +++ b/packages/oauth-providers/src/providers/linkedin/linkedinAuth.ts @@ -13,9 +13,10 @@ export function linkedinAuth(options: { scope?: LinkedInScope[] appAuth?: boolean redirect_uri?: string + state?: string }): MiddlewareHandler { return async (c, next) => { - const newState = getRandomState() + const newState = options.state || getRandomState() // Create new Auth instance const auth = new AuthFlow({ client_id: options.client_id || (env(c).LINKEDIN_ID as string), diff --git a/packages/oauth-providers/src/providers/x/xAuth.ts b/packages/oauth-providers/src/providers/x/xAuth.ts index 18381a35..6b9620f9 100644 --- a/packages/oauth-providers/src/providers/x/xAuth.ts +++ b/packages/oauth-providers/src/providers/x/xAuth.ts @@ -14,10 +14,11 @@ export function xAuth(options: { client_id?: string client_secret?: string redirect_uri?: string + state?: string }): MiddlewareHandler { return async (c, next) => { // Generate encoded "keys" - const newState = getRandomState() + const newState = options.state || getRandomState() const challenge = await getCodeChallenge() const auth = new AuthFlow({