Skip to content

Commit

Permalink
Merge pull request #97 from aiji42/feat-auth0-auto-add-origin
Browse files Browse the repository at this point in the history
Feat auth0 auto add origin
  • Loading branch information
aiji42 authored Nov 9, 2021
2 parents 9eecbd8 + faae7c7 commit a418185
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 304 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const middleware = async (req: NextRequest) => {
// type makeAuth0Inspector = (fallback: Fallback, apiEndpoint: string) => AsyncMiddleware;
return makeAuth0Inspector(
{ type: 'redirect', destination: '/singin' },
`${req.nextUrl.origin}/api/auth/me` // api endpoint for auth0 profile
'/api/auth/me' // api endpoint for auth0 profile
)(req)
}
```
Expand Down
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"@geist-ui/react": "^2.2.0",
"@geist-ui/react-icons": "^1.0.1",
"aws-amplify": "^4.3.4",
"firebase": "^9.3.0",
"firebase": "^9.4.0",
"js-cookie": "^3.0.1",
"next": "12.0.3",
"next-fortress": "^3.0.0",
"next-fortress": "^3.1.0-beta.1",
"react": "^17.0.2",
"react-dom": "17.0.2"
},
Expand Down
2 changes: 1 addition & 1 deletion example/src/pages/auth0/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const middleware = async (req: NextRequest) => {

return makeAuth0Inspector(
{ type: 'redirect', destination: '/auth0' },
req.nextUrl.origin + '/api/auth/me'
'/api/auth/me'
)(req)
}
448 changes: 224 additions & 224 deletions example/yarn.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"@commitlint/config-conventional": "^14.1.0",
"@types/jest": "^27.0.2",
"@types/netmask": "^1.0.30",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.3.0",
"@typescript-eslint/parser": "^5.3.0",
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
"babel-jest": "^27.3.1",
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.3.0",
Expand All @@ -60,7 +60,7 @@
"typescript": "^4.4.4"
},
"dependencies": {
"jose": "^4.1.5",
"jose": "^4.2.0",
"netmask": "^2.0.2"
}
}
71 changes: 51 additions & 20 deletions src/__tests__/auth0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ fetchMock
status: 200
})
.get('/api/auth/failed/me', {
status: 403
status: 401
})
.get('https://authed.com/api/auth/me', {
status: 200
})
.get('https://not.authed.com/api/auth/me', {
status: 401
})

const fallback: Fallback = { type: 'redirect', destination: '/foo' }
Expand All @@ -27,30 +33,55 @@ describe('makeAuth0Inspector', () => {
jest.resetAllMocks()
})

test('not logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/failed/me'
)({ headers } as unknown as NextRequest)
describe('dose not have nextUrl origin', () => {
test('not logged in', async () => {
const req = { headers, nextUrl: { origin: '' } } as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/failed/me')(req)

expect(handleFallback).toBeCalledWith(fallback, { headers }, undefined)
})
expect(handleFallback).toBeCalledWith(fallback, req, undefined)
})

test('does not have cookie', async () => {
const noCookieReq = {
headers: { get: () => undefined }
} as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/failed/me')(noCookieReq)
test('does not have cookie', async () => {
const noCookieReq = {
headers: { get: () => undefined },
nextUrl: { origin: '' }
} as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/failed/me')(noCookieReq)

expect(handleFallback).toBeCalledWith(fallback, noCookieReq, undefined)
expect(handleFallback).toBeCalledWith(fallback, noCookieReq, undefined)
})

test('logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me'
)({ headers, nextUrl: { origin: '' } } as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
})
})

test('logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me'
)({ headers } as unknown as NextRequest)
describe('has nextUrl origin', () => {
test('not logged in', async () => {
const req = {
headers,
nextUrl: { origin: 'https://not.authed.com' }
} as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/me')(req)

expect(handleFallback).toBeCalledWith(fallback, req, undefined)
})

test('logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me'
)({
headers,
nextUrl: { origin: 'https://authed.com' }
} as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
expect(handleFallback).not.toBeCalled()
})
})
})
9 changes: 6 additions & 3 deletions src/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ const verifyAuth0Session = async (
req: NextRequest,
apiEndpoint: string
): Promise<boolean> => {
const res = await fetch(apiEndpoint, {
headers: { cookie: req.headers.get('cookie') ?? '' }
})
const res = await fetch(
(/^\//.test(apiEndpoint) ? req.nextUrl.origin : '') + apiEndpoint,
{
headers: { cookie: req.headers.get('cookie') ?? '' }
}
)

return res.ok
}
98 changes: 49 additions & 49 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2191,10 +2191,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.1.tgz#9fad171a5b701613ee8a6f4ece3c88b1034b1b03"
integrity sha512-UW7cbLqf/Wu5XH2RKKY1cHwUNLicIDRLMraYKz+HHAerJ0ZffUEk+fMnd8qU2JaS6cAy0r8tsaf7yqHASf/Y0Q==

"@types/node@^16.11.6":
version "16.11.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.6.tgz#6bef7a2a0ad684cf6e90fcfe31cecabd9ce0a3ae"
integrity sha512-ua7PgUoeQFjmWPcoo9khiPum3Pd60k4/2ZGXt18sm2Slk0W0xZTqt5Y0Ny1NyBiN1EVQ/+FaF9NcY4Qe6rwk5w==
"@types/node@^16.11.7":
version "16.11.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.7.tgz#36820945061326978c42a01e56b61cd223dfdc42"
integrity sha512-QB5D2sqfSjCmTuWcBWyJ+/44bcjO7VbjSbOE0ucoVbAsSNQc4Lt6QkgkVXkTDwkL4z/beecZNDvVX15D4P8Jbw==

"@types/normalize-package-data@^2.4.0":
version "2.4.1"
Expand Down Expand Up @@ -2233,74 +2233,74 @@
dependencies:
"@types/yargs-parser" "*"

"@typescript-eslint/eslint-plugin@^5.3.0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.3.0.tgz#a55ae72d28ffeb6badd817fe4566c9cced1f5e29"
integrity sha512-ARUEJHJrq85aaiCqez7SANeahDsJTD3AEua34EoQN9pHS6S5Bq9emcIaGGySt/4X2zSi+vF5hAH52sEen7IO7g==
"@typescript-eslint/eslint-plugin@^5.3.1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.3.1.tgz#d8ff412f10f54f6364e7fd7c1e70eb6767f434c3"
integrity sha512-cFImaoIr5Ojj358xI/SDhjog57OK2NqlpxwdcgyxDA3bJlZcJq5CPzUXtpD7CxI2Hm6ATU7w5fQnnkVnmwpHqw==
dependencies:
"@typescript-eslint/experimental-utils" "5.3.0"
"@typescript-eslint/scope-manager" "5.3.0"
"@typescript-eslint/experimental-utils" "5.3.1"
"@typescript-eslint/scope-manager" "5.3.1"
debug "^4.3.2"
functional-red-black-tree "^1.0.1"
ignore "^5.1.8"
regexpp "^3.2.0"
semver "^7.3.5"
tsutils "^3.21.0"

"@typescript-eslint/[email protected].0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-5.3.0.tgz#ee56b4957547ed2b0fc7451205e41502e664f546"
integrity sha512-NFVxYTjKj69qB0FM+piah1x3G/63WB8vCBMnlnEHUsiLzXSTWb9FmFn36FD9Zb4APKBLY3xRArOGSMQkuzTF1w==
"@typescript-eslint/[email protected].1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-5.3.1.tgz#bbd8f9b67b4d5fdcb9d2f90297d8fcda22561e05"
integrity sha512-RgFn5asjZ5daUhbK5Sp0peq0SSMytqcrkNfU4pnDma2D8P3ElZ6JbYjY8IMSFfZAJ0f3x3tnO3vXHweYg0g59w==
dependencies:
"@types/json-schema" "^7.0.9"
"@typescript-eslint/scope-manager" "5.3.0"
"@typescript-eslint/types" "5.3.0"
"@typescript-eslint/typescript-estree" "5.3.0"
"@typescript-eslint/scope-manager" "5.3.1"
"@typescript-eslint/types" "5.3.1"
"@typescript-eslint/typescript-estree" "5.3.1"
eslint-scope "^5.1.1"
eslint-utils "^3.0.0"

"@typescript-eslint/parser@^5.3.0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.3.0.tgz#7879f15e26d370ed3f653fb7dd06479531ed3ab9"
integrity sha512-rKu/yAReip7ovx8UwOAszJVO5MgBquo8WjIQcp1gx4pYQCwYzag+I5nVNHO4MqyMkAo0gWt2gWUi+36gWAVKcw==
"@typescript-eslint/parser@^5.3.1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.3.1.tgz#8ff1977c3d3200c217b3e4628d43ef92f89e5261"
integrity sha512-TD+ONlx5c+Qhk21x9gsJAMRohWAUMavSOmJgv3JGy9dgPhuBd5Wok0lmMClZDyJNLLZK1JRKiATzCKZNUmoyfw==
dependencies:
"@typescript-eslint/scope-manager" "5.3.0"
"@typescript-eslint/types" "5.3.0"
"@typescript-eslint/typescript-estree" "5.3.0"
"@typescript-eslint/scope-manager" "5.3.1"
"@typescript-eslint/types" "5.3.1"
"@typescript-eslint/typescript-estree" "5.3.1"
debug "^4.3.2"

"@typescript-eslint/[email protected].0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.3.0.tgz#97d0ccc7c9158e89e202d5e24ce6ba49052d432e"
integrity sha512-22Uic9oRlTsPppy5Tcwfj+QET5RWEnZ5414Prby465XxQrQFZ6nnm5KnXgnsAJefG4hEgMnaxTB3kNEyjdjj6A==
"@typescript-eslint/[email protected].1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.3.1.tgz#3cfbfbcf5488fb2a9a6fbbe97963ee1e8d419269"
integrity sha512-XksFVBgAq0Y9H40BDbuPOTUIp7dn4u8oOuhcgGq7EoDP50eqcafkMVGrypyVGvDYHzjhdUCUwuwVUK4JhkMAMg==
dependencies:
"@typescript-eslint/types" "5.3.0"
"@typescript-eslint/visitor-keys" "5.3.0"
"@typescript-eslint/types" "5.3.1"
"@typescript-eslint/visitor-keys" "5.3.1"

"@typescript-eslint/[email protected].0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.3.0.tgz#af29fd53867c2df0028c57c36a655bd7e9e05416"
integrity sha512-fce5pG41/w8O6ahQEhXmMV+xuh4+GayzqEogN24EK+vECA3I6pUwKuLi5QbXO721EMitpQne5VKXofPonYlAQg==
"@typescript-eslint/[email protected].1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.3.1.tgz#afaa715b69ebfcfde3af8b0403bf27527912f9b7"
integrity sha512-bG7HeBLolxKHtdHG54Uac750eXuQQPpdJfCYuw4ZI3bZ7+GgKClMWM8jExBtp7NSP4m8PmLRM8+lhzkYnSmSxQ==

"@typescript-eslint/[email protected].0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.3.0.tgz#4f68ddd46dc2983182402d2ab21fb44ad94988cf"
integrity sha512-FJ0nqcaUOpn/6Z4Jwbtf+o0valjBLkqc3MWkMvrhA2TvzFXtcclIM8F4MBEmYa2kgcI8EZeSAzwoSrIC8JYkug==
"@typescript-eslint/[email protected].1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.3.1.tgz#50cc4bfb93dc31bc75e08ae52e29fcb786d606ec"
integrity sha512-PwFbh/PKDVo/Wct6N3w+E4rLZxUDgsoII/GrWM2A62ETOzJd4M6s0Mu7w4CWsZraTbaC5UQI+dLeyOIFF1PquQ==
dependencies:
"@typescript-eslint/types" "5.3.0"
"@typescript-eslint/visitor-keys" "5.3.0"
"@typescript-eslint/types" "5.3.1"
"@typescript-eslint/visitor-keys" "5.3.1"
debug "^4.3.2"
globby "^11.0.4"
is-glob "^4.0.3"
semver "^7.3.5"
tsutils "^3.21.0"

"@typescript-eslint/[email protected].0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.3.0.tgz#a6258790f3b7b2547f70ed8d4a1e0c3499994523"
integrity sha512-oVIAfIQuq0x2TFDNLVavUn548WL+7hdhxYn+9j3YdJJXB7mH9dAmZNJsPDa7Jc+B9WGqoiex7GUDbyMxV0a/aw==
"@typescript-eslint/[email protected].1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.3.1.tgz#c2860ff22939352db4f3806f34b21d8ad00588ba"
integrity sha512-3cHUzUuVTuNHx0Gjjt5pEHa87+lzyqOiHXy/Gz+SJOCW1mpw9xQHIIEwnKn+Thph1mgWyZ90nboOcSuZr/jTTQ==
dependencies:
"@typescript-eslint/types" "5.3.0"
"@typescript-eslint/types" "5.3.1"
eslint-visitor-keys "^3.0.0"

JSONStream@^1.0.4, JSONStream@^1.3.4, JSONStream@^1.3.5:
Expand Down Expand Up @@ -6840,10 +6840,10 @@ jest@^27.3.1:
import-local "^3.0.2"
jest-cli "^27.3.1"

jose@^4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/jose/-/jose-4.1.5.tgz#fc681f89b9fee618eebe579a493fed46db78d990"
integrity sha512-GGTo/8TUgKsLrohNRwFzkJ+dI/TzFNknPO4hPbzc3lUi01mDmqlrvKDTzHxT6ESZKBMWNfiffQ755T5SiUJuxQ==
jose@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/jose/-/jose-4.2.0.tgz#eb3dfe4926514a99f325ba604d32e41589394f6d"
integrity sha512-7nlU7qankWiES1WmZXAJl0jiGusoouXhjiGR12yc+0/SIDi+4uhEGzqcfONtDI7g66K4IyqA44botXGpi9EBWA==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
Expand Down

1 comment on commit a418185

@vercel
Copy link

@vercel vercel bot commented on a418185 Nov 9, 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.