@feathersjs/authentication-oauth 5.0.0-pre.22 → 5.0.0-pre.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/lib/express.js +15 -13
- package/lib/express.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/strategy.js +14 -13
- package/lib/strategy.js.map +1 -1
- package/lib/utils.js.map +1 -1
- package/package.json +12 -12
- package/src/express.ts +85 -87
- package/src/index.ts +53 -46
- package/src/strategy.ts +92 -85
- package/src/utils.ts +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.23](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.22...v5.0.0-pre.23) (2022-06-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @feathersjs/authentication-oauth
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [5.0.0-pre.22](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.21...v5.0.0-pre.22) (2022-05-24)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @feathersjs/authentication-oauth
|
package/lib/express.js
CHANGED
|
@@ -19,11 +19,12 @@ exports.default = (options) => {
|
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
21
|
const { prefix } = config.defaults;
|
|
22
|
-
const expressSession = options.expressSession ||
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
const expressSession = options.expressSession ||
|
|
23
|
+
(0, express_session_1.default)({
|
|
24
|
+
secret: Math.random().toString(36).substring(7),
|
|
25
|
+
saveUninitialized: true,
|
|
26
|
+
resave: true
|
|
27
|
+
});
|
|
27
28
|
const grantApp = grantInstance(config);
|
|
28
29
|
const authApp = (0, express_1.original)();
|
|
29
30
|
authApp.use(expressSession);
|
|
@@ -36,7 +37,7 @@ exports.default = (options) => {
|
|
|
36
37
|
req.session.redirect = redirect;
|
|
37
38
|
req.session.query = query;
|
|
38
39
|
req.session.headers = req.headers;
|
|
39
|
-
if (typeof
|
|
40
|
+
if (typeof req.session.save === 'function') {
|
|
40
41
|
req.session.save((err) => {
|
|
41
42
|
if (err) {
|
|
42
43
|
next(`Error storing session: ${err}`);
|
|
@@ -58,10 +59,12 @@ exports.default = (options) => {
|
|
|
58
59
|
const params = {
|
|
59
60
|
...req.feathers,
|
|
60
61
|
authStrategies: [name],
|
|
61
|
-
authentication: accessToken
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
authentication: accessToken
|
|
63
|
+
? {
|
|
64
|
+
strategy: linkStrategy,
|
|
65
|
+
accessToken
|
|
66
|
+
}
|
|
67
|
+
: null,
|
|
65
68
|
query,
|
|
66
69
|
redirect,
|
|
67
70
|
headers
|
|
@@ -85,15 +88,14 @@ exports.default = (options) => {
|
|
|
85
88
|
}
|
|
86
89
|
};
|
|
87
90
|
try {
|
|
88
|
-
const payload = config.defaults.transport === 'session' ?
|
|
89
|
-
grant.response : req.query;
|
|
91
|
+
const payload = config.defaults.transport === 'session' ? grant.response : req.query;
|
|
90
92
|
const authentication = {
|
|
91
93
|
strategy: name,
|
|
92
94
|
...payload
|
|
93
95
|
};
|
|
94
96
|
await new Promise((resolve, reject) => {
|
|
95
97
|
if (req.session.destroy) {
|
|
96
|
-
req.session.destroy((err) => err ? reject(err) : resolve());
|
|
98
|
+
req.session.destroy((err) => (err ? reject(err) : resolve()));
|
|
97
99
|
}
|
|
98
100
|
else {
|
|
99
101
|
req.session = null;
|
package/lib/express.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"express.js","sourceRoot":"","sources":["../src/express.ts"],"names":[],"mappings":";;;;;AAAA,
|
|
1
|
+
{"version":3,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;gBACH,CAAC,CAAC,CAAA;gBAEF,KAAK,CAAC,WAAW,WAAW,wCAAwC,IAAI,EAAE,CAAC,CAAA;gBAE3E,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;gBAE/D,KAAK,CAAC,mDAAmD,CAAC,CAAA;gBAE1D,MAAM,YAAY,CAAC,UAAU,CAAC,CAAA;aAC/B;YAAC,OAAO,KAAU,EAAE;gBACnB,KAAK,CAAC,qCAAqC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAA;gBACzD,MAAM,YAAY,CAAC,KAAK,CAAC,CAAA;aAC1B;QACH,CAAC,CAAC,CAAA;QAEF,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QAErB,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;QACjC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC1B,CAAC,CAAA;AACH,CAAC,CAAA"}
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,uEAA8C;AAC9C,uDAA8B;AAC9B,uDAA8B;AAC9B,iDAAiD;AAEjD,yCAAwD;AAM3B,8FANpB,wBAAa,OAMoB;AAL1C,wDAAmD;AACnD,mCAAgE;AAEhE,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,kCAAkC,CAAC,CAAA;AAItD,MAAM,KAAK,GAAG,CAAC,OAA2B,EAAE,EAAE,CAAC,CAAC,GAAgB,EAAE,EAAE;;IACzE,MAAM,OAAO,GAAG,GAAG,CAAC,qBAAqB,CAAC,CAAC,CAAC,GAAG,CAAC,qBAAqB,CAAC,OAAO,CAAC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AO,CAAC,CAAC,CAAA;AACtC,CAAC,CAAA;AAPU,QAAA,OAAO,WAOjB;AAEU,QAAA,YAAY,GAAG,eAAO,CAAA"}
|
package/lib/strategy.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.OAuthStrategy = void 0;
|
|
7
7
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
9
|
// @ts-ignore
|
|
9
10
|
const querystring_1 = __importDefault(require("querystring"));
|
|
10
11
|
const authentication_1 = require("@feathersjs/authentication");
|
|
@@ -45,8 +46,7 @@ class OAuthStrategy extends authentication_1.AuthenticationBaseStrategy {
|
|
|
45
46
|
if (authentication && authentication.strategy) {
|
|
46
47
|
debug('getCurrentEntity with authentication', authentication);
|
|
47
48
|
const { strategy } = authentication;
|
|
48
|
-
const authResult = await this.authentication
|
|
49
|
-
.authenticate(authentication, params, strategy);
|
|
49
|
+
const authResult = await this.authentication.authenticate(authentication, params, strategy);
|
|
50
50
|
return authResult[entity];
|
|
51
51
|
}
|
|
52
52
|
return null;
|
|
@@ -56,7 +56,7 @@ class OAuthStrategy extends authentication_1.AuthenticationBaseStrategy {
|
|
|
56
56
|
const { redirect, origins } = this.authentication.configuration.oauth;
|
|
57
57
|
if (Array.isArray(origins)) {
|
|
58
58
|
const referer = ((_a = params === null || params === void 0 ? void 0 : params.headers) === null || _a === void 0 ? void 0 : _a.referer) || '';
|
|
59
|
-
const allowedOrigin = origins.find(current => referer.toLowerCase().startsWith(current.toLowerCase()));
|
|
59
|
+
const allowedOrigin = origins.find((current) => referer.toLowerCase().startsWith(current.toLowerCase()));
|
|
60
60
|
if (!allowedOrigin) {
|
|
61
61
|
throw new errors_1.NotAuthenticated(`Referer "${referer || '[header not available]'}" not allowed.`);
|
|
62
62
|
}
|
|
@@ -71,14 +71,15 @@ class OAuthStrategy extends authentication_1.AuthenticationBaseStrategy {
|
|
|
71
71
|
return null;
|
|
72
72
|
}
|
|
73
73
|
const redirectUrl = `${redirect}${queryRedirect}`;
|
|
74
|
-
const separator = redirect.endsWith('?') ? '' :
|
|
75
|
-
(redirect.indexOf('#') !== -1 ? '?' : '#');
|
|
74
|
+
const separator = redirect.endsWith('?') ? '' : redirect.indexOf('#') !== -1 ? '?' : '#';
|
|
76
75
|
const authResult = data;
|
|
77
|
-
const query = authResult.accessToken
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
const query = authResult.accessToken
|
|
77
|
+
? {
|
|
78
|
+
access_token: authResult.accessToken
|
|
79
|
+
}
|
|
80
|
+
: {
|
|
81
|
+
error: data.message || 'OAuth Authentication not successful'
|
|
82
|
+
};
|
|
82
83
|
return `${redirectUrl}${separator}${querystring_1.default.stringify(query)}`;
|
|
83
84
|
}
|
|
84
85
|
async findEntity(profile, params) {
|
|
@@ -121,10 +122,10 @@ class OAuthStrategy extends authentication_1.AuthenticationBaseStrategy {
|
|
|
121
122
|
const entity = this.configuration.entity;
|
|
122
123
|
const { provider, ...params } = originalParams;
|
|
123
124
|
const profile = await this.getProfile(authentication, params);
|
|
124
|
-
const existingEntity = await this.findEntity(profile, params)
|
|
125
|
-
|| await this.getCurrentEntity(params);
|
|
125
|
+
const existingEntity = (await this.findEntity(profile, params)) || (await this.getCurrentEntity(params));
|
|
126
126
|
debug('authenticate with (existing) entity', existingEntity);
|
|
127
|
-
const authEntity = !existingEntity
|
|
127
|
+
const authEntity = !existingEntity
|
|
128
|
+
? await this.createEntity(profile, params)
|
|
128
129
|
: await this.updateEntity(existingEntity, profile, params);
|
|
129
130
|
return {
|
|
130
131
|
authentication: { strategy: this.name },
|
package/lib/strategy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../src/strategy.ts"],"names":[],"mappings":";;;;;;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KAAmC,EAAE,EAAE;IAC3F,MAAM,QAAQ,GAAuB;QACnC,YAAY,EAAE,KAAK;QACnB,GAAG,KAAK;KACT,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AASO,MAAM,kBAAkB,GAAG,CAAC,IAAiB,EAAE,KAAmC,EAAE,EAAE;IAC3F,MAAM,QAAQ,GAAuB;QACnC,YAAY,EAAE,KAAK;QACnB,GAAG,KAAK;KACT,CAAA;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA;AAPY,QAAA,kBAAkB,sBAO9B"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/authentication-oauth",
|
|
3
3
|
"description": "oAuth 1 and 2 authentication for Feathers. Powered by Grant.",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.23",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -53,27 +53,27 @@
|
|
|
53
53
|
"access": "public"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@feathersjs/authentication": "^5.0.0-pre.
|
|
57
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
58
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
59
|
-
"@feathersjs/express": "^5.0.0-pre.
|
|
60
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
61
|
-
"express-session": "^1.17.
|
|
56
|
+
"@feathersjs/authentication": "^5.0.0-pre.23",
|
|
57
|
+
"@feathersjs/commons": "^5.0.0-pre.23",
|
|
58
|
+
"@feathersjs/errors": "^5.0.0-pre.23",
|
|
59
|
+
"@feathersjs/express": "^5.0.0-pre.23",
|
|
60
|
+
"@feathersjs/feathers": "^5.0.0-pre.23",
|
|
61
|
+
"express-session": "^1.17.3",
|
|
62
62
|
"grant": "^5.4.21",
|
|
63
63
|
"lodash": "^4.17.21"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@feathersjs/memory": "^5.0.0-pre.
|
|
66
|
+
"@feathersjs/memory": "^5.0.0-pre.23",
|
|
67
67
|
"@types/express": "^4.17.13",
|
|
68
68
|
"@types/express-session": "^1.17.4",
|
|
69
69
|
"@types/lodash": "^4.14.182",
|
|
70
70
|
"@types/mocha": "^9.1.1",
|
|
71
|
-
"@types/node": "^17.0.
|
|
71
|
+
"@types/node": "^17.0.40",
|
|
72
72
|
"axios": "^0.27.2",
|
|
73
73
|
"mocha": "^10.0.0",
|
|
74
74
|
"shx": "^0.3.4",
|
|
75
|
-
"ts-node": "^10.
|
|
76
|
-
"typescript": "^4.
|
|
75
|
+
"ts-node": "^10.8.1",
|
|
76
|
+
"typescript": "^4.7.3"
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "a60910bd730b88053ca6648337095f1ca1e3b39f"
|
|
79
79
|
}
|
package/src/express.ts
CHANGED
|
@@ -1,142 +1,140 @@
|
|
|
1
|
-
import grant from 'grant'
|
|
2
|
-
import session from 'express-session'
|
|
3
|
-
import { Request, Response, NextFunction } from 'express'
|
|
4
|
-
import { createDebug } from '@feathersjs/commons'
|
|
5
|
-
import { Application } from '@feathersjs/feathers'
|
|
6
|
-
import { AuthenticationResult } from '@feathersjs/authentication'
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const grantInstance = grant.express();
|
|
15
|
-
const debug = createDebug('@feathersjs/authentication-oauth/express');
|
|
1
|
+
import grant from 'grant'
|
|
2
|
+
import session from 'express-session'
|
|
3
|
+
import { Request, Response, NextFunction } from 'express'
|
|
4
|
+
import { createDebug } from '@feathersjs/commons'
|
|
5
|
+
import { Application } from '@feathersjs/feathers'
|
|
6
|
+
import { AuthenticationResult } from '@feathersjs/authentication'
|
|
7
|
+
import { Application as ExpressApplication, original as originalExpress } from '@feathersjs/express'
|
|
8
|
+
import { OauthSetupSettings } from './utils'
|
|
9
|
+
import { OAuthStrategy } from './strategy'
|
|
10
|
+
|
|
11
|
+
const grantInstance = grant.express()
|
|
12
|
+
const debug = createDebug('@feathersjs/authentication-oauth/express')
|
|
16
13
|
|
|
17
14
|
declare module 'express-session' {
|
|
18
15
|
interface SessionData {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
redirect: string
|
|
17
|
+
accessToken: string
|
|
18
|
+
query: { [key: string]: any }
|
|
19
|
+
grant: { [key: string]: any }
|
|
20
|
+
headers: { [key: string]: any }
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
export default (options: OauthSetupSettings) => {
|
|
28
25
|
return (feathersApp: Application) => {
|
|
29
|
-
const { authService, linkStrategy } = options
|
|
30
|
-
const app = feathersApp as ExpressApplication
|
|
31
|
-
const config = app.get('grant')
|
|
26
|
+
const { authService, linkStrategy } = options
|
|
27
|
+
const app = feathersApp as ExpressApplication
|
|
28
|
+
const config = app.get('grant')
|
|
32
29
|
|
|
33
30
|
if (!config) {
|
|
34
|
-
debug('No grant configuration found, skipping Express oAuth setup')
|
|
35
|
-
return
|
|
31
|
+
debug('No grant configuration found, skipping Express oAuth setup')
|
|
32
|
+
return
|
|
36
33
|
}
|
|
37
34
|
|
|
38
|
-
const { prefix } = config.defaults
|
|
39
|
-
const expressSession =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
35
|
+
const { prefix } = config.defaults
|
|
36
|
+
const expressSession =
|
|
37
|
+
options.expressSession ||
|
|
38
|
+
session({
|
|
39
|
+
secret: Math.random().toString(36).substring(7),
|
|
40
|
+
saveUninitialized: true,
|
|
41
|
+
resave: true
|
|
42
|
+
})
|
|
43
|
+
const grantApp = grantInstance(config)
|
|
44
|
+
const authApp = originalExpress()
|
|
46
45
|
|
|
47
|
-
authApp.use(expressSession)
|
|
46
|
+
authApp.use(expressSession)
|
|
48
47
|
|
|
49
48
|
authApp.get('/:name', (req: Request, _res: Response, next: NextFunction) => {
|
|
50
|
-
const { feathers_token, redirect, ...query } = req.query
|
|
49
|
+
const { feathers_token, redirect, ...query } = req.query
|
|
51
50
|
|
|
52
51
|
if (feathers_token) {
|
|
53
|
-
debug('Got feathers_token query parameter to link accounts', feathers_token)
|
|
54
|
-
req.session.accessToken = feathers_token as string
|
|
52
|
+
debug('Got feathers_token query parameter to link accounts', feathers_token)
|
|
53
|
+
req.session.accessToken = feathers_token as string
|
|
55
54
|
}
|
|
56
|
-
req.session.redirect = redirect as string
|
|
57
|
-
req.session.query = query
|
|
58
|
-
req.session.headers = req.headers
|
|
59
|
-
if (typeof
|
|
55
|
+
req.session.redirect = redirect as string
|
|
56
|
+
req.session.query = query
|
|
57
|
+
req.session.headers = req.headers
|
|
58
|
+
if (typeof req.session.save === 'function') {
|
|
60
59
|
req.session.save((err: any) => {
|
|
61
60
|
if (err) {
|
|
62
|
-
next(`Error storing session: ${err}`)
|
|
61
|
+
next(`Error storing session: ${err}`)
|
|
63
62
|
} else {
|
|
64
|
-
next()
|
|
63
|
+
next()
|
|
65
64
|
}
|
|
66
|
-
})
|
|
65
|
+
})
|
|
66
|
+
} else {
|
|
67
|
+
next()
|
|
67
68
|
}
|
|
68
|
-
|
|
69
|
-
next();
|
|
70
|
-
}
|
|
71
|
-
});
|
|
69
|
+
})
|
|
72
70
|
|
|
73
71
|
authApp.get('/:name/authenticate', async (req: Request, res: Response, next: NextFunction) => {
|
|
74
|
-
const { name } = req.params
|
|
75
|
-
const { accessToken, grant, query = {}, redirect, headers } = req.session
|
|
76
|
-
const service = app.defaultAuthentication(authService)
|
|
77
|
-
const [
|
|
72
|
+
const { name } = req.params
|
|
73
|
+
const { accessToken, grant, query = {}, redirect, headers } = req.session
|
|
74
|
+
const service = app.defaultAuthentication(authService)
|
|
75
|
+
const [strategy] = service.getStrategies(name) as OAuthStrategy[]
|
|
78
76
|
const params = {
|
|
79
77
|
...req.feathers,
|
|
80
|
-
authStrategies: [
|
|
81
|
-
authentication: accessToken
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
78
|
+
authStrategies: [name],
|
|
79
|
+
authentication: accessToken
|
|
80
|
+
? {
|
|
81
|
+
strategy: linkStrategy,
|
|
82
|
+
accessToken
|
|
83
|
+
}
|
|
84
|
+
: null,
|
|
85
85
|
query,
|
|
86
86
|
redirect,
|
|
87
87
|
headers
|
|
88
|
-
}
|
|
89
|
-
const sendResponse = async (data: AuthenticationResult|Error) => {
|
|
88
|
+
}
|
|
89
|
+
const sendResponse = async (data: AuthenticationResult | Error) => {
|
|
90
90
|
try {
|
|
91
|
-
const redirect = await strategy.getRedirect(data, params)
|
|
91
|
+
const redirect = await strategy.getRedirect(data, params)
|
|
92
92
|
|
|
93
93
|
if (redirect !== null) {
|
|
94
|
-
res.redirect(redirect)
|
|
94
|
+
res.redirect(redirect)
|
|
95
95
|
} else if (data instanceof Error) {
|
|
96
|
-
throw data
|
|
96
|
+
throw data
|
|
97
97
|
} else {
|
|
98
|
-
res.json(data)
|
|
98
|
+
res.json(data)
|
|
99
99
|
}
|
|
100
100
|
} catch (error: any) {
|
|
101
|
-
debug('oAuth error', error)
|
|
102
|
-
next(error)
|
|
101
|
+
debug('oAuth error', error)
|
|
102
|
+
next(error)
|
|
103
103
|
}
|
|
104
|
-
}
|
|
104
|
+
}
|
|
105
105
|
|
|
106
106
|
try {
|
|
107
|
-
const payload = config.defaults.transport === 'session' ?
|
|
108
|
-
grant.response : req.query;
|
|
107
|
+
const payload = config.defaults.transport === 'session' ? grant.response : req.query
|
|
109
108
|
const authentication = {
|
|
110
109
|
strategy: name,
|
|
111
110
|
...payload
|
|
112
|
-
}
|
|
111
|
+
}
|
|
113
112
|
|
|
114
113
|
await new Promise<void>((resolve, reject) => {
|
|
115
114
|
if (req.session.destroy) {
|
|
116
|
-
req.session.destroy((err: any) => err ? reject(err) : resolve())
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
resolve();
|
|
115
|
+
req.session.destroy((err: any) => (err ? reject(err) : resolve()))
|
|
116
|
+
} else {
|
|
117
|
+
req.session = null
|
|
118
|
+
resolve()
|
|
121
119
|
}
|
|
122
|
-
})
|
|
120
|
+
})
|
|
123
121
|
|
|
124
|
-
debug(`Calling ${authService}.create authentication with strategy ${name}`)
|
|
122
|
+
debug(`Calling ${authService}.create authentication with strategy ${name}`)
|
|
125
123
|
|
|
126
|
-
const authResult = await service.create(authentication, params)
|
|
124
|
+
const authResult = await service.create(authentication, params)
|
|
127
125
|
|
|
128
|
-
debug('Successful oAuth authentication, sending response')
|
|
126
|
+
debug('Successful oAuth authentication, sending response')
|
|
129
127
|
|
|
130
|
-
await sendResponse(authResult)
|
|
128
|
+
await sendResponse(authResult)
|
|
131
129
|
} catch (error: any) {
|
|
132
|
-
debug('Received oAuth authentication error', error.stack)
|
|
133
|
-
await sendResponse(error)
|
|
130
|
+
debug('Received oAuth authentication error', error.stack)
|
|
131
|
+
await sendResponse(error)
|
|
134
132
|
}
|
|
135
|
-
})
|
|
133
|
+
})
|
|
136
134
|
|
|
137
|
-
authApp.use(grantApp)
|
|
135
|
+
authApp.use(grantApp)
|
|
138
136
|
|
|
139
|
-
app.set('grant', grantApp.config)
|
|
140
|
-
app.use(prefix, authApp)
|
|
141
|
-
}
|
|
142
|
-
}
|
|
137
|
+
app.set('grant', grantApp.config)
|
|
138
|
+
app.use(prefix, authApp)
|
|
139
|
+
}
|
|
140
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,80 +1,87 @@
|
|
|
1
|
-
import defaultsDeep from 'lodash/defaultsDeep'
|
|
2
|
-
import each from 'lodash/each'
|
|
3
|
-
import omit from 'lodash/omit'
|
|
4
|
-
import { createDebug } from '@feathersjs/commons'
|
|
5
|
-
import { Application } from '@feathersjs/feathers'
|
|
6
|
-
import { OAuthStrategy, OAuthProfile } from './strategy'
|
|
7
|
-
import { default as setupExpress } from './express'
|
|
8
|
-
import { OauthSetupSettings, getDefaultSettings } from './utils'
|
|
1
|
+
import defaultsDeep from 'lodash/defaultsDeep'
|
|
2
|
+
import each from 'lodash/each'
|
|
3
|
+
import omit from 'lodash/omit'
|
|
4
|
+
import { createDebug } from '@feathersjs/commons'
|
|
5
|
+
import { Application } from '@feathersjs/feathers'
|
|
6
|
+
import { OAuthStrategy, OAuthProfile } from './strategy'
|
|
7
|
+
import { default as setupExpress } from './express'
|
|
8
|
+
import { OauthSetupSettings, getDefaultSettings } from './utils'
|
|
9
9
|
|
|
10
|
-
const debug = createDebug('@feathersjs/authentication-oauth')
|
|
10
|
+
const debug = createDebug('@feathersjs/authentication-oauth')
|
|
11
11
|
|
|
12
|
-
export { OauthSetupSettings, OAuthStrategy, OAuthProfile }
|
|
12
|
+
export { OauthSetupSettings, OAuthStrategy, OAuthProfile }
|
|
13
13
|
|
|
14
14
|
export const setup = (options: OauthSetupSettings) => (app: Application) => {
|
|
15
|
-
const service = app.defaultAuthentication ? app.defaultAuthentication(options.authService) : null
|
|
15
|
+
const service = app.defaultAuthentication ? app.defaultAuthentication(options.authService) : null
|
|
16
16
|
|
|
17
17
|
if (!service) {
|
|
18
|
-
throw new Error(
|
|
18
|
+
throw new Error(
|
|
19
|
+
'An authentication service must exist before registering @feathersjs/authentication-oauth'
|
|
20
|
+
)
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
const { oauth } = service.configuration
|
|
23
|
+
const { oauth } = service.configuration
|
|
22
24
|
|
|
23
25
|
if (!oauth) {
|
|
24
|
-
debug('No oauth configuration found in authentication configuration. Skipping oAuth setup.')
|
|
25
|
-
return
|
|
26
|
+
debug('No oauth configuration found in authentication configuration. Skipping oAuth setup.')
|
|
27
|
+
return
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
const { strategyNames } = service
|
|
30
|
+
const { strategyNames } = service
|
|
29
31
|
|
|
30
32
|
// Set up all the defaults
|
|
31
|
-
const { prefix = '/oauth' } = oauth.defaults || {}
|
|
32
|
-
const port = app.get('port')
|
|
33
|
-
let host = app.get('host')
|
|
34
|
-
let protocol = 'https'
|
|
33
|
+
const { prefix = '/oauth' } = oauth.defaults || {}
|
|
34
|
+
const port = app.get('port')
|
|
35
|
+
let host = app.get('host')
|
|
36
|
+
let protocol = 'https'
|
|
35
37
|
|
|
36
38
|
// Development environments commonly run on HTTP with an extended port
|
|
37
39
|
if (app.get('env') === 'development') {
|
|
38
|
-
protocol = 'http'
|
|
40
|
+
protocol = 'http'
|
|
39
41
|
if (String(port) !== '80') {
|
|
40
|
-
host += `:${port}
|
|
42
|
+
host += `:${port}`
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
const grant = defaultsDeep(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
const grant = defaultsDeep(
|
|
47
|
+
{
|
|
48
|
+
defaults: {
|
|
49
|
+
prefix,
|
|
50
|
+
origin: `${oauth?.defaults?.protocol ?? protocol}://${oauth?.defaults?.host ?? host}`,
|
|
51
|
+
transport: oauth?.defaults?.transport ?? 'session',
|
|
52
|
+
response: ['tokens', 'raw', 'profile']
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
omit(oauth, ['redirect', 'origins'])
|
|
56
|
+
)
|
|
52
57
|
|
|
53
58
|
const getUrl = (url: string) => {
|
|
54
|
-
const { defaults } = grant
|
|
55
|
-
return `${defaults.origin}${prefix}/${url}
|
|
56
|
-
}
|
|
59
|
+
const { defaults } = grant
|
|
60
|
+
return `${defaults.origin}${prefix}/${url}`
|
|
61
|
+
}
|
|
57
62
|
|
|
58
63
|
each(grant, (value, name) => {
|
|
59
64
|
if (name !== 'defaults') {
|
|
60
|
-
value.callback = value.callback || getUrl(`${name}/authenticate`)
|
|
61
|
-
value.redirect_uri = value.redirect_uri || getUrl(`${name}/callback`)
|
|
65
|
+
value.callback = value.callback || getUrl(`${name}/authenticate`)
|
|
66
|
+
value.redirect_uri = value.redirect_uri || getUrl(`${name}/callback`)
|
|
62
67
|
|
|
63
68
|
if (!strategyNames.includes(name)) {
|
|
64
|
-
debug(`Registering oAuth default strategy for '${name}'`)
|
|
65
|
-
service.register(name, new OAuthStrategy())
|
|
69
|
+
debug(`Registering oAuth default strategy for '${name}'`)
|
|
70
|
+
service.register(name, new OAuthStrategy())
|
|
66
71
|
}
|
|
67
72
|
}
|
|
68
|
-
})
|
|
73
|
+
})
|
|
69
74
|
|
|
70
|
-
app.set('grant', grant)
|
|
71
|
-
}
|
|
75
|
+
app.set('grant', grant)
|
|
76
|
+
}
|
|
72
77
|
|
|
73
|
-
export const express =
|
|
74
|
-
|
|
78
|
+
export const express =
|
|
79
|
+
(settings: Partial<OauthSetupSettings> = {}) =>
|
|
80
|
+
(app: Application) => {
|
|
81
|
+
const options = getDefaultSettings(app, settings)
|
|
75
82
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
83
|
+
app.configure(setup(options))
|
|
84
|
+
app.configure(setupExpress(options))
|
|
85
|
+
}
|
|
79
86
|
|
|
80
|
-
export const expressOauth = express
|
|
87
|
+
export const expressOauth = express
|
package/src/strategy.ts
CHANGED
|
@@ -1,176 +1,183 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
3
|
// @ts-ignore
|
|
3
|
-
import querystring from 'querystring'
|
|
4
|
+
import querystring from 'querystring'
|
|
4
5
|
import {
|
|
5
|
-
AuthenticationRequest,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
AuthenticationRequest,
|
|
7
|
+
AuthenticationBaseStrategy,
|
|
8
|
+
AuthenticationResult,
|
|
9
|
+
AuthenticationParams
|
|
10
|
+
} from '@feathersjs/authentication'
|
|
11
|
+
import { Params } from '@feathersjs/feathers'
|
|
12
|
+
import { NotAuthenticated } from '@feathersjs/errors'
|
|
13
|
+
import { createDebug, _ } from '@feathersjs/commons'
|
|
10
14
|
|
|
11
|
-
const debug = createDebug('@feathersjs/authentication-oauth/strategy')
|
|
15
|
+
const debug = createDebug('@feathersjs/authentication-oauth/strategy')
|
|
12
16
|
|
|
13
17
|
export interface OAuthProfile {
|
|
14
|
-
id?: string|number
|
|
15
|
-
[key: string]: any
|
|
18
|
+
id?: string | number
|
|
19
|
+
[key: string]: any
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
export class OAuthStrategy extends AuthenticationBaseStrategy {
|
|
19
|
-
get configuration
|
|
20
|
-
const { entity, service, entityId, oauth } = this.authentication.configuration
|
|
21
|
-
const config = oauth[this.name]
|
|
23
|
+
get configuration() {
|
|
24
|
+
const { entity, service, entityId, oauth } = this.authentication.configuration
|
|
25
|
+
const config = oauth[this.name]
|
|
22
26
|
|
|
23
27
|
return {
|
|
24
28
|
entity,
|
|
25
29
|
service,
|
|
26
30
|
entityId,
|
|
27
31
|
...config
|
|
28
|
-
}
|
|
32
|
+
}
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
get entityId
|
|
32
|
-
const { entityService } = this
|
|
35
|
+
get entityId(): string {
|
|
36
|
+
const { entityService } = this
|
|
33
37
|
|
|
34
|
-
return this.configuration.entityId || (entityService && (entityService as any).id)
|
|
38
|
+
return this.configuration.entityId || (entityService && (entityService as any).id)
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
async getEntityQuery
|
|
41
|
+
async getEntityQuery(profile: OAuthProfile, _params: Params) {
|
|
38
42
|
return {
|
|
39
43
|
[`${this.name}Id`]: profile.sub || profile.id
|
|
40
|
-
}
|
|
44
|
+
}
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
async getEntityData
|
|
47
|
+
async getEntityData(profile: OAuthProfile, _existingEntity: any, _params: Params) {
|
|
44
48
|
return {
|
|
45
49
|
[`${this.name}Id`]: profile.sub || profile.id
|
|
46
|
-
}
|
|
50
|
+
}
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
async getProfile
|
|
53
|
+
async getProfile(data: AuthenticationRequest, _params: Params) {
|
|
50
54
|
return data.profile
|
|
51
55
|
}
|
|
52
56
|
|
|
53
|
-
async getCurrentEntity
|
|
54
|
-
const { authentication } = params
|
|
55
|
-
const { entity } = this.configuration
|
|
57
|
+
async getCurrentEntity(params: Params) {
|
|
58
|
+
const { authentication } = params
|
|
59
|
+
const { entity } = this.configuration
|
|
56
60
|
|
|
57
61
|
if (authentication && authentication.strategy) {
|
|
58
|
-
debug('getCurrentEntity with authentication', authentication)
|
|
62
|
+
debug('getCurrentEntity with authentication', authentication)
|
|
59
63
|
|
|
60
|
-
const { strategy } = authentication
|
|
61
|
-
const authResult = await this.authentication
|
|
62
|
-
.authenticate(authentication, params, strategy);
|
|
64
|
+
const { strategy } = authentication
|
|
65
|
+
const authResult = await this.authentication.authenticate(authentication, params, strategy)
|
|
63
66
|
|
|
64
|
-
return authResult[entity]
|
|
67
|
+
return authResult[entity]
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
return null
|
|
70
|
+
return null
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
async getAllowedOrigin
|
|
71
|
-
const { redirect, origins } = this.authentication.configuration.oauth
|
|
73
|
+
async getAllowedOrigin(params?: Params) {
|
|
74
|
+
const { redirect, origins } = this.authentication.configuration.oauth
|
|
72
75
|
|
|
73
76
|
if (Array.isArray(origins)) {
|
|
74
|
-
const referer = params?.headers?.referer || ''
|
|
75
|
-
const allowedOrigin = origins.find(current => referer.toLowerCase().startsWith(current.toLowerCase()))
|
|
77
|
+
const referer = params?.headers?.referer || ''
|
|
78
|
+
const allowedOrigin = origins.find((current) => referer.toLowerCase().startsWith(current.toLowerCase()))
|
|
76
79
|
|
|
77
|
-
if(!allowedOrigin) {
|
|
78
|
-
throw new NotAuthenticated(`Referer "${referer || '[header not available]'}" not allowed.`)
|
|
80
|
+
if (!allowedOrigin) {
|
|
81
|
+
throw new NotAuthenticated(`Referer "${referer || '[header not available]'}" not allowed.`)
|
|
79
82
|
}
|
|
80
83
|
|
|
81
|
-
return allowedOrigin
|
|
84
|
+
return allowedOrigin
|
|
82
85
|
}
|
|
83
86
|
|
|
84
|
-
return redirect
|
|
87
|
+
return redirect
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
async getRedirect
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
async getRedirect(
|
|
91
|
+
data: AuthenticationResult | Error,
|
|
92
|
+
params?: AuthenticationParams
|
|
93
|
+
): Promise<string | null> {
|
|
94
|
+
const queryRedirect = (params && params.redirect) || ''
|
|
95
|
+
const redirect = await this.getAllowedOrigin(params)
|
|
90
96
|
|
|
91
97
|
if (!redirect) {
|
|
92
|
-
return null
|
|
98
|
+
return null
|
|
93
99
|
}
|
|
94
100
|
|
|
95
|
-
const redirectUrl = `${redirect}${queryRedirect}
|
|
96
|
-
const separator = redirect.endsWith('?') ? '' :
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
const redirectUrl = `${redirect}${queryRedirect}`
|
|
102
|
+
const separator = redirect.endsWith('?') ? '' : redirect.indexOf('#') !== -1 ? '?' : '#'
|
|
103
|
+
const authResult: AuthenticationResult = data
|
|
104
|
+
const query = authResult.accessToken
|
|
105
|
+
? {
|
|
106
|
+
access_token: authResult.accessToken
|
|
107
|
+
}
|
|
108
|
+
: {
|
|
109
|
+
error: data.message || 'OAuth Authentication not successful'
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return `${redirectUrl}${separator}${querystring.stringify(query)}`
|
|
106
113
|
}
|
|
107
114
|
|
|
108
|
-
async findEntity
|
|
109
|
-
const query = await this.getEntityQuery(profile, params)
|
|
115
|
+
async findEntity(profile: OAuthProfile, params: Params) {
|
|
116
|
+
const query = await this.getEntityQuery(profile, params)
|
|
110
117
|
|
|
111
|
-
debug('findEntity with query', query)
|
|
118
|
+
debug('findEntity with query', query)
|
|
112
119
|
|
|
113
120
|
const result = await this.entityService.find({
|
|
114
121
|
...params,
|
|
115
122
|
query
|
|
116
|
-
})
|
|
117
|
-
const [
|
|
123
|
+
})
|
|
124
|
+
const [entity = null] = result.data ? result.data : result
|
|
118
125
|
|
|
119
|
-
debug('findEntity returning', entity)
|
|
126
|
+
debug('findEntity returning', entity)
|
|
120
127
|
|
|
121
|
-
return entity
|
|
128
|
+
return entity
|
|
122
129
|
}
|
|
123
130
|
|
|
124
|
-
async createEntity
|
|
125
|
-
const data = await this.getEntityData(profile, null, params)
|
|
131
|
+
async createEntity(profile: OAuthProfile, params: Params) {
|
|
132
|
+
const data = await this.getEntityData(profile, null, params)
|
|
126
133
|
|
|
127
|
-
debug('createEntity with data', data)
|
|
134
|
+
debug('createEntity with data', data)
|
|
128
135
|
|
|
129
|
-
return this.entityService.create(data, _.omit(params, 'query'))
|
|
136
|
+
return this.entityService.create(data, _.omit(params, 'query'))
|
|
130
137
|
}
|
|
131
138
|
|
|
132
|
-
async updateEntity
|
|
133
|
-
const id = entity[this.entityId]
|
|
134
|
-
const data = await this.getEntityData(profile, entity, params)
|
|
139
|
+
async updateEntity(entity: any, profile: OAuthProfile, params: Params) {
|
|
140
|
+
const id = entity[this.entityId]
|
|
141
|
+
const data = await this.getEntityData(profile, entity, params)
|
|
135
142
|
|
|
136
|
-
debug(`updateEntity with id ${id} and data`, data)
|
|
143
|
+
debug(`updateEntity with id ${id} and data`, data)
|
|
137
144
|
|
|
138
|
-
return this.entityService.patch(id, data, _.omit(params, 'query'))
|
|
145
|
+
return this.entityService.patch(id, data, _.omit(params, 'query'))
|
|
139
146
|
}
|
|
140
147
|
|
|
141
|
-
async getEntity
|
|
142
|
-
const { entityService } = this
|
|
143
|
-
const { entityId = (entityService as any).id, entity } = this.configuration
|
|
148
|
+
async getEntity(result: any, params: Params) {
|
|
149
|
+
const { entityService } = this
|
|
150
|
+
const { entityId = (entityService as any).id, entity } = this.configuration
|
|
144
151
|
|
|
145
152
|
if (!entityId || result[entityId] === undefined) {
|
|
146
|
-
throw new NotAuthenticated('Could not get oAuth entity')
|
|
153
|
+
throw new NotAuthenticated('Could not get oAuth entity')
|
|
147
154
|
}
|
|
148
155
|
|
|
149
156
|
if (!params.provider) {
|
|
150
|
-
return result
|
|
157
|
+
return result
|
|
151
158
|
}
|
|
152
159
|
|
|
153
160
|
return entityService.get(result[entityId], {
|
|
154
161
|
..._.omit(params, 'query'),
|
|
155
162
|
[entity]: result
|
|
156
|
-
})
|
|
163
|
+
})
|
|
157
164
|
}
|
|
158
165
|
|
|
159
|
-
async authenticate
|
|
160
|
-
const entity: string = this.configuration.entity
|
|
161
|
-
const { provider, ...params } = originalParams
|
|
162
|
-
const profile = await this.getProfile(authentication, params)
|
|
163
|
-
const existingEntity = await this.findEntity(profile, params)
|
|
164
|
-
|| await this.getCurrentEntity(params);
|
|
166
|
+
async authenticate(authentication: AuthenticationRequest, originalParams: AuthenticationParams) {
|
|
167
|
+
const entity: string = this.configuration.entity
|
|
168
|
+
const { provider, ...params } = originalParams
|
|
169
|
+
const profile = await this.getProfile(authentication, params)
|
|
170
|
+
const existingEntity = (await this.findEntity(profile, params)) || (await this.getCurrentEntity(params))
|
|
165
171
|
|
|
166
|
-
debug('authenticate with (existing) entity', existingEntity)
|
|
172
|
+
debug('authenticate with (existing) entity', existingEntity)
|
|
167
173
|
|
|
168
|
-
const authEntity = !existingEntity
|
|
169
|
-
|
|
174
|
+
const authEntity = !existingEntity
|
|
175
|
+
? await this.createEntity(profile, params)
|
|
176
|
+
: await this.updateEntity(existingEntity, profile, params)
|
|
170
177
|
|
|
171
178
|
return {
|
|
172
179
|
authentication: { strategy: this.name },
|
|
173
180
|
[entity]: await this.getEntity(authEntity, originalParams)
|
|
174
|
-
}
|
|
181
|
+
}
|
|
175
182
|
}
|
|
176
183
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { RequestHandler } from 'express'
|
|
2
|
-
import { Application } from '@feathersjs/feathers'
|
|
1
|
+
import { RequestHandler } from 'express'
|
|
2
|
+
import { Application } from '@feathersjs/feathers'
|
|
3
3
|
|
|
4
4
|
export interface OauthSetupSettings {
|
|
5
|
-
authService?: string
|
|
6
|
-
expressSession?: RequestHandler
|
|
7
|
-
linkStrategy: string
|
|
5
|
+
authService?: string
|
|
6
|
+
expressSession?: RequestHandler
|
|
7
|
+
linkStrategy: string
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export const getDefaultSettings = (_app: Application, other?: Partial<OauthSetupSettings>) => {
|
|
11
11
|
const defaults: OauthSetupSettings = {
|
|
12
12
|
linkStrategy: 'jwt',
|
|
13
13
|
...other
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
return defaults
|
|
17
|
-
}
|
|
16
|
+
return defaults
|
|
17
|
+
}
|