GamePass 是一个基于 Go 语言实现的身份认证服务,支持 OAuth2 登录和 JWT 令牌管理。
- OAuth2 认证支持(目前支持 GitHub)
- JWT 令牌管理(访问令牌和刷新令牌)
- MongoDB 持久化存储
- RESTful API
- 支持令牌刷新和撤销
- 用户会话管理
- Gin - Web 框架
- Goth - OAuth2 认证库
- JWT - JWT 实现
- MongoDB Go Driver - MongoDB 驱动
- GoDotEnv - 环境变量管理
- 克隆仓库:
git clone https://github.com/yourusername/gamepass.git
cd gamepass- 安装依赖:
go mod tidy- 配置环境变量,创建
.env文件:
PORT=3180
BASE_URL=http://localhost:3180
MONGODB_URI=mongodb://localhost:27017
DB_NAME=authdb
GITHUB_KEY=your_github_key
GITHUB_SECRET=your_github_secret
JWT_ACCESS_SECRET=your_access_secret
JWT_REFRESH_SECRET=your_refresh_secret- 运行服务:
go run main.goGET /api/providers- 获取可用的登录提供商列表
示例响应:
{
"providers": [
{
"name": "github",
"auth_url": "/auth/github",
"display_name": "Github"
},
{
"name": "google",
"auth_url": "/auth/google",
"display_name": "Google"
}
],
"total": 2
}GET /auth/github- 开始 GitHub OAuth 登录流程GET /auth/github/callback- GitHub OAuth 回调处理POST /auth/refresh- 刷新访问令牌POST /auth/logout- 注销会话
GET /api/profile- 获取用户资料(需要认证)
# 重定向到 GitHub 登录页面
curl -i -H "Accept: application/json" http://localhost:3180/auth/githubcurl -X POST \
http://localhost:3180/auth/refresh \
-H 'Refresh-Token: your_refresh_token'curl -X GET \
http://localhost:3180/api/profile \
-H 'Authorization: Bearer your_access_token'curl -X POST \
http://localhost:3180/auth/logout \
-H 'Authorization: Bearer your_access_token' \
-H 'Refresh-Token: your_refresh_token'.
├── config/ # 配置相关
├── db/ # 数据库相关
├── handlers/ # HTTP 处理器
├── middlewares/ # 中间件
├── models/ # 数据模型
├── services/ # 业务逻辑
├── utils/ # 工具函数
├── .env # 环境变量
├── main.go # 程序入口
└── README.md # 项目文档
在 main.go 中添加新的提供商:
goth.UseProviders(
github.New(cfg.GithubKey, cfg.GithubSecret, cfg.GithubCallback),
// 添加其他提供商...
)数据库架构变更通过 db/migrations 包管理。
- 使用 HTTPS
- 设置安全的环境变量
- 配置 MongoDB 认证
- 使用反向代理(如 Nginx)
- 设置适当的日志级别
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/amazing-feature) - 提交更改 (
git commit -m 'Add some amazing feature') - 推送到分支 (
git push origin feature/amazing-feature) - 创建 Pull Request
本项目采用 MIT 许可证 - 详见 LICENSE 文件
- hanxi - 项目维护者
- 感谢所有贡献者
- 感谢使用到的开源项目