passportd 2.0.0__tar.gz

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.
Files changed (73) hide show
  1. passportd-2.0.0/LICENSE +201 -0
  2. passportd-2.0.0/MANIFEST.in +2 -0
  3. passportd-2.0.0/PKG-INFO +143 -0
  4. passportd-2.0.0/README.md +100 -0
  5. passportd-2.0.0/passportd/app.py +141 -0
  6. passportd-2.0.0/passportd/basis/__init__.py +0 -0
  7. passportd-2.0.0/passportd/basis/common.py +122 -0
  8. passportd-2.0.0/passportd/basis/conf.py +222 -0
  9. passportd-2.0.0/passportd/basis/errors.py +82 -0
  10. passportd-2.0.0/passportd/basis/mixin.py +535 -0
  11. passportd-2.0.0/passportd/basis/vars.py +88 -0
  12. passportd-2.0.0/passportd/cli.py +223 -0
  13. passportd-2.0.0/passportd/libs/__init__.py +0 -0
  14. passportd-2.0.0/passportd/libs/interface.py +440 -0
  15. passportd-2.0.0/passportd/libs/oidc.py +428 -0
  16. passportd-2.0.0/passportd/models/__init__.py +0 -0
  17. passportd-2.0.0/passportd/models/model.py +218 -0
  18. passportd-2.0.0/passportd/models/oidc.py +369 -0
  19. passportd-2.0.0/passportd/models/user.py +504 -0
  20. passportd-2.0.0/passportd/modules/__init__.py +0 -0
  21. passportd-2.0.0/passportd/modules/oauth2_gitee/__init__.py +86 -0
  22. passportd-2.0.0/passportd/modules/oauth2_github/__init__.py +89 -0
  23. passportd-2.0.0/passportd/modules/oauth2_qq/__init__.py +154 -0
  24. passportd-2.0.0/passportd/modules/oauth2_weibo/__init__.py +97 -0
  25. passportd-2.0.0/passportd/passportd.py +30 -0
  26. passportd-2.0.0/passportd/static/css/bulma-v1.0.4.min.css +3 -0
  27. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/LICENSE.txt +165 -0
  28. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/all.js +8620 -0
  29. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/all.min.js +6 -0
  30. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/brands.js +1435 -0
  31. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/brands.min.js +6 -0
  32. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/conflict-detection.js +1624 -0
  33. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/conflict-detection.min.js +6 -0
  34. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/fontawesome.js +3883 -0
  35. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/fontawesome.min.js +6 -0
  36. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/regular.js +1032 -0
  37. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/regular.min.js +6 -0
  38. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/solid.js +2285 -0
  39. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/solid.min.js +6 -0
  40. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/v4-shims.js +836 -0
  41. passportd-2.0.0/passportd/static/fontawesome-free-7.3.1-web/js/v4-shims.min.js +6 -0
  42. passportd-2.0.0/passportd/static/img/favicon.png +0 -0
  43. passportd-2.0.0/passportd/static/js/jquery-v3.7.1.min.js +2 -0
  44. passportd-2.0.0/passportd/templates/authorize.j2 +82 -0
  45. passportd-2.0.0/passportd/templates/error.j2 +64 -0
  46. passportd-2.0.0/passportd/templates/layout.j2 +255 -0
  47. passportd-2.0.0/passportd/templates/oauth2go.j2 +174 -0
  48. passportd-2.0.0/passportd/templates/oidc_client.j2 +427 -0
  49. passportd-2.0.0/passportd/templates/profile.j2 +504 -0
  50. passportd-2.0.0/passportd/templates/signin.j2 +94 -0
  51. passportd-2.0.0/passportd/templates/signup.j2 +71 -0
  52. passportd-2.0.0/passportd/templates/vcode.j2 +43 -0
  53. passportd-2.0.0/passportd/utils/__init__.py +0 -0
  54. passportd-2.0.0/passportd/utils/common.py +595 -0
  55. passportd-2.0.0/passportd/utils/web.py +256 -0
  56. passportd-2.0.0/passportd/version.py +1 -0
  57. passportd-2.0.0/passportd/views/__init__.py +0 -0
  58. passportd-2.0.0/passportd/views/api.py +260 -0
  59. passportd-2.0.0/passportd/views/front.py +406 -0
  60. passportd-2.0.0/passportd/views/oidc.py +208 -0
  61. passportd-2.0.0/passportd/views/root.py +27 -0
  62. passportd-2.0.0/passportd.egg-info/PKG-INFO +143 -0
  63. passportd-2.0.0/passportd.egg-info/SOURCES.txt +72 -0
  64. passportd-2.0.0/passportd.egg-info/dependency_links.txt +1 -0
  65. passportd-2.0.0/passportd.egg-info/entry_points.txt +2 -0
  66. passportd-2.0.0/passportd.egg-info/not-zip-safe +1 -0
  67. passportd-2.0.0/passportd.egg-info/requires.txt +15 -0
  68. passportd-2.0.0/passportd.egg-info/top_level.txt +1 -0
  69. passportd-2.0.0/setup.cfg +30 -0
  70. passportd-2.0.0/setup.py +75 -0
  71. passportd-2.0.0/tests/test_basis_common.py +185 -0
  72. passportd-2.0.0/tests/test_basis_errors.py +135 -0
  73. passportd-2.0.0/tests/test_util_common.py +305 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ recursive-include passportd/templates *
2
+ recursive-include passportd/static *
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.1
2
+ Name: passportd
3
+ Version: 2.0.0
4
+ Summary: Unified login module for my personal application
5
+ Home-page: https://github.com/staugur/passportd
6
+ Author: Hiroshi.tao
7
+ Author-email: me@tcw.im
8
+ License: Apache-2.0
9
+ Project-URL: Code, https://github.com/staugur/passportd
10
+ Project-URL: Issue tracker, https://github.com/staugur/passportd/issues
11
+ Project-URL: Documentation, https://passportd.readthedocs.io/
12
+ Project-URL: Releases, https://github.com/staugur/passportd/releases
13
+ Keywords: sso,auth,signup,signin
14
+ Platform: any
15
+ Classifier: Development Status :: 4 - Beta
16
+ Classifier: Framework :: Flask
17
+ Classifier: Environment :: Web Environment
18
+ Classifier: Intended Audience :: Developers
19
+ Classifier: Intended Audience :: System Administrators
20
+ Classifier: Operating System :: OS Independent
21
+ Classifier: Programming Language :: Python :: 3
22
+ Classifier: Programming Language :: Python :: 3.10
23
+ Classifier: Programming Language :: Python :: 3.11
24
+ Classifier: Programming Language :: Python :: 3.12
25
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
26
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
+ Requires-Python: >=3.10
28
+ Description-Content-Type: text/markdown
29
+ License-File: LICENSE
30
+ Requires-Dist: Flask==3.1.3
31
+ Requires-Dist: Flask-PluginKit==3.10.2
32
+ Requires-Dist: peewee[psycopg3]==4.2.6
33
+ Requires-Dist: peewee[mysql]==4.2.6
34
+ Requires-Dist: shortuuid==1.0.13
35
+ Requires-Dist: authlib==1.7.2
36
+ Requires-Dist: joserfc==1.7.4
37
+ Requires-Dist: requests==2.34.2
38
+ Requires-Dist: redis==5.3.1
39
+ Requires-Dist: typing_extensions>=4.0.0; python_version < "3.11"
40
+ Requires-Dist: gunicorn==26.0.0
41
+ Requires-Dist: gevent==26.7.0
42
+ Requires-Dist: setproctitle==1.3.7
43
+
44
+ # passportd
45
+
46
+ 认证、授权、统一登录 — 基于 Flask 的 SSO 单点登录服务。
47
+
48
+ [![Python](https://img.shields.io/badge/python-%3E%3D3.10-blue)](https://www.python.org/)
49
+ [![License](https://img.shields.io/badge/license-Apache%202.0-green)](LICENSE)
50
+ [![Docs](https://img.shields.io/badge/docs-passportd.readthedocs.io-brightgreen)](https://passportd.readthedocs.io/)
51
+ [![codecov](https://codecov.io/gh/staugur/passportd/branch/master/graph/badge.svg)](https://codecov.io/gh/staugur/passportd)
52
+
53
+ ## 核心功能
54
+
55
+ - **本地账号系统**:支持用户名、手机号、邮箱注册与登录
56
+ - **OAuth2 第三方登录**:内置 GitHub 和 Gitee OAuth2 登录插件
57
+ - **OpenID Connect Provider**:支持 OIDC Server 模式,可作为独立 SSO 服务
58
+ - **插件扩展**:基于 Flask-PluginKit 的插件架构,方便扩展更多登录方式
59
+ - **多数据库支持**:SQLite(开发) / MySQL / PostgreSQL(生产)
60
+ - **JWT 认证**:支持 HMAC-SHA256 和 RS256 双算法 JWT 签名
61
+ - **RESTful API**:提供完整的注册、登录、用户信息、OIDC 客户端管理接口
62
+
63
+ ## 快速开始
64
+
65
+ ### pip 安装
66
+
67
+ ```shell
68
+ # 安装
69
+ pip install passportd
70
+
71
+ # 确保 Redis 已启动
72
+
73
+ # 启动
74
+ passportd run
75
+ ```
76
+
77
+ 访问 http://localhost:10030/ 即可看到登录页面。
78
+
79
+ ### Docker 运行
80
+
81
+ ```shell
82
+ # 拉取镜像
83
+ docker pull staugur/passportd:latest
84
+
85
+ # 启动容器(需要先启动 Redis)
86
+ docker run -d \
87
+ --name passportd \
88
+ -p 10030:10030 \
89
+ -e PASSPORT_REDIS_URI="redis://:pwd@redis:6379/0" \
90
+ -e PASSPORT_DB_URI="sqlite:///app/data/passportd.db" \
91
+ -e PASSPORT_ENV="production" \
92
+ -v passportd-data:/app/data \
93
+ staugur/passportd:latest
94
+ ```
95
+
96
+ 也可以通过 `docker-compose.yml` 一键启动(包含 Redis):
97
+
98
+ ```yaml
99
+ version: "3.8"
100
+ services:
101
+ redis:
102
+ image: redis:7-alpine
103
+ restart: unless-stopped
104
+ volumes:
105
+ - redis-data:/data
106
+
107
+ passportd:
108
+ image: staugur/passportd:latest
109
+ restart: unless-stopped
110
+ ports:
111
+ - "10030:10030"
112
+ environment:
113
+ - PASSPORT_ENV=production
114
+ - PASSPORT_REDIS_URI=redis://redis:6379/0
115
+ - PASSPORT_DB_URI=sqlite:///app/data/passportd.db
116
+ volumes:
117
+ - passportd-data:/app/data
118
+ depends_on:
119
+ - redis
120
+
121
+ volumes:
122
+ redis-data:
123
+ passportd-data:
124
+ ```
125
+
126
+ ## 文档
127
+
128
+ 完整文档请访问 [passportd.readthedocs.io](https://passportd.readthedocs.io/)
129
+
130
+ 本地构建文档:
131
+
132
+ ```shell
133
+ pip install -r requirements/docs.txt
134
+ cd docs && make html
135
+ ```
136
+
137
+ ## 许可证
138
+
139
+ Apache License 2.0
140
+
141
+ ## 备注
142
+
143
+ 沿用 1.x 设计思路,使用 OIDC 标准协议重构实现,除核心代码外大部分使用 AI 生成。
@@ -0,0 +1,100 @@
1
+ # passportd
2
+
3
+ 认证、授权、统一登录 — 基于 Flask 的 SSO 单点登录服务。
4
+
5
+ [![Python](https://img.shields.io/badge/python-%3E%3D3.10-blue)](https://www.python.org/)
6
+ [![License](https://img.shields.io/badge/license-Apache%202.0-green)](LICENSE)
7
+ [![Docs](https://img.shields.io/badge/docs-passportd.readthedocs.io-brightgreen)](https://passportd.readthedocs.io/)
8
+ [![codecov](https://codecov.io/gh/staugur/passportd/branch/master/graph/badge.svg)](https://codecov.io/gh/staugur/passportd)
9
+
10
+ ## 核心功能
11
+
12
+ - **本地账号系统**:支持用户名、手机号、邮箱注册与登录
13
+ - **OAuth2 第三方登录**:内置 GitHub 和 Gitee OAuth2 登录插件
14
+ - **OpenID Connect Provider**:支持 OIDC Server 模式,可作为独立 SSO 服务
15
+ - **插件扩展**:基于 Flask-PluginKit 的插件架构,方便扩展更多登录方式
16
+ - **多数据库支持**:SQLite(开发) / MySQL / PostgreSQL(生产)
17
+ - **JWT 认证**:支持 HMAC-SHA256 和 RS256 双算法 JWT 签名
18
+ - **RESTful API**:提供完整的注册、登录、用户信息、OIDC 客户端管理接口
19
+
20
+ ## 快速开始
21
+
22
+ ### pip 安装
23
+
24
+ ```shell
25
+ # 安装
26
+ pip install passportd
27
+
28
+ # 确保 Redis 已启动
29
+
30
+ # 启动
31
+ passportd run
32
+ ```
33
+
34
+ 访问 http://localhost:10030/ 即可看到登录页面。
35
+
36
+ ### Docker 运行
37
+
38
+ ```shell
39
+ # 拉取镜像
40
+ docker pull staugur/passportd:latest
41
+
42
+ # 启动容器(需要先启动 Redis)
43
+ docker run -d \
44
+ --name passportd \
45
+ -p 10030:10030 \
46
+ -e PASSPORT_REDIS_URI="redis://:pwd@redis:6379/0" \
47
+ -e PASSPORT_DB_URI="sqlite:///app/data/passportd.db" \
48
+ -e PASSPORT_ENV="production" \
49
+ -v passportd-data:/app/data \
50
+ staugur/passportd:latest
51
+ ```
52
+
53
+ 也可以通过 `docker-compose.yml` 一键启动(包含 Redis):
54
+
55
+ ```yaml
56
+ version: "3.8"
57
+ services:
58
+ redis:
59
+ image: redis:7-alpine
60
+ restart: unless-stopped
61
+ volumes:
62
+ - redis-data:/data
63
+
64
+ passportd:
65
+ image: staugur/passportd:latest
66
+ restart: unless-stopped
67
+ ports:
68
+ - "10030:10030"
69
+ environment:
70
+ - PASSPORT_ENV=production
71
+ - PASSPORT_REDIS_URI=redis://redis:6379/0
72
+ - PASSPORT_DB_URI=sqlite:///app/data/passportd.db
73
+ volumes:
74
+ - passportd-data:/app/data
75
+ depends_on:
76
+ - redis
77
+
78
+ volumes:
79
+ redis-data:
80
+ passportd-data:
81
+ ```
82
+
83
+ ## 文档
84
+
85
+ 完整文档请访问 [passportd.readthedocs.io](https://passportd.readthedocs.io/)
86
+
87
+ 本地构建文档:
88
+
89
+ ```shell
90
+ pip install -r requirements/docs.txt
91
+ cd docs && make html
92
+ ```
93
+
94
+ ## 许可证
95
+
96
+ Apache License 2.0
97
+
98
+ ## 备注
99
+
100
+ 沿用 1.x 设计思路,使用 OIDC 标准协议重构实现,除核心代码外大部分使用 AI 生成。
@@ -0,0 +1,141 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Copyright 2021 Hiroshi.tao
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ """
17
+
18
+ from .basis.common import auto_create_data_dir, raise_version
19
+ from .utils.common import auto_init_rsa_key
20
+
21
+ __author__ = "Hiroshi.tao <me@tcw.im>"
22
+ __date__ = "2021-06-25"
23
+
24
+ raise_version()
25
+ auto_create_data_dir()
26
+ auto_init_rsa_key()
27
+
28
+
29
+ def create_app():
30
+ """创建并配置 Flask 应用实例。
31
+
32
+ 初始化应用的核心组件,包括:
33
+ - 配置加载与代理信任
34
+ - 蓝图注册(根路由、插件管理器)
35
+ - OAuth2 客户端和 OIDC 服务端初始化
36
+ - 插件加载(GitHub、Gitee OAuth2)
37
+ - 请求前后处理钩子(数据库连接、用户状态解析、错误处理)
38
+
39
+ :returns: 配置完成的 Flask 应用实例
40
+ :rtype: flask.Flask
41
+ """
42
+ from flask import Flask, g, request, jsonify, render_template
43
+ from flask_pluginkit import PluginManager, JsonResponse, blueprint
44
+ from werkzeug.middleware.proxy_fix import ProxyFix
45
+
46
+ from .basis.vars import PROC_NAME
47
+ from .basis.conf import config
48
+ from .basis.errors import ApiError
49
+ from .basis.common import is_true, new_res
50
+ from .utils.common import logger
51
+ from .utils.web import parse_user_state
52
+ from .models.model import db
53
+ from .views.root import root
54
+ from .views.oidc import server as OIDCServer
55
+ from .libs.oidc import OIDCClient, oidc_save_token
56
+ from .libs.interface import OAuthClient
57
+
58
+ try:
59
+ from setproctitle import setproctitle
60
+
61
+ setproctitle(PROC_NAME)
62
+ except ImportError:
63
+ pass
64
+
65
+ app = Flask(__name__)
66
+ app.response_class = JsonResponse
67
+ app.config.update(config)
68
+ if is_true(config.get("TRUST_PROXY")):
69
+ app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_for=1)
70
+ app.register_blueprint(root, url_prefix=config.get("URI_PREFIX"))
71
+ app.register_blueprint(blueprint, url_prefix="/pluginmanager")
72
+
73
+ OAuthClient.init_app(app)
74
+ OIDCServer.init_app(
75
+ app,
76
+ query_client=lambda client_id: OIDCClient(client_id),
77
+ save_token=oidc_save_token,
78
+ )
79
+ PluginManager(
80
+ app,
81
+ plugin_packages=(
82
+ "passportd.modules.oauth2_github",
83
+ "passportd.modules.oauth2_gitee",
84
+ "passportd.modules.oauth2_weibo",
85
+ "passportd.modules.oauth2_qq",
86
+ ), # type: ignore
87
+ )
88
+
89
+ @app.before_request
90
+ def br():
91
+ """在每个请求前解析用户登录态。"""
92
+ #: 每个请求从连接池获取一个数据库连接,复用当前线程已有连接。
93
+ db.connect(reuse_if_open=True)
94
+ #: signin:bool -- 用户是否已登录
95
+ #: user:dict -- uid, account 等用户信息
96
+ g.signin, g.user = parse_user_state()
97
+
98
+ @app.after_request
99
+ def ar(response):
100
+ """在每个响应后追加 CORS 头,允许 Authorization 请求头跨域访问。"""
101
+ response.headers["Access-Control-Allow-Headers"] = "Authorization"
102
+ return response
103
+
104
+ @app.teardown_request
105
+ def _db_close(exc=None):
106
+ """请求结束后将连接归还连接池,确保不泄露。"""
107
+ if not db.is_closed():
108
+ db.close()
109
+
110
+ @app.errorhandler(500)
111
+ @app.errorhandler(404)
112
+ @app.errorhandler(403)
113
+ def handle_error(e):
114
+ """统一 HTTP 错误处理:500 记录日志,API 返回 JSON,页面返回 error.j2 模板。
115
+
116
+ :param e: HTTP 异常对象
117
+ :type e: werkzeug.exceptions.HTTPException
118
+ :returns: JSON 响应(API 路径)或 HTML 错误页面
119
+ """
120
+ if getattr(e, "code", None) == 500:
121
+ logger.error(e, exc_info=True)
122
+ code = e.code
123
+ name = e.name
124
+ if "/api/" in request.path:
125
+ return jsonify(new_res()), code
126
+ else:
127
+ return render_template("error.j2", code=code, name=name), code
128
+
129
+ @app.errorhandler(ApiError)
130
+ def handle_api_error(e):
131
+ """捕获 ApiError 异常,返回结构化 JSON 错误响应。
132
+
133
+ :param e: ApiError 异常实例
134
+ :type e: passportd.basis.errors.ApiError
135
+ :returns: JSON 错误响应
136
+ """
137
+ response = jsonify(e.to_dict())
138
+ response.status_code = e.status_code
139
+ return response
140
+
141
+ return app
File without changes