-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(auth-js): use `env` in `hono/adapter` and add tests * add changeset * polyfill crypto
- Loading branch information
Showing
5 changed files
with
138 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hono/auth-js': patch | ||
--- | ||
|
||
fix: use `env` in `hono/adapter` and add tests |
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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import { webcrypto } from 'node:crypto' | ||
import { skipCSRFCheck } from '@auth/core' | ||
import type { Adapter } from '@auth/core/adapters' | ||
import Credentials from '@auth/core/providers/credentials' | ||
import { Hono } from 'hono' | ||
import { describe, expect, it } from 'vitest' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import type { AuthConfig } from '../src' | ||
import { authHandler, verifyAuth, initAuthConfig, reqWithEnvUrl } from '../src' | ||
|
||
// @ts-expect-error - global crypto | ||
//needed for node 18 and below but should work in node 20 and above | ||
global.crypto = webcrypto | ||
|
||
describe('Auth.js Adapter Middleware', () => { | ||
describe('Config', () => { | ||
it('Should return 500 if AUTH_SECRET is missing', async () => { | ||
globalThis.process.env = { AUTH_SECRET: '' } | ||
const app = new Hono() | ||
|
||
app.use('/*', (c, next) => { | ||
|
@@ -32,14 +37,10 @@ describe('Auth.js Adapter Middleware', () => { | |
expect(await res.text()).toBe('Missing AUTH_SECRET') | ||
}) | ||
|
||
it('Should return 200 auth initial config is correct', async () => { | ||
it('Should return 200 auth initial config is correct', async () => { | ||
globalThis.process.env = { AUTH_SECRET: 'secret' } | ||
const app = new Hono() | ||
|
||
app.use('/*', (c, next) => { | ||
c.env = { AUTH_SECRET: 'secret' } | ||
return next() | ||
}) | ||
|
||
app.use( | ||
'/*', | ||
initAuthConfig(() => { | ||
|
@@ -90,3 +91,107 @@ describe('reqWithEnvUrl()', () => { | |
expect(newReq.url.toString()).toBe('https://auth-url-base/request-path') | ||
}) | ||
}) | ||
|
||
describe('Credentials Provider', () => { | ||
const mockAdapter: Adapter = { | ||
createVerificationToken: vi.fn(), | ||
useVerificationToken: vi.fn(), | ||
getUserByEmail: vi.fn(), | ||
createUser: vi.fn(), | ||
getUser: vi.fn(), | ||
getUserByAccount: vi.fn(), | ||
updateUser: vi.fn(), | ||
linkAccount: vi.fn(), | ||
createSession: vi.fn(), | ||
getSessionAndUser: vi.fn(), | ||
updateSession: vi.fn(), | ||
deleteSession: vi.fn(), | ||
} | ||
|
||
globalThis.process.env = { | ||
AUTH_SECRET: 'secret', | ||
} | ||
|
||
const user = { email: '[email protected]', name: 'Hono' } | ||
|
||
const app = new Hono() | ||
|
||
app.use('*', initAuthConfig(getAuthConfig)) | ||
|
||
app.use('/api/auth/*', authHandler()) | ||
|
||
app.use('/api/*', verifyAuth()) | ||
|
||
app.get('/api/protected', (c) => { | ||
const auth = c.get('authUser') | ||
return c.json(auth) | ||
}) | ||
|
||
const credentials = Credentials({ | ||
credentials: { | ||
password: {}, | ||
}, | ||
authorize: (credentials) => { | ||
if (credentials.password === 'password') { | ||
return user | ||
} | ||
return null | ||
}, | ||
}) | ||
|
||
function getAuthConfig(): AuthConfig { | ||
return { | ||
secret: 'secret', | ||
providers: [credentials], | ||
adapter: mockAdapter, | ||
skipCSRFCheck, | ||
callbacks: { | ||
jwt: ({ token, user }) => { | ||
if (user) { | ||
token.id = user.id | ||
} | ||
return token | ||
}, | ||
}, | ||
session: { | ||
strategy: 'jwt', | ||
}, | ||
} | ||
} | ||
|
||
let cookie = [''] | ||
|
||
it('Should not authorize and return 302 - /api/auth/callback/credentials', async () => { | ||
const res = await app.request('/api/auth/callback/credentials', { | ||
method: 'POST', | ||
}) | ||
expect(res.status).toBe(302) | ||
expect(res.headers.get('location')).toBe( | ||
'http://localhost/api/auth/signin?error=CredentialsSignin&code=credentials' | ||
) | ||
}) | ||
|
||
it('Should authorize and return 302 - /api/auth/callback/credentials', async () => { | ||
const res = await app.request('http://localhost/api/auth/callback/credentials', { | ||
method: 'POST', | ||
body: new URLSearchParams({ | ||
password: 'password', | ||
}), | ||
}) | ||
expect(res.status).toBe(302) | ||
expect(res.headers.get('location')).toBe('http://localhost') | ||
cookie = res.headers.getSetCookie() | ||
}) | ||
|
||
it('Should authorize and return 200 - /api/protected', async () => { | ||
const headers = new Headers() | ||
headers.append('cookie', cookie[1]) | ||
const res = await app.request('http://localhost/api/protected', { | ||
headers, | ||
}) | ||
expect(res.status).toBe(200) | ||
const obj = await res.json() | ||
expect(obj['token']['name']).toBe(user.name) | ||
expect(obj['token']['email']).toBe(user.email) | ||
}) | ||
}) |
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