flask-enciphers 0.1.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.
@@ -0,0 +1,21 @@
1
+ name: Publish
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+
8
+ jobs:
9
+ publish:
10
+ runs-on: ubuntu-latest
11
+ environment: Any
12
+ permissions:
13
+ id-token: write
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-python@v5
17
+ with:
18
+ python-version: "3.11"
19
+ - run: pip install hatchling
20
+ - run: python -m hatchling build
21
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,184 @@
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 made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and derivative works thereof.
46
+
47
+ "Contribution" shall mean any work of authorship, including
48
+ the original version of the Work and any modifications or additions
49
+ to that Work or Derivative Works of the Work, that is intentionally
50
+ submitted to the Licensor for inclusion in the Work by the copyright owner
51
+ or by an individual or Legal Entity authorized to submit on behalf of
52
+ the copyright owner. For the purposes of this definition, "submitted"
53
+ means any form of electronic, verbal, or written communication sent
54
+ to the Licensor or its representatives, including but not limited to
55
+ communication on electronic mailing lists, source code control systems,
56
+ and issue tracking systems that are managed by, or on behalf of, the
57
+ Licensor for the purpose of discussing and improving the Work, but
58
+ excluding communication that is conspicuously marked or otherwise
59
+ designated in writing by the copyright owner as "Not a Contribution."
60
+
61
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
62
+ whom a Contribution has been received by the Licensor and subsequently
63
+ incorporated within the Work.
64
+
65
+ 2. Grant of Copyright License. Subject to the terms and conditions of
66
+ this License, each Contributor hereby grants to You a perpetual,
67
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
68
+ copyright license to reproduce, prepare Derivative Works of,
69
+ publicly display, publicly perform, sublicense, and distribute the
70
+ Work and such Derivative Works in Source or Object form.
71
+
72
+ 3. Grant of Patent License. Subject to the terms and conditions of
73
+ this License, each Contributor hereby grants to You a perpetual,
74
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
75
+ (except as stated in this section) patent license to make, have made,
76
+ use, offer to sell, sell, import, and otherwise transfer the Work,
77
+ where such license applies only to those patent claims licensable
78
+ by such Contributor that are necessarily infringed by their
79
+ Contribution(s) alone or by the combination of their Contribution(s)
80
+ with the Work to which such Contribution(s) was submitted. If You
81
+ institute patent litigation against any entity (including a cross-claim
82
+ or counterclaim in a lawsuit) alleging that the Work or a Contribution
83
+ incorporated within the Work constitutes direct or contributory patent
84
+ infringement, then any patent licenses granted to You under this License
85
+ for that Work shall terminate as of the date such litigation is filed.
86
+
87
+ 4. Redistribution. You may reproduce and distribute copies of the
88
+ Work or Derivative Works thereof in any medium, with or without
89
+ modifications, and in Source or Object form, provided that You
90
+ meet the following conditions:
91
+
92
+ (a) You must give any other recipients of the Work or Derivative Works
93
+ a copy of this License; and
94
+
95
+ (b) You must cause any modified files to carry prominent notices
96
+ stating that You changed the files; and
97
+
98
+ (c) You must retain, in the Source form of any Derivative Works
99
+ that You distribute, all copyright, patent, trademark, and
100
+ attribution notices from the Source form of the Work,
101
+ excluding those notices that do not pertain to any part of
102
+ the Derivative Works; and
103
+
104
+ (d) If the Work includes a "NOTICE" text file as part of its
105
+ distribution, You must include a readable copy of the attribution
106
+ notices contained within such NOTICE file, in at least one
107
+ of the following places: within a NOTICE text provided as part of
108
+ the Derivative Works; within the Source form or documentation,
109
+ if provided along with the Derivative Works; or, within a display
110
+ generated by the Derivative Works, if and wherever such third-party
111
+ notices normally appear. The contents of the NOTICE file are for
112
+ informational purposes only and do not modify the License. You may
113
+ add Your own attribution notices within Derivative Works that You
114
+ distribute, alongside or as an addendum to the NOTICE text from
115
+ the Work, provided that such additional attribution notices cannot
116
+ be construed as modifying the License.
117
+
118
+ You may add Your own license statement for Your modifications and
119
+ may provide additional grant of rights to use, copy, modify, merge,
120
+ publish, distribute, sublicense, and/or sell copies of the
121
+ Contribution, and to permit persons to whom the Contribution is
122
+ furnished to do so.
123
+
124
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
125
+ any Contribution intentionally submitted for inclusion in the Work
126
+ by You to the Licensor shall be under the terms and conditions of
127
+ this License, without any additional terms or conditions.
128
+ Notwithstanding the above, nothing herein shall supersede or modify
129
+ the terms of any separate license agreement you may have executed
130
+ with Licensor regarding such Contributions.
131
+
132
+ 6. Trademarks. This License does not grant permission to use the trade
133
+ names, trademarks, service marks, or product names of the Licensor,
134
+ except as required for reasonable and customary use in describing the
135
+ origin of the Work and reproducing the content of the NOTICE file.
136
+
137
+ 7. Disclaimer of Warranty. Unless required by applicable law or
138
+ agreed to in writing, Licensor provides the Work (and each
139
+ Contributor provides its Contributions) on an "AS IS" BASIS,
140
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
141
+ implied, including, without limitation, any warranties or conditions
142
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
143
+ PARTICULAR PURPOSE. You are solely responsible for determining the
144
+ appropriateness of using or reproducing the Work and assume any
145
+ risks associated with Your exercise of permissions under this License.
146
+
147
+ 8. Limitation of Liability. In no event and under no legal theory,
148
+ whether in tort (including negligence), contract, or otherwise,
149
+ unless required by applicable law (such as deliberate and grossly
150
+ negligent acts) or agreed to in writing, shall any Contributor be
151
+ liable to You for damages, including any direct, indirect, special,
152
+ incidental, or exemplary damages of any character arising as a
153
+ result of this License or out of the use or inability to use the
154
+ Work (including but not limited to damages for loss of goodwill,
155
+ work stoppage, computer failure or malfunction, or all other
156
+ commercial damages or losses), even if such Contributor has been
157
+ advised of the possibility of such damages.
158
+
159
+ 9. Accepting Warranty or Additional Liability. While redistributing
160
+ the Work or Derivative Works thereof, You may choose to offer,
161
+ and charge a fee for, acceptance of support, warranty, indemnity,
162
+ or other liability obligations and/or rights consistent with this
163
+ License. However, in accepting such obligations, You may act only
164
+ on Your own behalf and on Your sole responsibility, not on behalf
165
+ of any other Contributor, and only if You agree to indemnify,
166
+ defend, and hold each Contributor harmless for any liability
167
+ incurred by, or claims asserted against, such Contributor by reason
168
+ of your accepting any warranty or additional liability.
169
+
170
+ END OF TERMS AND CONDITIONS
171
+
172
+ Copyright 2026 Mejlad Alsubaie
173
+
174
+ Licensed under the Apache License, Version 2.0 (the "License");
175
+ you may not use this file except in compliance with the License.
176
+ You may obtain a copy of the License at
177
+
178
+ http://www.apache.org/licenses/LICENSE-2.0
179
+
180
+ Unless required by applicable law or agreed to in writing, software
181
+ distributed under the License is distributed on an "AS IS" BASIS,
182
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183
+ See the License for the specific language governing permissions and
184
+ limitations under the License.
@@ -0,0 +1,77 @@
1
+ Metadata-Version: 2.4
2
+ Name: flask-enciphers
3
+ Version: 0.1.0
4
+ Summary: Encrypted session interface for Flask using enciphers
5
+ Project-URL: Homepage, https://github.com/mjlad/flask-enciphers
6
+ Author: Mejlad Alsubaie
7
+ License: Apache-2.0
8
+ License-File: LICENSE
9
+ Keywords: enciphers,encryption,flask,session
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Framework :: Flask
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Security :: Cryptography
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: enciphers
21
+ Requires-Dist: flask>=2.0
22
+ Requires-Dist: orjson
23
+ Description-Content-Type: text/markdown
24
+
25
+ # quart-enciphers
26
+
27
+ Encrypted session interface for Quart using [enciphers](https://pypi.org/project/enciphers/).
28
+
29
+ Replaces Quart's default signed cookie session with a fully encrypted one.
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install quart-enciphers
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ```python
40
+ from quart import Quart, session
41
+ from quart_enciphers import EnciphersSession
42
+
43
+ app = Quart(__name__)
44
+ EnciphersSession(app)
45
+
46
+ @app.route("/login")
47
+ async def login():
48
+ session["user_id"] = 1
49
+ return "logged in"
50
+ ```
51
+
52
+ ### Application Factory Pattern
53
+
54
+ ```python
55
+ from quart_enciphers import EnciphersSession
56
+
57
+ es = EnciphersSession()
58
+
59
+ def create_app():
60
+ app = Quart(__name__)
61
+ es.init_app(app)
62
+ return app
63
+ ```
64
+
65
+ ## Configuration
66
+
67
+ | Key | Type | Default | Description |
68
+ |---|---|---|---|
69
+ | `ENCIPHERS_STEP` | `int` | random | Encryption step |
70
+ | `ENCIPHERS_KEY` | `int` | random | Secret key |
71
+ | `ENCIPHERS_KEY_ENV` | `str` | None | Environment variable for key |
72
+
73
+ > If no configuration is provided, random values are generated at startup.
74
+
75
+ ## License
76
+
77
+ Apache-2.0 — Copyright 2026 Mejlad Alsubaie
@@ -0,0 +1,53 @@
1
+ # quart-enciphers
2
+
3
+ Encrypted session interface for Quart using [enciphers](https://pypi.org/project/enciphers/).
4
+
5
+ Replaces Quart's default signed cookie session with a fully encrypted one.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install quart-enciphers
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```python
16
+ from quart import Quart, session
17
+ from quart_enciphers import EnciphersSession
18
+
19
+ app = Quart(__name__)
20
+ EnciphersSession(app)
21
+
22
+ @app.route("/login")
23
+ async def login():
24
+ session["user_id"] = 1
25
+ return "logged in"
26
+ ```
27
+
28
+ ### Application Factory Pattern
29
+
30
+ ```python
31
+ from quart_enciphers import EnciphersSession
32
+
33
+ es = EnciphersSession()
34
+
35
+ def create_app():
36
+ app = Quart(__name__)
37
+ es.init_app(app)
38
+ return app
39
+ ```
40
+
41
+ ## Configuration
42
+
43
+ | Key | Type | Default | Description |
44
+ |---|---|---|---|
45
+ | `ENCIPHERS_STEP` | `int` | random | Encryption step |
46
+ | `ENCIPHERS_KEY` | `int` | random | Secret key |
47
+ | `ENCIPHERS_KEY_ENV` | `str` | None | Environment variable for key |
48
+
49
+ > If no configuration is provided, random values are generated at startup.
50
+
51
+ ## License
52
+
53
+ Apache-2.0 — Copyright 2026 Mejlad Alsubaie
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "flask-enciphers"
7
+ version = "0.1.0"
8
+ description = "Encrypted session interface for Flask using enciphers"
9
+ readme = "README.md"
10
+ license = { text = "Apache-2.0" }
11
+ authors = [{ name = "Mejlad Alsubaie" }]
12
+ keywords = ["flask", "session", "encryption", "enciphers"]
13
+ requires-python = ">=3.11"
14
+ dependencies = ["flask >= 2.0", "enciphers", "orjson"]
15
+
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "License :: OSI Approved :: Apache Software License",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Topic :: Security :: Cryptography",
25
+ "Framework :: Flask",
26
+ ]
27
+
28
+ [project.urls]
29
+ Homepage = "https://github.com/mjlad/flask-enciphers"
30
+
31
+ [tool.hatch.build.targets.wheel]
32
+ packages = ["src/flask_enciphers"]
@@ -0,0 +1,3 @@
1
+ from .sessions import EnciphersSession
2
+
3
+ __all__ = ["EnciphersSession"]
@@ -0,0 +1,118 @@
1
+ from __future__ import annotations
2
+ import secrets
3
+ import orjson
4
+ from flask import Flask
5
+ from enciphers import Encipher
6
+ from flask.sessions import SecureCookieSessionInterface, SecureCookieSession
7
+ from flask.sessions import SessionMixin
8
+ from werkzeug.wrappers import Request, Response
9
+
10
+
11
+ class EnciphersSession(SecureCookieSessionInterface):
12
+ """A Flask session interface that uses enciphers for encryption.
13
+
14
+ Replaces the default signing with full encryption using enciphers.
15
+ All cookie settings are inherited from SecureCookieSessionInterface.
16
+
17
+ Usage::
18
+
19
+ app = Flask(__name__)
20
+ EnciphersSession(app)
21
+
22
+ # or with application factory pattern
23
+ es = EnciphersSession()
24
+
25
+ def create_app():
26
+ app = Flask(__name__)
27
+ es.init_app(app)
28
+ return app
29
+
30
+ Configuration::
31
+
32
+ app.config["ENCIPHERS_STEP"] = 7 # optional
33
+ app.config["ENCIPHERS_KEY"] = 42 # optional
34
+ app.config["ENCIPHERS_KEY_ENV"] = "MY_KEY" # optional
35
+ """
36
+
37
+ session_class = SecureCookieSession
38
+
39
+ def __init__(self, app: Flask | None = None) -> None:
40
+ self.app = app
41
+ if self.app is not None:
42
+ self.init_app(self.app)
43
+
44
+ @staticmethod
45
+ def _setup(app: Flask) -> Encipher:
46
+ step: int | None = app.config.get("ENCIPHERS_STEP")
47
+ key: int | None = app.config.get("ENCIPHERS_KEY")
48
+ key_env : str | None = app.config.get("ENCIPHERS_KEY_ENV")
49
+
50
+ if not step:
51
+ step = secrets.randbelow(255) + 1
52
+
53
+ if not key and not key_env:
54
+ key = secrets.randbits(64)
55
+
56
+ return Encipher(step=step, key=key, key_env=key_env)
57
+
58
+ def init_app(self, app: Flask) -> None:
59
+ self.cipher = self._setup(app)
60
+ app.session_interface = self
61
+
62
+ def open_session(
63
+ self, app: Flask, request: Request
64
+ ) -> SecureCookieSession | None:
65
+
66
+ cookie = request.cookies.get(self.get_cookie_name(app))
67
+
68
+ if cookie is None:
69
+ return self.session_class()
70
+
71
+ try:
72
+ data = orjson.loads(self.cipher.decrypt(cookie))
73
+ return self.session_class(data)
74
+ except Exception:
75
+ return self.session_class()
76
+
77
+ def save_session(
78
+ self,
79
+ app: Flask,
80
+ session: SessionMixin,
81
+ response: Response,
82
+ ) -> None:
83
+
84
+ name = self.get_cookie_name(app)
85
+ domain = self.get_cookie_domain(app)
86
+ path = self.get_cookie_path(app)
87
+ secure = self.get_cookie_secure(app)
88
+ samesite = self.get_cookie_samesite(app)
89
+ httponly = self.get_cookie_httponly(app)
90
+
91
+ if not session:
92
+ if session.modified:
93
+ response.delete_cookie(
94
+ name,
95
+ domain = domain,
96
+ path = path,
97
+ secure = secure,
98
+ samesite = samesite,
99
+ httponly = httponly,
100
+ )
101
+ return
102
+
103
+ if not self.should_set_cookie(app, session):
104
+ return
105
+
106
+ expires = self.get_expiration_time(app, session)
107
+ token = self.cipher.encrypt(orjson.dumps(dict(session)))
108
+
109
+ response.set_cookie(
110
+ name,
111
+ token,
112
+ expires = expires,
113
+ httponly = httponly,
114
+ domain = domain,
115
+ path = path,
116
+ secure = secure,
117
+ samesite = samesite,
118
+ )