135 lines
3.9 KiB
JavaScript
135 lines
3.9 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
require('dotenv').config();
|
|
|
|
const User = require('./models/User');
|
|
const OAuthClient = require('./models/OAuthClient');
|
|
const OAuthToken = require('./models/OAuthToken');
|
|
const authRoutes = require('./routes/auth');
|
|
const oauthRoutes = require('./routes/oauth');
|
|
const oauthClientRoutes = require('./routes/oauth-clients');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// 中间件
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// 请求日志中间件
|
|
app.use((req, res, next) => {
|
|
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
|
|
next();
|
|
});
|
|
|
|
// 路由
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/oauth', oauthRoutes);
|
|
app.use('/api/oauth', oauthClientRoutes);
|
|
|
|
// 根路由
|
|
app.get('/', (req, res) => {
|
|
res.json({
|
|
success: true,
|
|
message: '认证和OAuth API服务器运行中',
|
|
endpoints: {
|
|
auth: {
|
|
register: 'POST /api/auth/register',
|
|
login: 'POST /api/auth/login',
|
|
profile: 'GET /api/auth/profile',
|
|
test: 'GET /api/auth/test'
|
|
},
|
|
oauth: {
|
|
authorize: 'GET /api/oauth/authorize',
|
|
token: 'POST /api/oauth/token',
|
|
revoke: 'POST /api/oauth/revoke',
|
|
userinfo: 'GET /api/oauth/userinfo',
|
|
tokeninfo: 'GET /api/oauth/tokeninfo'
|
|
},
|
|
oauth_clients: {
|
|
create: 'POST /api/oauth/clients',
|
|
list: 'GET /api/oauth/clients',
|
|
detail: 'GET /api/oauth/clients/:clientId',
|
|
delete: 'DELETE /api/oauth/clients/:clientId',
|
|
stats: 'GET /api/oauth/clients/:clientId/stats'
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
// 健康检查
|
|
app.get('/health', (req, res) => {
|
|
res.json({
|
|
success: true,
|
|
message: '服务器运行正常',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
// OAuth发现端点
|
|
app.get('/.well-known/oauth-authorization-server', (req, res) => {
|
|
res.json({
|
|
issuer: `${req.protocol}://${req.get('host')}`,
|
|
authorization_endpoint: `${req.protocol}://${req.get('host')}/api/oauth/authorize`,
|
|
token_endpoint: `${req.protocol}://${req.get('host')}/api/oauth/token`,
|
|
revocation_endpoint: `${req.protocol}://${req.get('host')}/api/oauth/revoke`,
|
|
userinfo_endpoint: `${req.protocol}://${req.get('host')}/api/oauth/userinfo`,
|
|
tokeninfo_endpoint: `${req.protocol}://${req.get('host')}/api/oauth/tokeninfo`,
|
|
response_types_supported: ['code'],
|
|
grant_types_supported: ['authorization_code', 'refresh_token'],
|
|
token_endpoint_auth_methods_supported: ['client_secret_post'],
|
|
scopes_supported: ['read', 'write'],
|
|
code_challenge_methods_supported: []
|
|
});
|
|
});
|
|
|
|
// 404处理
|
|
app.use('*', (req, res) => {
|
|
res.status(404).json({
|
|
success: false,
|
|
message: '路由不存在'
|
|
});
|
|
});
|
|
|
|
// 错误处理中间件
|
|
app.use((err, req, res, next) => {
|
|
console.error('服务器错误:', err);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '服务器内部错误'
|
|
});
|
|
});
|
|
|
|
// 启动服务器
|
|
const startServer = async () => {
|
|
try {
|
|
// 创建所有数据库表
|
|
await User.createTable();
|
|
await OAuthClient.createTable();
|
|
await OAuthClient.createAuthCodeTable();
|
|
await OAuthClient.createAccessTokenTable();
|
|
await OAuthClient.createRefreshTokenTable();
|
|
|
|
// 设置定时清理过期令牌
|
|
setInterval(async () => {
|
|
try {
|
|
await OAuthToken.cleanupExpiredTokens();
|
|
} catch (error) {
|
|
console.error('清理过期令牌失败:', error);
|
|
}
|
|
}, 60 * 60 * 1000); // 每小时清理一次
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`服务器运行在端口 ${PORT}`);
|
|
console.log(`API文档: http://localhost:${PORT}`);
|
|
console.log(`OAuth发现端点: http://localhost:${PORT}/.well-known/oauth-authorization-server`);
|
|
});
|
|
} catch (error) {
|
|
console.error('启动服务器失败:', error);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
startServer();
|