-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign_request.py
More file actions
executable file
·40 lines (29 loc) · 1.13 KB
/
sign_request.py
File metadata and controls
executable file
·40 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
"""sign_request.py
An example showing the signing of a request object.
"""
from os import environ, path
from jwcrypto.jwk import JWKSet
import util
def sign_request_example():
"""Example: sign a request object.
Loads the request object fields from environment variables and produces the
signed JWT as text.
"""
# Create a store for holding JWKs (e.g. on service startup).
jwk_store = JWKSet()
# Import the relevant private key.
private_key_file_path = environ.get('PRIVATE_KEY_PATH')
if private_key_file_path is None:
private_key_file_path = path.join(util.REPO_ROOT, 'keys/private.json')
private_key_text = util.load_key_text(filepath=private_key_file_path)
jwk_store.import_keyset(private_key_text)
# Create an example request object.
raw_request_object = util.get_raw_request_object()
# Get a key from the JWK store, and use it to sign the request object.
kid = util.get_kid()
jwk = jwk_store.get_key(kid)
jwt_as_text = util.sign_request(jwk, raw_request_object)
return jwt_as_text
if __name__ == '__main__':
print(sign_request_example())