Skip to content

Commit fd218f2

Browse files
authored
feat: Add CORS support to oAuth, Express, Koa and generated application (#2744)
1 parent 9231525 commit fd218f2

File tree

13 files changed

+87
-23
lines changed

13 files changed

+87
-23
lines changed

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/authentication-oauth/src/strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
6868
}
6969

7070
async getAllowedOrigin(params?: Params) {
71-
const { redirect, origins } = this.authentication.configuration.oauth
71+
const { redirect, origins = this.app.get('origins') } = this.authentication.configuration.oauth
7272

7373
if (Array.isArray(origins)) {
7474
const referer = params?.headers?.referer || ''

packages/cli/src/app/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export const generate = (ctx: AppGeneratorArguments) =>
171171
}
172172

173173
if (framework === 'express') {
174-
dependencies.push('@feathersjs/express', 'compression', 'helmet')
174+
dependencies.push('@feathersjs/express', 'compression')
175175
}
176176

177177
return addVersions(dependencies, dependencyVersions)

packages/cli/src/app/templates/app.tpl.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const tsKoaApp = ({ transports }: AppGeneratorContext) =>
66
`import serveStatic from 'koa-static'
77
import { feathers } from '@feathersjs/feathers'
88
import configuration from '@feathersjs/configuration'
9-
import { koa, rest, bodyParser, errorHandler, parseAuthentication } from '@feathersjs/koa'
9+
import { koa, rest, bodyParser, errorHandler, parseAuthentication, cors } from '@feathersjs/koa'
1010
${transports.includes('websockets') ? "import socketio from '@feathersjs/socketio'" : ''}
1111
1212
import type { Application } from './declarations'
@@ -23,12 +23,21 @@ app.configure(configuration(configurationSchema))
2323
// Set up Koa middleware
2424
app.use(serveStatic(app.get('public')))
2525
app.use(errorHandler())
26+
app.use(cors())
2627
app.use(parseAuthentication())
2728
app.use(bodyParser())
2829
2930
// Configure services and transports
3031
app.configure(rest())
31-
${transports.includes('websockets') ? 'app.configure(socketio())' : ''}
32+
${
33+
transports.includes('websockets')
34+
? `app.configure(socketio({
35+
cors: {
36+
origin: app.get('origins')
37+
}
38+
}))`
39+
: ''
40+
}
3241
app.configure(services)
3342
app.configure(channels)
3443
@@ -52,11 +61,10 @@ export { app }
5261

5362
const tsExpressApp = ({ transports }: AppGeneratorContext) =>
5463
`import compress from 'compression'
55-
import helmet from 'helmet'
5664
5765
import { feathers } from '@feathersjs/feathers'
5866
import express, {
59-
rest, json, urlencoded,
67+
rest, json, urlencoded, cors,
6068
serveStatic, notFound, errorHandler
6169
} from '@feathersjs/express'
6270
import configuration from '@feathersjs/configuration'
@@ -72,7 +80,7 @@ const app: Application = express(feathers())
7280
7381
// Load app configuration
7482
app.configure(configuration(configurationSchema))
75-
app.use(helmet())
83+
app.use(cors())
7684
app.use(compress())
7785
app.use(json())
7886
app.use(urlencoded({ extended: true }))
@@ -81,7 +89,15 @@ app.use('/', serveStatic(app.get('public')))
8189
8290
// Configure services and real-time functionality
8391
app.configure(rest())
84-
${transports.includes('websockets') ? 'app.configure(socketio())' : ''}
92+
${
93+
transports.includes('websockets')
94+
? `app.configure(socketio({
95+
cors: {
96+
origin: app.get('origins')
97+
}
98+
}))`
99+
: ''
100+
}
85101
app.configure(services)
86102
app.configure(channels)
87103

packages/cli/src/app/templates/config.tpl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const defaultConfig = ({}: AppGeneratorContext) => ({
55
host: 'localhost',
66
port: 3030,
77
public: './public/',
8+
origins: ['http://localhost:3030'],
89
paginate: {
910
default: 10,
1011
max: 50

packages/cli/src/app/templates/configuration.tpl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export const configurationSchema = schema(
1818
port: { type: 'number' },
1919
public: { type: 'string' },
2020
authentication: authenticationSettingsSchema,
21+
origins: {
22+
type: 'array',
23+
items: {
24+
type: 'string'
25+
}
26+
},
2127
paginate: {
2228
type: 'object',
2329
additionalProperties: false,

packages/cli/src/authentication/templates/config.tpl.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,14 @@ export const generate = (ctx: AuthenticationGeneratorContext) =>
3131
const oauthStrategies = authStrategies.filter((name) => name !== 'local')
3232

3333
if (oauthStrategies.length) {
34-
authentication.oauth = oauthStrategies.reduce(
35-
(oauth, name) => {
36-
oauth[name] = {
37-
key: '<Client ID>',
38-
secret: '<Client secret>'
39-
}
34+
authentication.oauth = oauthStrategies.reduce((oauth, name) => {
35+
oauth[name] = {
36+
key: '<Client ID>',
37+
secret: '<Client secret>'
38+
}
4039

41-
return oauth
42-
},
43-
{
44-
redirect: '/'
45-
} as any
46-
)
40+
return oauth
41+
}, {} as any)
4742
}
4843

4944
return { authentication }

packages/express/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@feathersjs/transport-commons": "^5.0.0-pre.28",
6060
"@types/express": "^4.17.13",
6161
"@types/express-serve-static-core": "^4.17.30",
62+
"cors": "^2.8.5",
6263
"express": "^4.18.1"
6364
},
6465
"devDependencies": {

packages/express/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express, { Express } from 'express'
22
import { Application as FeathersApplication, defaultServiceMethods } from '@feathersjs/feathers'
33
import { routing } from '@feathersjs/transport-commons'
44
import { createDebug } from '@feathersjs/commons'
5+
import cors from 'cors'
56

67
import { rest, RestOptions, formatter } from './rest'
78
import { errorHandler, notFound, ErrorHandlerOptions } from './handlers'
@@ -28,7 +29,8 @@ export {
2829
ExpressOverrides,
2930
AuthenticationSettings,
3031
parseAuthentication,
31-
authenticate
32+
authenticate,
33+
cors
3234
}
3335

3436
const debug = createDebug('@feathersjs/express')

packages/express/test/rest.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ describe('@feathersjs/express/rest provider', () => {
8787

8888
before(async () => {
8989
app = expressify(feathers())
90+
.use(express.cors())
9091
.use(express.json())
9192
.configure(rest(express.formatter))
9293
.use('codes', {

0 commit comments

Comments
 (0)