canaille 0.0.1__py3-none-any.whl

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 (70) hide show
  1. Canaille-0.0.1.dist-info/LICENSE.md +19 -0
  2. Canaille-0.0.1.dist-info/METADATA +92 -0
  3. Canaille-0.0.1.dist-info/RECORD +70 -0
  4. Canaille-0.0.1.dist-info/WHEEL +5 -0
  5. Canaille-0.0.1.dist-info/top_level.txt +1 -0
  6. canaille/__init__.py +197 -0
  7. canaille/account.py +72 -0
  8. canaille/admin/__init__.py +0 -0
  9. canaille/admin/authorizations.py +24 -0
  10. canaille/admin/clients.py +208 -0
  11. canaille/admin/tokens.py +20 -0
  12. canaille/conf/config.sample.toml +61 -0
  13. canaille/conf/oauth-authorization-server.sample.json +29 -0
  14. canaille/conf/openid-configuration.sample.json +64 -0
  15. canaille/consents.py +34 -0
  16. canaille/flaskutils.py +42 -0
  17. canaille/forms.py +76 -0
  18. canaille/ldaputils.py +259 -0
  19. canaille/models.py +269 -0
  20. canaille/oauth.py +138 -0
  21. canaille/oauth2utils.py +285 -0
  22. canaille/static/css/base.css +26 -0
  23. canaille/static/datatables/French.json +32 -0
  24. canaille/static/datatables/dataTables.semanticui.min.css +1 -0
  25. canaille/static/datatables/dataTables.semanticui.min.js +12 -0
  26. canaille/static/datatables/jquery.dataTables.min.css +1 -0
  27. canaille/static/datatables/jquery.dataTables.min.js +181 -0
  28. canaille/static/fomanticui/semantic.min.css +404 -0
  29. canaille/static/fomanticui/semantic.min.js +11 -0
  30. canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.eot +0 -0
  31. canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.svg +3570 -0
  32. canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.ttf +0 -0
  33. canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.woff +0 -0
  34. canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.woff2 +0 -0
  35. canaille/static/fomanticui/themes/default/assets/fonts/icons.eot +0 -0
  36. canaille/static/fomanticui/themes/default/assets/fonts/icons.otf +0 -0
  37. canaille/static/fomanticui/themes/default/assets/fonts/icons.svg +4938 -0
  38. canaille/static/fomanticui/themes/default/assets/fonts/icons.ttf +0 -0
  39. canaille/static/fomanticui/themes/default/assets/fonts/icons.woff +0 -0
  40. canaille/static/fomanticui/themes/default/assets/fonts/icons.woff2 +0 -0
  41. canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.eot +0 -0
  42. canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.svg +803 -0
  43. canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.ttf +0 -0
  44. canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.woff +0 -0
  45. canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.woff2 +0 -0
  46. canaille/static/fomanticui/themes/default/assets/images/flags.png +0 -0
  47. canaille/static/jquery/jquery.min.js +2 -0
  48. canaille/static/js/base.js +4 -0
  49. canaille/static/js/users.js +16 -0
  50. canaille/templates/admin/authorization_list.html +33 -0
  51. canaille/templates/admin/authorization_view.html +27 -0
  52. canaille/templates/admin/client_add.html +20 -0
  53. canaille/templates/admin/client_edit.html +37 -0
  54. canaille/templates/admin/client_list.html +35 -0
  55. canaille/templates/admin/token_list.html +33 -0
  56. canaille/templates/admin/token_view.html +27 -0
  57. canaille/templates/authorize.html +33 -0
  58. canaille/templates/base.html +91 -0
  59. canaille/templates/consent_list.html +70 -0
  60. canaille/templates/error.html +13 -0
  61. canaille/templates/fomanticui.j2 +61 -0
  62. canaille/templates/home.html +7 -0
  63. canaille/templates/login.html +41 -0
  64. canaille/templates/profile.html +25 -0
  65. canaille/templates/token_list.html +69 -0
  66. canaille/tokens.py +36 -0
  67. canaille/translations/fr/LC_MESSAGES/messages.mo +0 -0
  68. canaille/translations/fr/LC_MESSAGES/messages.po +340 -0
  69. canaille/translations/messages.pot +328 -0
  70. canaille/well_known.py +17 -0
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2020 by Yaal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.1
2
+ Name: Canaille
3
+ Version: 0.0.1
4
+ Summary: Simple OAuth2/OpenID Connect server based upon LDAP
5
+ Home-page: https://gitlab.com/yaal/canaille
6
+ Maintainer: Yaal
7
+ Maintainer-email: contact@yaal.fr
8
+ License: MIT
9
+ Project-URL: Code, https://gitlab.com/yaal/canaille
10
+ Project-URL: Issue Tracker, https://gitlab.com/yaal/canaille/-/issues
11
+ Platform: UNKNOWN
12
+ Classifier: Environment :: Web Environment
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Topic :: System :: Systems Administration :: Authentication/Directory
17
+ Classifier: Topic :: System :: Systems Administration :: Authentication/Directory :: LDAP
18
+ Requires-Python: >=3.6
19
+ Description-Content-Type: text/markdown
20
+ Requires-Dist: authlib
21
+ Requires-Dist: email-validator
22
+ Requires-Dist: flask
23
+ Requires-Dist: flask-babel
24
+ Requires-Dist: flask-wtf
25
+ Requires-Dist: python-ldap
26
+ Requires-Dist: sentry-sdk[flask]
27
+ Requires-Dist: toml
28
+
29
+ ⚠ Canaille is under development. Do not use in production yet. ⚠
30
+
31
+ # Canaille
32
+
33
+ Canaille is a French word meaning *rascal*, and is pronounced approximatively **Can I?**,
34
+ as in *Can I access this data please?*. It is a simple OpenID Connect provider based upon OpenLDAP.
35
+
36
+ It aims to be very light, simple to install and simple to maintain. Its main features are :
37
+ - OAuth/OpenID Connect support;
38
+ - Authentication against a LDAP directory;
39
+ - No additional database required. Everything is stored in your OpenLDAP server;
40
+ - The code is easy to read and easy to edit in case you want to write a patch.
41
+
42
+ ## Install
43
+
44
+ First you need to install the schemas into your LDAP server. There are several ways to achieve this:
45
+
46
+ ### LDAP schemas
47
+
48
+ #### Option 1: Add the schema into your filesystem
49
+
50
+ ```bash
51
+ test -d /etc/openldap/schema && sudo cp schema/* /etc/openldap/schema
52
+ test -d /etc/ldap/schema && sudo cp schema/* /etc/ldap/schema
53
+ sudo service slapd restart
54
+ ```
55
+
56
+ #### Option 2: Use slapadd
57
+
58
+ ```bash
59
+ sudo slapadd -n0 -l schema/*.ldif
60
+ ```
61
+
62
+ ### Web interface
63
+
64
+ Then you can deploy the code either by copying the git repository or installing the pip package:
65
+
66
+ ```bash
67
+ pip install canaille
68
+ ```
69
+
70
+ Finally you have to run the website in a WSGI server:
71
+
72
+ ```bash
73
+ pip install gunicorn
74
+ gunicorn "canaille:create_app()"
75
+ ```
76
+
77
+ ## Contribute
78
+
79
+ Contributions are welcome!
80
+ To run the tests, you just need to run `tox`.
81
+
82
+ To try a development environment, you can run the docker image and then open https://127.0.0.1:5000
83
+ You can then connect with user *admin* and password *admin* to access an admin account, or user *user* and password *user* for a regular one.
84
+
85
+ ```bash
86
+ cp canaille/conf/config.sample.toml canaille/conf/config.toml
87
+ cp canaille/conf/oauth-authorization-server.sample.json canaille/conf/oauth-authorization-server.json
88
+ cp canaille/conf/openid-configuration.sample.json canaille/conf/openid-configuration.json
89
+ docker-compose up
90
+ ```
91
+
92
+
@@ -0,0 +1,70 @@
1
+ canaille/__init__.py,sha256=nH_5AxaaNvDnfNlUJyN8F5FJOoolQ5mSTfrxwKKcP8k,6108
2
+ canaille/account.py,sha256=yixiHTw_glJDOwAsMYjtAgFtOrAJ10DkgP_c0vIqfJU,2151
3
+ canaille/consents.py,sha256=u2ai6afpQylqFS4-ylQGZEj3VIsUhydSXdca9NVtU28,1035
4
+ canaille/flaskutils.py,sha256=9oumsgo6u1VUXxHLkum-WXt05c3cdXwIOZofIzMnZc4,927
5
+ canaille/forms.py,sha256=FqR4FbTCmfd9ylwqWW8K3vFtYVlPVr6SV4r3T8_AAJw,2455
6
+ canaille/ldaputils.py,sha256=NtmEUwwQ8cvVXyUTJbx6OhWMPPXGU2JpqwosCzq4orI,7992
7
+ canaille/models.py,sha256=jkVNmR59uY9mws-a1kQmeQo5tlQ1YWnVgGe37MSyTvA,7548
8
+ canaille/oauth.py,sha256=xiQRG0-QJko3dPEbiRcb0uKies-Fz2JicPEfm2kGDCc,4056
9
+ canaille/oauth2utils.py,sha256=Vuk1D3i8CMpogP60iaMCi97V4PNeB-zcMOFjDCKqItg,9483
10
+ canaille/tokens.py,sha256=88gZ165HxG4bwnU5hn37C-K1sSTtnf2p318aAp8UyUQ,1093
11
+ canaille/well_known.py,sha256=_gctoGC4f2w8RDNboZ65bSi-zUCczJzKmhUzgc6_oDc,443
12
+ canaille/admin/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ canaille/admin/authorizations.py,sha256=Bxtgx5CbAsYBnxC13d2mCVUuJ_7CfP2xmATSfCa6tGE,665
14
+ canaille/admin/clients.py,sha256=cL2kpZB1H0CPm4EQB6NJ_jX4sAsK6XkbQQduYiK-4WU,7645
15
+ canaille/admin/tokens.py,sha256=f0konokNeoKi1eR2mo7j6jpxZZdYGrv10lVEkuaMXEQ,523
16
+ canaille/conf/config.sample.toml,sha256=3PtQEEsv-Sz2OqCJmt9S1QNEkYuonKQ_h9oDhHch_qY,1898
17
+ canaille/conf/oauth-authorization-server.sample.json,sha256=J-fGflF0fbwthbsiCruIB-XYS0DcNSQOg7nFmIM8LJI,926
18
+ canaille/conf/openid-configuration.sample.json,sha256=R-phkWPN___hxSbNxQX8RgUa3NH0b-xAgZM2550RJ64,2174
19
+ canaille/static/css/base.css,sha256=daN_6MGXJ__1IdY62e6fenxbtqrnkvm1NXVxM26ahxo,471
20
+ canaille/static/datatables/French.json,sha256=Rt3I8_rVRREOOIgr3jZIoelsBFlQE5Fe_wOEZiwdV2c,1049
21
+ canaille/static/datatables/dataTables.semanticui.min.css,sha256=SJpV2BABjesndlj0vdtRpj4LE4eigoPZVjTNQ6cMsy8,2625
22
+ canaille/static/datatables/dataTables.semanticui.min.js,sha256=L9t1ZuJNPnJ2fHvdL9PFpdW3IC3jQFx-0oXN520PVFM,3629
23
+ canaille/static/datatables/jquery.dataTables.min.css,sha256=NsjPUPa1DUo51N8aw3uVf4ygtyEBB8DzDAfH2eRJTUs,13892
24
+ canaille/static/datatables/jquery.dataTables.min.js,sha256=_7zo27Ll_hVKhCsE-y8m2SS5bhFPEQFheTCL87HuumA,84647
25
+ canaille/static/fomanticui/semantic.min.css,sha256=2-dssJtgusl_DZZZ8gF9ayAgRzcewXQsaP86E4Ul-ss,1362383
26
+ canaille/static/fomanticui/semantic.min.js,sha256=yibQd6vg4YwSTFUcgd-MwPALTUAVCKTjh4jMON4j-Gk,360427
27
+ canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.eot,sha256=gR4VdxhAZhCM7KhtWmJKkBIwPfUgbFywsn1eP-3LpaM,133034
28
+ canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.svg,sha256=AjQLoXidCbzFB1Y8icSoR7pTe7gTIIJ3elBRnPdLy1s,715890
29
+ canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.ttf,sha256=oMFFOCrP5Zdn0Uh9ZQXzSWZbaFwohc2RtiHRQaKSsPg,132728
30
+ canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.woff,sha256=6DbF1d_BPhgXDFvd77T9rHxlrArUOktDGs3EqYhlomA,89824
31
+ canaille/static/fomanticui/themes/default/assets/fonts/brand-icons.woff2,sha256=XZGQKSrN1Iug_DUID350SPPN8NeRmaTSPw9JtTQf3yk,76612
32
+ canaille/static/fomanticui/themes/default/assets/fonts/icons.eot,sha256=MlAXJ7sj_HdhWx7Ha18pjsIhmMDz1qPn1v5Kw88xXbk,202902
33
+ canaille/static/fomanticui/themes/default/assets/fonts/icons.otf,sha256=vcXQufOXvoPohsdLAUHRlUqkOEs1nc5JgpmUxKLh978,93888
34
+ canaille/static/fomanticui/themes/default/assets/fonts/icons.svg,sha256=52hTmAh1f37sIPCcnumzaw5sVjbpDJ7ml5lcu8buBVU,897426
35
+ canaille/static/fomanticui/themes/default/assets/fonts/icons.ttf,sha256=Ix1ozvvGhG-3jK_KhGdAGk83HiJkGGYuiQDj1SrcAKs,202616
36
+ canaille/static/fomanticui/themes/default/assets/fonts/icons.woff,sha256=NS-OrSgEGd2VPSvHhDOGu5Qw6BUsLb0sLSSU9rlWfuc,103300
37
+ canaille/static/fomanticui/themes/default/assets/fonts/icons.woff2,sha256=f00_0KcF2_hAMpiq2R1d5pcua11TYGjrqLJJVKWgqMc,79444
38
+ canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.eot,sha256=AfRBb121ni3WtvvZ3DIzbZnbGPfrYjpJ9YTQSv0nlHM,34390
39
+ canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.svg,sha256=hl_SPeE7GSAoDhgjWVeeCe_3oJ6yRnbs7sdVKDhVn1U,144322
40
+ canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.ttf,sha256=8yuUopBg3-wMeXod_hETV-sdaC8aykrEuVzw9MPiv6k,34092
41
+ canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.woff,sha256=ji2WGYcRtjdS5fL1mHuP_FNt9hVqBlvb4oy3IxyteT0,16800
42
+ canaille/static/fomanticui/themes/default/assets/fonts/outline-icons.woff2,sha256=aoyOnh5_aSwhrxlW3hY_PQJneOZEn-k6CaZxhHyhrmU,13584
43
+ canaille/static/fomanticui/themes/default/assets/images/flags.png,sha256=lNXH8WYTAcSm3Ekdct1VmgYgzZF6gm8N8bAju5bqnd0,28123
44
+ canaille/static/jquery/jquery.min.js,sha256=FgpCb_KJQlLNfOu91ta32o_NMZxltwRo8QtmkMRdAu8,86927
45
+ canaille/static/js/base.js,sha256=FRF2aY3_vnTx3Fj5GcPnLAXgum4JvX2-ck3tgd_ULG8,93
46
+ canaille/static/js/users.js,sha256=auJrLvGRLS4P451DXKfbn2Fg28YXShtHf2q9Stg5yno,432
47
+ canaille/templates/authorize.html,sha256=nlfj5z8bJhaqP8xCDNdFYDrjivKLenzSEPh3hce00us,1295
48
+ canaille/templates/base.html,sha256=qBRPAix1PKelSpXXZyAOYWH-FEVpjiDNdsWhZA4QTJI,3704
49
+ canaille/templates/consent_list.html,sha256=2BOKqbIZMGFWPNJ3eufOSFlWAWvdXpKGYqVHi9Bb0Rg,2839
50
+ canaille/templates/error.html,sha256=QfF7keTyDBEZZ0DqcRm7yVeJ-Kk-nbVk9020uF2JSwM,264
51
+ canaille/templates/fomanticui.j2,sha256=xrFXIUUE_gYOjWMqCEog1Ktb6ywK7qHB4aUMMDgsUgE,1565
52
+ canaille/templates/home.html,sha256=L70TrEUWEVwsFMFXI9EbHXSNYy8QgvHtHM3nOMgvL4M,106
53
+ canaille/templates/login.html,sha256=5KPTIU2T3CJs2EojVg4bv-wBPBWo9x4gMmcnk0esc7U,1282
54
+ canaille/templates/profile.html,sha256=VgGOhjZcQp7Kdl4CXJT1KZcHnUdyGJGthCeaCN2SWmc,686
55
+ canaille/templates/token_list.html,sha256=EgNEEG1iqvSBmBH3SHItX_aaFKFbLrK97C7hciqBheQ,2858
56
+ canaille/templates/admin/authorization_list.html,sha256=wROv_vEIBOhvmLI80E9vxnOZoJn7vYnzULaIn9PuC4w,1233
57
+ canaille/templates/admin/authorization_view.html,sha256=9kIUGRWsv6sYq1fLDYYFkk5MsKljJ6NRPdOFHyXZ3l0,801
58
+ canaille/templates/admin/client_add.html,sha256=h6IhZbla9gE1Vp7nk4piBSro_BDq7b-IIqvC7ev0rWM,567
59
+ canaille/templates/admin/client_edit.html,sha256=K7ui58kad1FB8foi2qH143qeUrDRN71benbtkVhkqZo,1365
60
+ canaille/templates/admin/client_list.html,sha256=Wgd_4f6jD_dslUGeFc31FXjq-7cynkOWomhD1SODGyM,1185
61
+ canaille/templates/admin/token_list.html,sha256=rMztwC3Pr0BNIqyRdbspeod9hzTvbV9vpxzUUcbAtBo,1167
62
+ canaille/templates/admin/token_view.html,sha256=4T-r2zCGl0FKTxhR4cYFbFZdQxPH9DT3Ady3G0TaDt8,761
63
+ canaille/translations/messages.pot,sha256=TKotVvILXaVOOhj_3AMb7ImqoQbac5E-wbOg4CO6rNo,6562
64
+ canaille/translations/fr/LC_MESSAGES/messages.mo,sha256=KOGDaGfce5HdvoUSlVOarvrLepaFwRuqMbZ_I9N-WSk,4232
65
+ canaille/translations/fr/LC_MESSAGES/messages.po,sha256=NZgbkOvEd1PuI-GjahgdM5O4cLzDZMWblzQdgo-59H4,8226
66
+ Canaille-0.0.1.dist-info/LICENSE.md,sha256=UR_cMK98RpwF1LowncFEah4jft4uJsIMr1EvkpXmFO4,1051
67
+ Canaille-0.0.1.dist-info/METADATA,sha256=BMkaYacy-_q9XmFFU6p4J42KwivFggpKApoi5kmiKdI,2911
68
+ Canaille-0.0.1.dist-info/WHEEL,sha256=EVRjI69F5qVjm_YgqcTXPnTAv3BfSUr0WVAHuSP3Xoo,92
69
+ Canaille-0.0.1.dist-info/top_level.txt,sha256=YrVDHbkt3FE8oH1GwYrrT6sFbGorxLmzod00bnQmofM,9
70
+ Canaille-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.35.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ canaille
canaille/__init__.py ADDED
@@ -0,0 +1,197 @@
1
+ import ldap
2
+ import os
3
+ import toml
4
+
5
+ import canaille.admin
6
+ import canaille.admin.tokens
7
+ import canaille.admin.authorizations
8
+ import canaille.admin.clients
9
+ import canaille.consents
10
+ import canaille.oauth
11
+ import canaille.account
12
+ import canaille.tokens
13
+ import canaille.well_known
14
+
15
+ from cryptography.hazmat.primitives import serialization as crypto_serialization
16
+ from cryptography.hazmat.primitives.asymmetric import rsa
17
+ from cryptography.hazmat.backends import default_backend as crypto_default_backend
18
+
19
+ from flask import Flask, g, request, render_template
20
+ from flask_babel import Babel
21
+
22
+ from .flaskutils import current_user
23
+ from .ldaputils import LDAPObject
24
+ from .oauth2utils import config_oauth
25
+ from .models import User, Token, AuthorizationCode, Client, Consent
26
+
27
+ try: # pragma: no cover
28
+ import sentry_sdk
29
+ from sentry_sdk.integrations.flask import FlaskIntegration
30
+
31
+ SENTRY = True
32
+ except Exception:
33
+ SENTRY = False
34
+
35
+
36
+ def create_app(config=None):
37
+ app = Flask(__name__)
38
+ dir_path = os.path.dirname(os.path.realpath(__file__))
39
+
40
+ app.config.from_mapping(
41
+ {
42
+ "SESSION_COOKIE_NAME": "canaille",
43
+ "OAUTH2_REFRESH_TOKEN_GENERATOR": True,
44
+ }
45
+ )
46
+ if config:
47
+ app.config.from_mapping(config)
48
+ elif "CONFIG" in os.environ:
49
+ app.config.from_mapping(toml.load(os.environ.get("CONFIG")))
50
+ elif os.path.exists(os.path.join(dir_path, "conf", "config.toml")):
51
+ app.config.from_mapping(
52
+ toml.load(os.path.join(dir_path, "conf", "config.toml"))
53
+ )
54
+ else:
55
+ raise Exception(
56
+ "No configuration file found. "
57
+ "Either create conf/config.toml or set the 'CONFIG' variable environment."
58
+ )
59
+
60
+ setup_dev_keypair(app)
61
+
62
+ if not os.path.exists(app.config["JWT"]["PUBLIC_KEY"]) or not os.path.exists(
63
+ app.config["JWT"]["PRIVATE_KEY"]
64
+ ):
65
+ raise Exception("Invalid keypair")
66
+ setup_app(app)
67
+
68
+ return app
69
+
70
+
71
+ def setup_dev_keypair(app):
72
+ if not os.environ.get("FLASK_ENV") == "development":
73
+ return
74
+
75
+ if os.path.exists(app.config["JWT"]["PUBLIC_KEY"]) or os.path.exists(
76
+ app.config["JWT"]["PRIVATE_KEY"]
77
+ ):
78
+ return
79
+
80
+ key = rsa.generate_private_key(
81
+ backend=crypto_default_backend(), public_exponent=65537, key_size=2048
82
+ )
83
+ private_key = key.private_bytes(
84
+ crypto_serialization.Encoding.PEM,
85
+ crypto_serialization.PrivateFormat.PKCS8,
86
+ crypto_serialization.NoEncryption(),
87
+ )
88
+ public_key = key.public_key().public_bytes(
89
+ crypto_serialization.Encoding.OpenSSH, crypto_serialization.PublicFormat.OpenSSH
90
+ )
91
+
92
+ with open(app.config["JWT"]["PUBLIC_KEY"], "wb") as fd:
93
+ fd.write(public_key)
94
+
95
+ with open(app.config["JWT"]["PRIVATE_KEY"], "wb") as fd:
96
+ fd.write(private_key)
97
+
98
+
99
+ def setup_ldap_tree(app):
100
+ conn = ldap.initialize(app.config["LDAP"]["URI"])
101
+ conn.simple_bind_s(app.config["LDAP"]["BIND_DN"], app.config["LDAP"]["BIND_PW"])
102
+ Token.initialize(conn)
103
+ AuthorizationCode.initialize(conn)
104
+ Client.initialize(conn)
105
+ Consent.initialize(conn)
106
+ conn.unbind_s()
107
+
108
+
109
+ def setup_app(app):
110
+ if SENTRY and app.config.get("SENTRY_DSN"):
111
+ sentry_sdk.init(dsn=app.config["SENTRY_DSN"], integrations=[FlaskIntegration()])
112
+
113
+ try:
114
+ LDAPObject.root_dn = app.config["LDAP"]["ROOT_DN"]
115
+ base = app.config["LDAP"]["USER_BASE"]
116
+ if base.endswith(app.config["LDAP"]["ROOT_DN"]):
117
+ base = base[: -len(app.config["LDAP"]["ROOT_DN"]) - 1]
118
+ User.base = base
119
+
120
+ app.url_map.strict_slashes = False
121
+
122
+ config_oauth(app)
123
+ setup_ldap_tree(app)
124
+ app.register_blueprint(canaille.account.bp)
125
+ app.register_blueprint(canaille.oauth.bp, url_prefix="/oauth")
126
+ app.register_blueprint(canaille.consents.bp, url_prefix="/consent")
127
+ app.register_blueprint(canaille.tokens.bp, url_prefix="/token")
128
+ app.register_blueprint(
129
+ canaille.well_known.bp, url_prefix="/.well-known"
130
+ )
131
+ app.register_blueprint(
132
+ canaille.admin.tokens.bp, url_prefix="/admin/token"
133
+ )
134
+ app.register_blueprint(
135
+ canaille.admin.authorizations.bp, url_prefix="/admin/authorization"
136
+ )
137
+ app.register_blueprint(
138
+ canaille.admin.clients.bp, url_prefix="/admin/client"
139
+ )
140
+
141
+ babel = Babel(app)
142
+
143
+ @babel.localeselector
144
+ def get_locale():
145
+ user = getattr(g, "user", None)
146
+ if user is not None:
147
+ return user.locale
148
+
149
+ if app.config.get("LANGUAGE"):
150
+ return app.config.get("LANGUAGE")
151
+
152
+ return request.accept_languages.best_match(["fr_FR", "en_US"])
153
+
154
+ @babel.timezoneselector
155
+ def get_timezone():
156
+ user = getattr(g, "user", None)
157
+ if user is not None:
158
+ return user.timezone
159
+
160
+ @app.before_request
161
+ def before_request():
162
+ g.ldap = ldap.initialize(app.config["LDAP"]["URI"])
163
+ g.ldap.simple_bind_s(
164
+ app.config["LDAP"]["BIND_DN"], app.config["LDAP"]["BIND_PW"]
165
+ )
166
+
167
+ @app.after_request
168
+ def after_request(response):
169
+ if "ldap" in g:
170
+ g.ldap.unbind_s()
171
+ return response
172
+
173
+ @app.context_processor
174
+ def global_processor():
175
+ return {
176
+ "logo_url": app.config.get("LOGO"),
177
+ "website_name": app.config.get("NAME"),
178
+ "user": current_user(),
179
+ "menu": True,
180
+ }
181
+
182
+ @app.errorhandler(403)
183
+ def unauthorized(e):
184
+ return render_template("error.html", error=403), 403
185
+
186
+ @app.errorhandler(404)
187
+ def page_not_found(e):
188
+ return render_template("error.html", error=404), 404
189
+
190
+ @app.errorhandler(500)
191
+ def server_error(e):
192
+ return render_template("error.html", error=500), 500
193
+
194
+ except Exception as exc:
195
+ if SENTRY and app.config.get("SENTRY_DSN"):
196
+ sentry_sdk.capture_exception(exc)
197
+ raise
canaille/account.py ADDED
@@ -0,0 +1,72 @@
1
+ from flask import Blueprint, request, flash, url_for, current_app
2
+ from flask import render_template, redirect
3
+ from flask_babel import gettext
4
+
5
+ from .forms import LoginForm, ProfileForm
6
+ from .flaskutils import current_user, user_needed
7
+ from .models import User
8
+
9
+
10
+ bp = Blueprint(__name__, "home")
11
+
12
+
13
+ @bp.route("/")
14
+ def index():
15
+ if not current_user():
16
+ return redirect(url_for("canaille.account.login"))
17
+ return redirect(url_for("canaille.account.profile"))
18
+
19
+
20
+ @bp.route("/login", methods=("GET", "POST"))
21
+ def login():
22
+ form = LoginForm(request.form or None)
23
+
24
+ if request.form:
25
+ if not form.validate() or not User.authenticate(
26
+ form.login.data, form.password.data, True
27
+ ):
28
+ flash(gettext("Login failed, please check your information"), "error")
29
+ return render_template("login.html", form=form)
30
+
31
+ return redirect(url_for("canaille.account.index"))
32
+
33
+ return render_template("login.html", form=form)
34
+
35
+
36
+ @bp.route("/logout")
37
+ def logout():
38
+ if current_user():
39
+ current_user().logout()
40
+ return redirect("/")
41
+
42
+
43
+ @bp.route("/profile", methods=("GET", "POST"))
44
+ @user_needed()
45
+ def profile(user):
46
+ claims = current_app.config["JWT"]["MAPPING"]
47
+ data = {
48
+ k.lower(): getattr(user, v)[0]
49
+ if getattr(user, v) and isinstance(getattr(user, v), list)
50
+ else getattr(user, v) or ""
51
+ for k, v in claims.items()
52
+ }
53
+ form = ProfileForm(request.form or None, data=data)
54
+
55
+ if request.form:
56
+ if not form.validate():
57
+ flash(gettext("Profile edition failed."), "error")
58
+
59
+ else:
60
+ for attribute in form:
61
+ model_attribute_name = claims.get(attribute.name.upper())
62
+ if not model_attribute_name or not hasattr(user, model_attribute_name):
63
+ continue
64
+
65
+ user[model_attribute_name] = [attribute.data]
66
+
67
+ if not form.password1.data or user.set_password(form.password1.data):
68
+ flash(gettext("Profile updated successfuly."), "success")
69
+
70
+ user.save()
71
+
72
+ return render_template("profile.html", form=form, menuitem="profile")
File without changes
@@ -0,0 +1,24 @@
1
+ from flask import Blueprint, render_template
2
+ from canaille.models import AuthorizationCode
3
+ from canaille.flaskutils import admin_needed
4
+
5
+
6
+ bp = Blueprint(__name__, "authorizations")
7
+
8
+
9
+ @bp.route("/")
10
+ @admin_needed()
11
+ def index():
12
+ authorizations = AuthorizationCode.filter()
13
+ return render_template(
14
+ "admin/authorization_list.html", authorizations=authorizations
15
+ )
16
+
17
+
18
+ @bp.route("/<authorization_id>", methods=["GET", "POST"])
19
+ @admin_needed()
20
+ def view(authorization_id):
21
+ authorization = AuthorizationCode.get(authorization_id)
22
+ return render_template(
23
+ "admin/authorization_view.html", authorization=authorization, menuitem="admin"
24
+ )