From 4ae6fb311f906dc5be26d60de5a0a41c72503004 Mon Sep 17 00:00:00 2001 From: alpaylan Date: Thu, 15 Apr 2021 01:11:35 +0300 Subject: python_client halfway through and updated todo --- TODO.md | 24 ++++++++++ scripts/python_client.py | 111 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 123 insertions(+), 12 deletions(-) diff --git a/TODO.md b/TODO.md index b429097..15c317a 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,29 @@ # TODO +## Office Hour/Recitation +- [ ] Should give a little pointers but not too much, I think at first this is going to seem hard to many students but it should become fairly easy after some little pointers +## Docs +- [ ] Make a better explanation of authorization schema +- [ ] register: give the register message schema(passwd is missing) +- [ ] gradecoin: give narrative explanation +- [ ] how to bank works +- [ ] bank public key +- [ ] how to start(possibly some pointers and links -- blockchain, rest, jwt, rsa, public key) +- [ ] delete CONSTANTS +- [ ] register should have AuthRequest in the explanation +- [ ] link all types in schema.rs to the docs, they need to understand why we have them +- [ ] explain hash type(MD5 is missing in Claims) +- [ ] Initial auth request needs more explanation +- [ ] Explain JSON Wrapped +- [ ] Give links to the functions, their docs are very good. For example, it seems impossible to understand authentication from the first page, but when you go to handlers::authenticate_user many things are clarified. +- [ ] There is todo at handlers::authorized_propose_transaction, fix that +- [ ] authorized_propose_transaction and authorized_propose_block may have more explanation as in the case of + + +### Authorization +- [ ] Pointer to JWT +- [ ] Pointer to Public Key Sign + ## Tests - [ ] User Authentication/Authentication Tests - [ ] Route Tests diff --git a/scripts/python_client.py b/scripts/python_client.py index fe96cc2..2713f47 100644 --- a/scripts/python_client.py +++ b/scripts/python_client.py @@ -20,6 +20,92 @@ hash = "SHA-256" # return priv_key.publickey() +import requests +from jwt import ( + JWT, + jwk_from_dict, + jwk_from_pem, +) +from jwt.utils import get_int_from_datetime +from datetime import datetime, timedelta, timezone + + +def create_hashed_transaction(): + + pass + +def create_jwt(): + instance = JWT() + message = { + 'tha': create_hashed_transaction(), + 'iat': get_int_from_datetime(datetime.now(timezone.utc)), + 'exp': get_int_from_datetime( + datetime.now(timezone.utc) + timedelta(hours=1)), + } + + with open('rsa_private_key.pem', 'rb') as fh: + signing_key = jwk_from_pem(fh.read()) + + compact_jws = instance.encode(message, signing_key, alg='RS256') + + return compact_jws + + +def post_register(): + credentials = { + "c": "", + "iv": "", + "key": "" + } + response = requests.post("localhost:8080/register", data=credentials) + + +def post_transaction_from_bank(): + body = { + "by": "{my_public_key}", + "source": "{bank_public_key}", + "target": "{my_public_key}", + "amount": 0, + "timestamp": get_int_from_datetime(datetime.now(timezone.utc)), + } + header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'} + + response = requests.post("localhost:8080/transaction", headers=header, data=body) + print(response.headers) + print(response.content) + +def post_transaction_to_user(): + body = { + "by": "{my_public_key}", + "source": "{my_public_key}", + "target": "{user_public_key}", + "amount": 0, + "timestamp": get_int_from_datetime(datetime.now(timezone.utc)), + } + header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'} + + response = requests.post("localhost:8080/transaction", headers=header, data=body) + + +def post_block(): + credentials = { + "c": "", + "iv": "", + "key": "" + } + response = requests.post("localhost:8080/register", params=credentials) + + +def get_transaction(): + response = requests.get("http://localhost:8080/transaction") + print(response.headers) + + +def get_block(): + response = requests.get("http://localhost:8080/block") + print(response.headers) + + def encrypt(message, pub_key): cipher = PKCS1_OAEP.new(pub_key) return cipher.encrypt(message) @@ -39,18 +125,19 @@ AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL MwIDAQAB -----END PUBLIC KEY-----""" } - with open("../secrets/gradecoin.pub", "r") as fs: - data = fs.read() - pubkeyobj = RSA.importKey(data) - - cipher = PKCS1_OAEP.new(pubkeyobj) - - ser = json.dumps(myself, separators=(',', ':')) - - a = cipher.encrypt(ser) - - print(f"{a}") - + # with open("../secrets/gradecoin.pub", "r") as fs: + # data = fs.read() + # pubkeyobj = RSA.importKey(data) + # + # cipher = PKCS1_OAEP.new(pubkeyobj) + # + # ser = json.dumps(myself, separators=(',', ':')) + # + # a = cipher.encrypt(ser) + # + # print(f"{a}") + get_block() + get_transaction() -- cgit v1.2.3-70-g09d2