Skip to content

Commit

Permalink
feat: accept various types of request
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekonyx committed Mar 24, 2024
1 parent bc574bb commit f045342
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/steam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
PROVIDER_NAME
} from './constants'

import type { NextApiRequest } from 'next'
import type { OAuthConfig, OAuthUserConfig } from 'next-auth/providers'
import type { NextRequest } from 'next/server'
import type { SteamProfile } from './types'

export interface SteamProviderOptions extends Partial<OAuthUserConfig<SteamProfile>> {
Expand All @@ -21,7 +23,10 @@ export interface SteamProviderOptions extends Partial<OAuthUserConfig<SteamProfi
clientSecret: string
}

export function Steam(req: Request, options: SteamProviderOptions): OAuthConfig<SteamProfile> {
export function Steam(
req: Request | NextRequest | NextApiRequest,
options: SteamProviderOptions
): OAuthConfig<SteamProfile> {
const callbackUrl = new URL(options.callbackUrl)

// https://example.com
Expand Down Expand Up @@ -64,6 +69,10 @@ export function Steam(req: Request, options: SteamProviderOptions): OAuthConfig<
},
token: {
async request() {
if (!req.url) {
throw new Error('No URL found in request object')
}

const identifier = await verifyAssertion(req, realm, returnTo)

if (!identifier) {
Expand Down Expand Up @@ -108,7 +117,7 @@ export function Steam(req: Request, options: SteamProviderOptions): OAuthConfig<
* Verifies an assertion and returns the claimed identifier if authenticated, otherwise null.
*/
async function verifyAssertion(
req: Request,
req: Request | NextRequest | NextApiRequest,
realm: string,
returnTo: string
): Promise<string | null> {
Expand All @@ -123,7 +132,7 @@ async function verifyAssertion(

// We need to create a new URL object to parse the query string
// req.url in next@14 is an absolute url, but not in next@13, so example.com used as a base url
const url = new URL(req.url, 'https://example.com')
const url = new URL(req.url!, 'https://example.com')
const query = Object.fromEntries(url.searchParams.entries())

if (query['openid.op_endpoint'] !== AUTHORIZATION_URL || query['openid.ns'] !== OPENID_CHECK.ns) {
Expand Down

0 comments on commit f045342

Please sign in to comment.