|
| 1 | +import { test } from '@playwright/test'; |
| 2 | + |
| 3 | +import { appConfigs } from '../../presets'; |
| 4 | +import type { MachineAuthTestAdapter } from '../../testUtils/machineAuthHelpers'; |
| 5 | +import { |
| 6 | + registerApiKeyAuthTests, |
| 7 | + registerM2MAuthTests, |
| 8 | + registerOAuthAuthTests, |
| 9 | +} from '../../testUtils/machineAuthHelpers'; |
| 10 | + |
| 11 | +const adapter: MachineAuthTestAdapter = { |
| 12 | + baseConfig: appConfigs.nuxt.node, |
| 13 | + apiKey: { |
| 14 | + path: '/api/me', |
| 15 | + addRoutes: config => |
| 16 | + config |
| 17 | + .addFile( |
| 18 | + 'server/api/me.get.ts', |
| 19 | + () => ` |
| 20 | + export default eventHandler(event => { |
| 21 | + const { userId, tokenType } = event.context.auth({ acceptsToken: 'api_key' }); |
| 22 | +
|
| 23 | + if (!userId) { |
| 24 | + throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); |
| 25 | + } |
| 26 | +
|
| 27 | + return { userId, tokenType }; |
| 28 | + }); |
| 29 | + `, |
| 30 | + ) |
| 31 | + .addFile( |
| 32 | + 'server/api/me.post.ts', |
| 33 | + () => ` |
| 34 | + export default eventHandler(event => { |
| 35 | + const authObject = event.context.auth({ acceptsToken: ['api_key', 'session_token'] }); |
| 36 | +
|
| 37 | + if (!authObject.isAuthenticated) { |
| 38 | + throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); |
| 39 | + } |
| 40 | +
|
| 41 | + return { userId: authObject.userId, tokenType: authObject.tokenType }; |
| 42 | + }); |
| 43 | + `, |
| 44 | + ), |
| 45 | + }, |
| 46 | + m2m: { |
| 47 | + path: '/api/m2m', |
| 48 | + addRoutes: config => |
| 49 | + config.addFile( |
| 50 | + 'server/api/m2m.get.ts', |
| 51 | + () => ` |
| 52 | + export default eventHandler(event => { |
| 53 | + const { subject, tokenType, isAuthenticated } = event.context.auth({ acceptsToken: 'm2m_token' }); |
| 54 | +
|
| 55 | + if (!isAuthenticated) { |
| 56 | + throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); |
| 57 | + } |
| 58 | +
|
| 59 | + return { subject, tokenType }; |
| 60 | + }); |
| 61 | + `, |
| 62 | + ), |
| 63 | + }, |
| 64 | + oauth: { |
| 65 | + verifyPath: '/api/oauth-verify', |
| 66 | + callbackPath: '/api/oauth/callback', |
| 67 | + addRoutes: config => |
| 68 | + config |
| 69 | + .addFile( |
| 70 | + 'server/api/oauth-verify.get.ts', |
| 71 | + () => ` |
| 72 | + export default eventHandler(event => { |
| 73 | + const { userId, tokenType } = event.context.auth({ acceptsToken: 'oauth_token' }); |
| 74 | +
|
| 75 | + if (!userId) { |
| 76 | + throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }); |
| 77 | + } |
| 78 | +
|
| 79 | + return { userId, tokenType }; |
| 80 | + }); |
| 81 | + `, |
| 82 | + ) |
| 83 | + .addFile( |
| 84 | + 'server/api/oauth/callback.get.ts', |
| 85 | + () => ` |
| 86 | + export default eventHandler(() => { |
| 87 | + return { message: 'OAuth callback received' }; |
| 88 | + }); |
| 89 | + `, |
| 90 | + ), |
| 91 | + }, |
| 92 | +}; |
| 93 | + |
| 94 | +test.describe('Nuxt machine authentication @machine', () => { |
| 95 | + registerApiKeyAuthTests(adapter); |
| 96 | + registerM2MAuthTests(adapter); |
| 97 | + registerOAuthAuthTests(adapter); |
| 98 | +}); |
0 commit comments