@steedos/service-identity-jwt 2.3.2-beta.10 → 2.3.2-beta.12
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.
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
const router = require('@steedos/router').staticRouter();
|
|
11
|
+
const core = require('@steedos/core');
|
|
12
|
+
const objectql = require('@steedos/objectql');
|
|
13
|
+
const jwt = require('jsonwebtoken');
|
|
14
|
+
router.get('/api/external/app/:appId', core.requireAuthentication, function (req, res) {
|
|
15
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
try {
|
|
17
|
+
const userSession = req.user;
|
|
18
|
+
const { appId } = req.params;
|
|
19
|
+
const appObj = objectql.getObject('apps');
|
|
20
|
+
const appDoc = yield appObj.findOne(appId);
|
|
21
|
+
if (!appDoc) {
|
|
22
|
+
throw new Error(`not found app by ${appId}`);
|
|
23
|
+
}
|
|
24
|
+
const { url, secret } = appDoc;
|
|
25
|
+
if (!url) {
|
|
26
|
+
throw new Error(`url is null in ${appId}`);
|
|
27
|
+
}
|
|
28
|
+
if (!secret) {
|
|
29
|
+
throw new Error(`secret is null in ${appId}`);
|
|
30
|
+
}
|
|
31
|
+
const options = { expiresIn: 10 * 60 };
|
|
32
|
+
const token = jwt.sign({
|
|
33
|
+
profile: {
|
|
34
|
+
name: userSession.name,
|
|
35
|
+
username: userSession.username,
|
|
36
|
+
email: userSession.email
|
|
37
|
+
}
|
|
38
|
+
}, secret, options);
|
|
39
|
+
res.redirect(`${url}?t=${token}`);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
objectql.getSteedosSchema().broker.logger.error(error.stack);
|
|
43
|
+
res.status(500).send({ success: false, message: error.message });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
exports.default = router;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steedos/service-identity-jwt",
|
|
3
|
-
"version": "2.3.2-beta.
|
|
3
|
+
"version": "2.3.2-beta.12",
|
|
4
4
|
"main": "package.service.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"steedos"
|
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
"description": "steedos package",
|
|
12
12
|
"repository": {},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@steedos/accounts": "2.3.2-beta.
|
|
14
|
+
"@steedos/accounts": "2.3.2-beta.12",
|
|
15
15
|
"express": "4.18.1",
|
|
16
|
+
"jsonwebtoken": "8.5.1",
|
|
16
17
|
"passport": "^0.6.0",
|
|
17
18
|
"passport-jwt": "^4.0.0"
|
|
18
19
|
},
|
|
@@ -21,5 +22,5 @@
|
|
|
21
22
|
"publishConfig": {
|
|
22
23
|
"access": "public"
|
|
23
24
|
},
|
|
24
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "f738241842b61c66eae2b1fe7bf5c0bfb66348d6"
|
|
25
26
|
}
|
package/package.service.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* @Author: baozhoutao@steedos.com
|
|
3
3
|
* @Date: 2022-06-11 18:09:20
|
|
4
|
-
* @LastEditors:
|
|
5
|
-
* @LastEditTime: 2022-
|
|
4
|
+
* @LastEditors: sunhaolin@hotoa.com
|
|
5
|
+
* @LastEditTime: 2022-11-24 13:06:15
|
|
6
6
|
* @Description:
|
|
7
7
|
*/
|
|
8
8
|
"use strict";
|
|
@@ -13,6 +13,7 @@ const packageLoader = require('@steedos/service-package-loader');
|
|
|
13
13
|
const passport = require('passport');
|
|
14
14
|
const JwtStrategy = require("passport-jwt").Strategy
|
|
15
15
|
const ExtractJwt = require('passport-jwt').ExtractJwt;
|
|
16
|
+
const jwt = require('jsonwebtoken');
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
const authError = function (done, message, err = null) {
|
|
@@ -33,7 +34,7 @@ module.exports = {
|
|
|
33
34
|
packageInfo: {
|
|
34
35
|
path: __dirname,
|
|
35
36
|
name: packageName,
|
|
36
|
-
isPackage:
|
|
37
|
+
isPackage: false,
|
|
37
38
|
},
|
|
38
39
|
jwt: {
|
|
39
40
|
secret: process.env.STEEDOS_IDENTITY_JWT_SECRET,
|
|
@@ -64,9 +65,9 @@ module.exports = {
|
|
|
64
65
|
*/
|
|
65
66
|
methods: {
|
|
66
67
|
beforeStart: async function () {
|
|
67
|
-
if(!this.settings.jwt.secret){
|
|
68
|
-
|
|
69
|
-
}
|
|
68
|
+
// if(!this.settings.jwt.secret){
|
|
69
|
+
// throw new Error(`${packageName} jwt secret is not defined`);
|
|
70
|
+
// }
|
|
70
71
|
}
|
|
71
72
|
},
|
|
72
73
|
|
|
@@ -81,34 +82,59 @@ module.exports = {
|
|
|
81
82
|
*/
|
|
82
83
|
async started() {
|
|
83
84
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if(
|
|
96
|
-
|
|
97
|
-
return done(null, Object.assign({}, user, {id: user._id}))
|
|
98
|
-
}else{
|
|
99
|
-
return done('user not find', null)
|
|
85
|
+
const secret = this.settings.jwt.secret
|
|
86
|
+
const jwtOptions = {
|
|
87
|
+
// secretOrKey: this.settings.jwt.secret,
|
|
88
|
+
secretOrKeyProvider: function (request, rawJwtToken, done) {
|
|
89
|
+
let decoded = jwt.decode(rawJwtToken, { complete: true });
|
|
90
|
+
let payload = decoded.payload
|
|
91
|
+
const app_code = payload.app_code
|
|
92
|
+
if (app_code) { // 走应用认证
|
|
93
|
+
objectql.getObject('apps').find({filters: [['code', '=', app_code]], fields: ['secret']}).then((records)=>{
|
|
94
|
+
if(records.length > 0){
|
|
95
|
+
const app = records[0];
|
|
96
|
+
if (app.secret) {
|
|
97
|
+
return done(null, app.secret)
|
|
100
98
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
else {
|
|
100
|
+
done(`app ${app_code}'s secret is null`, null)
|
|
101
|
+
}
|
|
102
|
+
}else{
|
|
103
|
+
return done('app not find', null)
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
else if (secret) { // 走全局配置
|
|
108
|
+
return done(null, secret)
|
|
109
|
+
}
|
|
110
|
+
else { // 都没有则认证不通过
|
|
111
|
+
return done('need secret', null)
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const jwtAuthenticate = function (jwt_payload, done) {
|
|
118
|
+
try {
|
|
119
|
+
const { profile } = jwt_payload;
|
|
120
|
+
if(profile && profile.email){
|
|
121
|
+
objectql.getObject('users').find({filters: [['email', '=', profile.email]], fields: ['_id', 'name', 'email', 'username']}).then((records)=>{
|
|
122
|
+
if(records.length > 0){
|
|
123
|
+
const user = records[0];
|
|
124
|
+
return done(null, Object.assign({}, user, {id: user._id}))
|
|
125
|
+
}else{
|
|
126
|
+
return done('user not find', null)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
}else{
|
|
130
|
+
return done('JWT invalid', null)
|
|
107
131
|
}
|
|
132
|
+
} catch (err) {
|
|
133
|
+
return authError(done, "JWT invalid", err)
|
|
108
134
|
}
|
|
109
|
-
|
|
110
|
-
passport.use(new JwtStrategy(jwtOptions, jwtAuthenticate))
|
|
111
135
|
}
|
|
136
|
+
|
|
137
|
+
passport.use(new JwtStrategy(jwtOptions, jwtAuthenticate))
|
|
112
138
|
} catch (error) {
|
|
113
139
|
// this.setError(error);
|
|
114
140
|
this.broker.logger.error(error)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: sunhaolin@hotoa.com
|
|
3
|
+
* @Date: 2022-11-23 15:54:33
|
|
4
|
+
* @LastEditors: sunhaolin@hotoa.com
|
|
5
|
+
* @LastEditTime: 2022-11-23 16:45:17
|
|
6
|
+
* @Description: 新增jwt 接口(GET), 所有第三方应用都通过此接口转发出去, 自动带上 jwt 加密(app.secret)后的数据,比如username、email等.
|
|
7
|
+
*/
|
|
8
|
+
const router = require('@steedos/router').staticRouter()
|
|
9
|
+
const core = require('@steedos/core');
|
|
10
|
+
const objectql = require('@steedos/objectql')
|
|
11
|
+
const jwt = require('jsonwebtoken')
|
|
12
|
+
|
|
13
|
+
router.get('/api/external/app/:appId', core.requireAuthentication, async function (req, res) {
|
|
14
|
+
try {
|
|
15
|
+
const userSession = req.user;
|
|
16
|
+
// const spaceId = userSession.spaceId;
|
|
17
|
+
// const userId = userSession.userId;
|
|
18
|
+
// const isSpaceAdmin = userSession.is_space_admin;
|
|
19
|
+
const { appId } = req.params
|
|
20
|
+
|
|
21
|
+
const appObj = objectql.getObject('apps')
|
|
22
|
+
|
|
23
|
+
const appDoc = await appObj.findOne(appId)
|
|
24
|
+
|
|
25
|
+
if (!appDoc) {
|
|
26
|
+
throw new Error(`not found app by ${appId}`)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const { url, secret } = appDoc
|
|
30
|
+
|
|
31
|
+
if (!url) {
|
|
32
|
+
throw new Error(`url is null in ${appId}`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!secret) {
|
|
36
|
+
throw new Error(`secret is null in ${appId}`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const options = { expiresIn: 10 * 60 } // 有效期十分钟
|
|
40
|
+
const token = jwt.sign({
|
|
41
|
+
profile: {
|
|
42
|
+
name: userSession.name,
|
|
43
|
+
username: userSession.username,
|
|
44
|
+
email: userSession.email
|
|
45
|
+
}
|
|
46
|
+
}, secret, options);
|
|
47
|
+
|
|
48
|
+
res.redirect(`${url}?t=${token}`);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
objectql.getSteedosSchema().broker.logger.error(error.stack)
|
|
51
|
+
res.status(500).send({ success: false, message: error.message })
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
exports.default = router;
|