<s> import json <EOL> from django . http import HttpResponse <EOL> from django . http import HttpResponseBadRequest <EOL> from django . http import HttpResponseForbidden <EOL> from django . views . decorators . http import require_POST <EOL> from django . views . decorators . http import require_safe <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from pqauth . crypto import rsa_decrypt <EOL> from pqauth . crypto import rsa_encrypt <EOL> from pqauth . crypto import random_guid <EOL> from pqauth . pqauth_django_server . keys import SERVER_KEY <EOL> from pqauth . pqauth_django_server . keys import SERVER_KEY_FINGERPRINT <EOL> from pqauth . pqauth_django_server . models import PublicKey <EOL> from pqauth . pqauth_django_server . models import PQAuthSession <EOL> def encrypted_json_post ( view_func ) : <EOL> def inner ( request , * args , ** kwargs ) : <EOL> try : <EOL> decrypted_body = rsa_decrypt ( request . body , SERVER_KEY ) <EOL> request . decrypted_json = json . loads ( decrypted_body ) <EOL> return view_func ( request , * args , ** kwargs ) <EOL> except ValueError : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return HttpResponseBadRequest ( msg ) <EOL> return csrf_exempt ( require_POST ( inner ) ) <EOL> @ require_safe <EOL> def public_key ( _ ) : <EOL> key_text = SERVER_KEY . exportKey ( format = "<STR_LIT>" ) <EOL> return HttpResponse ( key_text , mimetype = "<STR_LIT>" ) <EOL> @ encrypted_json_post <EOL> def hello ( request ) : <EOL> client_hello = request . decrypted_json <EOL> try : <EOL> client_key = PublicKey . objects . get ( <EOL> fingerprint = client_hello [ "<STR_LIT>" ] ) <EOL> except PublicKey . DoesNotExist : <EOL> return HttpResponseForbidden ( "<STR_LIT>" % <EOL> client_hello [ "<STR_LIT>" ] ) <EOL> response = { "<STR_LIT>" : client_hello [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : random_guid ( ) , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : SERVER_KEY_FINGERPRINT } <EOL> started_session = PQAuthSession ( server_guid = response [ "<STR_LIT>" ] , <EOL> client_guid = response [ "<STR_LIT>" ] , <EOL> user = client_key . user ) <EOL> started_session . save ( ) <EOL> encrypted_response = rsa_encrypt ( json . dumps ( response ) , <EOL> client_key . public_key ) <EOL> return HttpResponse ( encrypted_response , <EOL> mimetype = "<STR_LIT>" ) <EOL> @ encrypted_json_post <EOL> def confirm ( request ) : <EOL> confirm = request . decrypted_json <EOL> guid = confirm [ "<STR_LIT>" ] <EOL> try : <EOL> started_session = PQAuthSession . objects . get ( server_guid = guid ) <EOL> started_session . session_key = "<STR_LIT>" % ( started_session . client_guid , <EOL> started_session . server_guid ) <EOL> started_session . save ( ) <EOL> except PQAuthSession . DoesNotExist : <EOL> pass <EOL> return HttpResponse ( ) </s>
