@steedos/service-identity-jwt 2.3.2-beta.9 → 2.3.3-beta.1
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/main/default/routes/apps.router.js +5 -1
- package/main/default/routes/external_app.router.d.ts +5 -0
- package/main/default/routes/external_app.router.js +48 -0
- package/package.json +4 -3
- package/package.service.js +57 -25
- package/src/routes/apps.router.ts +7 -4
- package/src/routes/external_app.router.ts +55 -0
|
@@ -3,9 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const account_1 = require("./account");
|
|
5
5
|
const passport = require('passport');
|
|
6
|
-
const
|
|
6
|
+
const express = require('express');
|
|
7
|
+
const router = express.Router();
|
|
7
8
|
router.use('/accounts/jwt/login', passport.authenticate('jwt', { session: false }), (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
8
9
|
account_1.Account.ssoLogin(req, res, { err: null, user: req.user, redirect: false, accessToken: null }).then((loginResult) => {
|
|
10
|
+
if (req.query.redirect) {
|
|
11
|
+
return res.redirect(req.query.redirect);
|
|
12
|
+
}
|
|
9
13
|
delete loginResult.user.services;
|
|
10
14
|
delete loginResult.user.thirdPartyUser;
|
|
11
15
|
return res.status(200).send(loginResult);
|
|
@@ -0,0 +1,48 @@
|
|
|
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 express = require('express');
|
|
11
|
+
const router = express.Router();
|
|
12
|
+
const core = require('@steedos/core');
|
|
13
|
+
const objectql = require('@steedos/objectql');
|
|
14
|
+
const jwt = require('jsonwebtoken');
|
|
15
|
+
router.get('/api/external/app/:appId', core.requireAuthentication, function (req, res) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
try {
|
|
18
|
+
const userSession = req.user;
|
|
19
|
+
const { appId } = req.params;
|
|
20
|
+
const appObj = objectql.getObject('apps');
|
|
21
|
+
const appDoc = yield appObj.findOne(appId);
|
|
22
|
+
if (!appDoc) {
|
|
23
|
+
throw new Error(`not found app by ${appId}`);
|
|
24
|
+
}
|
|
25
|
+
const { url, secret } = appDoc;
|
|
26
|
+
if (!url) {
|
|
27
|
+
throw new Error(`url is null in ${appId}`);
|
|
28
|
+
}
|
|
29
|
+
if (!secret) {
|
|
30
|
+
throw new Error(`secret is null in ${appId}`);
|
|
31
|
+
}
|
|
32
|
+
const options = { expiresIn: 10 * 60 };
|
|
33
|
+
const token = jwt.sign({
|
|
34
|
+
profile: {
|
|
35
|
+
name: userSession.name,
|
|
36
|
+
username: userSession.username,
|
|
37
|
+
email: userSession.email
|
|
38
|
+
}
|
|
39
|
+
}, secret, options);
|
|
40
|
+
res.redirect(`${url}?t=${token}`);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
objectql.getSteedosSchema().broker.logger.error(error.stack);
|
|
44
|
+
res.status(500).send({ success: false, message: error.message });
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
exports.default = router;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steedos/service-identity-jwt",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.3-beta.1",
|
|
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.
|
|
14
|
+
"@steedos/accounts": "2.3.3-beta.1",
|
|
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": "70ac301dccf90c0723115e3f4a8d73ce8cd4f5fc"
|
|
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-29 16:09:26
|
|
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,65 @@ module.exports = {
|
|
|
81
82
|
*/
|
|
82
83
|
async started() {
|
|
83
84
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const jwtAuthenticate = function (jwt_payload, done) {
|
|
85
|
+
const secret = this.settings.jwt.secret
|
|
86
|
+
const jwtOptions = {
|
|
87
|
+
// secretOrKey: this.settings.jwt.secret,
|
|
88
|
+
secretOrKeyProvider: function (request, rawJwtToken, done) {
|
|
91
89
|
try {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
let decoded = jwt.decode(rawJwtToken, { complete: true });
|
|
91
|
+
let payload = decoded.payload
|
|
92
|
+
const app_code = payload.app_code
|
|
93
|
+
if (app_code) { // 走应用认证
|
|
94
|
+
objectql.getObject('apps').find({filters: [['code', '=', app_code]], fields: ['secret']}).then((records)=>{
|
|
95
95
|
if(records.length > 0){
|
|
96
|
-
const
|
|
97
|
-
|
|
96
|
+
const app = records[0];
|
|
97
|
+
if (app.secret) {
|
|
98
|
+
return done(null, app.secret)
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
done(`app ${app_code}'s secret is null`, null)
|
|
102
|
+
}
|
|
98
103
|
}else{
|
|
99
|
-
return done('
|
|
104
|
+
return done('app not find', null)
|
|
100
105
|
}
|
|
101
106
|
})
|
|
102
|
-
}else{
|
|
103
|
-
return done('JWT invalid', null)
|
|
104
107
|
}
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
else if (secret) { // 走全局配置
|
|
109
|
+
return done(null, secret)
|
|
110
|
+
}
|
|
111
|
+
else { // 都没有则认证不通过
|
|
112
|
+
return done('need secret', null)
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
// console.error(error)
|
|
116
|
+
return done(error.message, null)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
},
|
|
120
|
+
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), ExtractJwt.fromUrlQueryParameter('t')])
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const jwtAuthenticate = function (jwt_payload, done) {
|
|
124
|
+
try {
|
|
125
|
+
const { profile } = jwt_payload;
|
|
126
|
+
if(profile && profile.email){
|
|
127
|
+
objectql.getObject('users').find({filters: [['email', '=', profile.email]], fields: ['_id', 'name', 'email', 'username']}).then((records)=>{
|
|
128
|
+
if(records.length > 0){
|
|
129
|
+
const user = records[0];
|
|
130
|
+
return done(null, Object.assign({}, user, {id: user._id}))
|
|
131
|
+
}else{
|
|
132
|
+
return done('user not find', null)
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
}else{
|
|
136
|
+
return done('JWT invalid', null)
|
|
107
137
|
}
|
|
138
|
+
} catch (err) {
|
|
139
|
+
return authError(done, "JWT invalid", err)
|
|
108
140
|
}
|
|
109
|
-
|
|
110
|
-
passport.use(new JwtStrategy(jwtOptions, jwtAuthenticate))
|
|
111
141
|
}
|
|
142
|
+
|
|
143
|
+
passport.use(new JwtStrategy(jwtOptions, jwtAuthenticate))
|
|
112
144
|
} catch (error) {
|
|
113
145
|
// this.setError(error);
|
|
114
146
|
this.broker.logger.error(error)
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* @Author: baozhoutao@steedos.com
|
|
3
3
|
* @Date: 2022-06-08 23:28:39
|
|
4
|
-
* @LastEditors:
|
|
5
|
-
* @LastEditTime: 2022-11-
|
|
4
|
+
* @LastEditors: sunhaolin@hotoa.com
|
|
5
|
+
* @LastEditTime: 2022-11-29 15:53:46
|
|
6
6
|
* @Description:
|
|
7
7
|
*/
|
|
8
8
|
import { Account } from './account'
|
|
9
9
|
const passport = require('passport');
|
|
10
|
-
|
|
11
|
-
const router =
|
|
10
|
+
const express = require('express');
|
|
11
|
+
const router =express.Router();
|
|
12
12
|
|
|
13
13
|
router.use('/accounts/jwt/login', passport.authenticate('jwt', { session: false }), async (req: any, res) => {
|
|
14
14
|
Account.ssoLogin(req, res, { err: null, user: req.user, redirect: false, accessToken: null }).then((loginResult) => {
|
|
15
|
+
if (req.query.redirect) { // 如果url传递了重定向的参数则优先重定向
|
|
16
|
+
return res.redirect(req.query.redirect)
|
|
17
|
+
}
|
|
15
18
|
delete loginResult.user.services;
|
|
16
19
|
delete loginResult.user.thirdPartyUser;
|
|
17
20
|
return res.status(200).send(loginResult)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: sunhaolin@hotoa.com
|
|
3
|
+
* @Date: 2022-11-23 15:54:33
|
|
4
|
+
* @LastEditors: baozhoutao@steedos.com
|
|
5
|
+
* @LastEditTime: 2022-11-28 17:59:06
|
|
6
|
+
* @Description: 新增jwt 接口(GET), 所有第三方应用都通过此接口转发出去, 自动带上 jwt 加密(app.secret)后的数据,比如username、email等.
|
|
7
|
+
*/
|
|
8
|
+
const express = require('express');
|
|
9
|
+
const router =express.Router();
|
|
10
|
+
const core = require('@steedos/core');
|
|
11
|
+
const objectql = require('@steedos/objectql')
|
|
12
|
+
const jwt = require('jsonwebtoken')
|
|
13
|
+
|
|
14
|
+
router.get('/api/external/app/:appId', core.requireAuthentication, async function (req, res) {
|
|
15
|
+
try {
|
|
16
|
+
const userSession = req.user;
|
|
17
|
+
// const spaceId = userSession.spaceId;
|
|
18
|
+
// const userId = userSession.userId;
|
|
19
|
+
// const isSpaceAdmin = userSession.is_space_admin;
|
|
20
|
+
const { appId } = req.params
|
|
21
|
+
|
|
22
|
+
const appObj = objectql.getObject('apps')
|
|
23
|
+
|
|
24
|
+
const appDoc = await appObj.findOne(appId)
|
|
25
|
+
|
|
26
|
+
if (!appDoc) {
|
|
27
|
+
throw new Error(`not found app by ${appId}`)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const { url, secret } = appDoc
|
|
31
|
+
|
|
32
|
+
if (!url) {
|
|
33
|
+
throw new Error(`url is null in ${appId}`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!secret) {
|
|
37
|
+
throw new Error(`secret is null in ${appId}`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const options = { expiresIn: 10 * 60 } // 有效期十分钟
|
|
41
|
+
const token = jwt.sign({
|
|
42
|
+
profile: {
|
|
43
|
+
name: userSession.name,
|
|
44
|
+
username: userSession.username,
|
|
45
|
+
email: userSession.email
|
|
46
|
+
}
|
|
47
|
+
}, secret, options);
|
|
48
|
+
|
|
49
|
+
res.redirect(`${url}?t=${token}`);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
objectql.getSteedosSchema().broker.logger.error(error.stack)
|
|
52
|
+
res.status(500).send({ success: false, message: error.message })
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
exports.default = router;
|