优化
This commit is contained in:
13
server/test/demo/getui/index.ts
Normal file
13
server/test/demo/getui/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
import { GetuiClient } from '../../../plugins/com.msgbyte.getui/lib/GetuiClient';
|
||||
|
||||
const client = new GetuiClient(
|
||||
process.env.GETUI_APPID,
|
||||
process.env.GETUI_APPKEY,
|
||||
process.env.GETUI_MASTERSECRET
|
||||
);
|
||||
|
||||
client.allPush('title', 'body', {}).then((res) => {
|
||||
console.log('res', res);
|
||||
});
|
||||
11
server/test/demo/openapi-client-simple/app.html
Normal file
11
server/test/demo/openapi-client-simple/app.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>OIDC Client Demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="<API>/open/auth?client_id=<clientId>&redirect_uri=<clientUrl>/cb&scope=openid profile&response_type=code&state=45535116436">登录</a>
|
||||
</body>
|
||||
</html>
|
||||
86
server/test/demo/openapi-client-simple/index.ts
Normal file
86
server/test/demo/openapi-client-simple/index.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import axios from 'axios';
|
||||
import fs from 'fs-extra';
|
||||
const app = express();
|
||||
const port = process.env.PORT || 8080;
|
||||
|
||||
const API = process.env.API || 'https://tailchat-nightly.moonrailgun.com'; // dev environment is 'http://localhost:11001'
|
||||
const clientUrl = `http://localhost:${port}`;
|
||||
const clientId = process.env.ID || 'tc_649aa2179e97b8b3b2d1004f';
|
||||
const clientSecret = process.env.SECRET || '4Pt4lccOaztJROs-VhmQf8XBU89-z8rr';
|
||||
|
||||
console.log('config:', {
|
||||
API,
|
||||
clientUrl,
|
||||
clientId,
|
||||
});
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: API,
|
||||
transformRequest: [
|
||||
function (data) {
|
||||
let ret = '';
|
||||
for (const it in data) {
|
||||
ret +=
|
||||
encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
|
||||
}
|
||||
ret = ret.substring(0, ret.lastIndexOf('&'));
|
||||
return ret;
|
||||
},
|
||||
],
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
});
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
let html = (
|
||||
await fs.readFile(path.resolve(__dirname, './app.html'))
|
||||
).toString();
|
||||
html = html
|
||||
.replace('<API>', API)
|
||||
.replace('<clientId>', clientId)
|
||||
.replace('<clientUrl>', clientUrl);
|
||||
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
app.get('/cb', async (req, res, next) => {
|
||||
try {
|
||||
const { code, state } = req.query;
|
||||
|
||||
console.log('code', code);
|
||||
|
||||
// 根据获取到的code获取授权码
|
||||
const { data: tokenInfo } = await request.post('/open/token', {
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
redirect_uri: `${clientUrl}/cb`,
|
||||
code,
|
||||
grant_type: 'authorization_code',
|
||||
});
|
||||
|
||||
console.log('tokenInfo', tokenInfo);
|
||||
|
||||
const { access_token, expires_in, id_token, scope, token_type } = tokenInfo;
|
||||
|
||||
console.log('access_token', access_token);
|
||||
|
||||
const { data: userInfo } = await request.post('/open/me', {
|
||||
access_token,
|
||||
});
|
||||
|
||||
res.json({ userInfo });
|
||||
} catch (err) {
|
||||
console.error(err.response.data);
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(
|
||||
`Please ensure that the third-party login function is enabled and the callback has been registered in the whitelist of the OIDC server: ${clientUrl}/cb`
|
||||
);
|
||||
console.log(`Test Server Address: http://127.0.0.1:${port}`);
|
||||
});
|
||||
21
server/test/demo/openapi-client-simple/package.json
Normal file
21
server/test/demo/openapi-client-simple/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "openapi-client",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "ts-node ./index.ts",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.26.0",
|
||||
"express": "^4.17.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.15",
|
||||
"ts-node": "10.9.1"
|
||||
}
|
||||
}
|
||||
101
server/test/demo/openapi-client/index.ts
Normal file
101
server/test/demo/openapi-client/index.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { Issuer } from 'openid-client';
|
||||
import express from 'express';
|
||||
import axios from 'axios';
|
||||
|
||||
const app = express();
|
||||
const port = 8080;
|
||||
|
||||
const API = process.env.API || 'http://localhost:11001';
|
||||
const clientUrl = `http://localhost:${port}`;
|
||||
const clientId = process.env.ID || 'tc_649aa2179e97b8b3b2d1004f';
|
||||
const clientSecret = process.env.SECRET || '4Pt4lccOaztJROs-VhmQf8XBU89-z8rr';
|
||||
|
||||
console.log('config:', {
|
||||
API,
|
||||
clientUrl,
|
||||
clientId,
|
||||
});
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: API,
|
||||
transformRequest: [
|
||||
function (data) {
|
||||
let ret = '';
|
||||
for (const it in data) {
|
||||
ret +=
|
||||
encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
|
||||
}
|
||||
ret = ret.substring(0, ret.lastIndexOf('&'));
|
||||
return ret;
|
||||
},
|
||||
],
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
});
|
||||
|
||||
(async () => {
|
||||
const tailchatIssuer = await Issuer.discover(
|
||||
'https://tailchat-nightly.moonrailgun.com/open/'
|
||||
);
|
||||
console.log(
|
||||
'Discovered issuer',
|
||||
tailchatIssuer.issuer,
|
||||
tailchatIssuer.metadata
|
||||
);
|
||||
|
||||
const client = new tailchatIssuer.Client({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
redirect_uris: [`${clientUrl}/cb`],
|
||||
response_types: ['code'],
|
||||
// id_token_signed_response_alg (default "RS256")
|
||||
// token_endpoint_auth_method (default "client_secret_basic")
|
||||
});
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
const url = client.authorizationUrl({
|
||||
scope: 'openid profile',
|
||||
// response_mode: 'form_post',
|
||||
nonce: Math.random().toString(),
|
||||
});
|
||||
res.send(`<a href="${url}">登录</a>`);
|
||||
});
|
||||
|
||||
app.get('/cb', async (req, res, next) => {
|
||||
try {
|
||||
const { code } = req.query;
|
||||
|
||||
// 根据获取到的code获取授权码
|
||||
const { data: tokenInfo } = await request.post('/open/token', {
|
||||
// client_id: 'foo',
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
redirect_uri: `${clientUrl}/cb`,
|
||||
code,
|
||||
grant_type: 'authorization_code',
|
||||
});
|
||||
|
||||
console.log('tokenInfo', tokenInfo);
|
||||
|
||||
const { access_token, expires_in, id_token, scope, token_type } =
|
||||
tokenInfo;
|
||||
|
||||
console.log('access_token', access_token);
|
||||
|
||||
const { data: userInfo } = await request.post('/open/me', {
|
||||
access_token,
|
||||
});
|
||||
|
||||
res.json({ userInfo });
|
||||
} catch (err) {
|
||||
console.error(err.response.data);
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`请确保回调已经被注册在OIDC服务端的白名单中: ${clientUrl}/cb`);
|
||||
console.log(`测试服务地址: http://127.0.0.1:${port}`);
|
||||
});
|
||||
})();
|
||||
21
server/test/demo/openapi-client/package.json
Normal file
21
server/test/demo/openapi-client/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "openapi-client",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "ts-node ./index.ts",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.26.0",
|
||||
"express": "^4.17.2",
|
||||
"openid-client": "^5.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts-node": "10.9.1"
|
||||
}
|
||||
}
|
||||
67
server/test/demo/openapi-oidc-page/index.ts
Normal file
67
server/test/demo/openapi-oidc-page/index.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import ejs from 'ejs';
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 8080;
|
||||
|
||||
const publicDir = path.resolve(__dirname, '../../../public');
|
||||
const viewRootDir = path.resolve(
|
||||
__dirname,
|
||||
'../../../services/openapi/oidc/views'
|
||||
);
|
||||
|
||||
app.use(express.static(publicDir));
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send(
|
||||
`<ul>${['/login', '/authorize', '/error']
|
||||
.map((p) => `<li><a href=${p}>${p}</a></li>`)
|
||||
.join('\n')}</ul>`
|
||||
);
|
||||
});
|
||||
|
||||
app.get('/login', async (req, res) => {
|
||||
const data = {
|
||||
uid: 'fooooooooo',
|
||||
};
|
||||
const html = await ejs.renderFile(
|
||||
path.resolve(viewRootDir, './login.ejs'),
|
||||
data
|
||||
);
|
||||
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
app.get('/error', async (req, res) => {
|
||||
const data = {
|
||||
text: 'fooooooooo',
|
||||
};
|
||||
const html = await ejs.renderFile(
|
||||
path.resolve(viewRootDir, './error.ejs'),
|
||||
data
|
||||
);
|
||||
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
app.get('/authorize', async (req, res) => {
|
||||
const data = {
|
||||
logoUri: 'loginUrl',
|
||||
clientName: 'Test',
|
||||
uid: 'foooo',
|
||||
details: {},
|
||||
params: {},
|
||||
session: '',
|
||||
};
|
||||
const html = await ejs.renderFile(
|
||||
path.resolve(viewRootDir, './authorize.ejs'),
|
||||
data
|
||||
);
|
||||
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server: http://127.0.0.1:${port}`);
|
||||
});
|
||||
22
server/test/demo/openapi-oidc-page/package.json
Normal file
22
server/test/demo/openapi-oidc-page/package.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "openapi-oidc-page",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "ts-node ./index.ts",
|
||||
"dev": "nodemon --watch \"index.ts\" --watch \"../../../services/openapi/oidc/views/**\" --ext \"ts,ejs\" --exec \"ts-node --transpile-only ./index.ts\"",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.17.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.15",
|
||||
"nodemon": "^3.0.1",
|
||||
"ts-node": "10.9.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user