Skip to content

Commit

Permalink
Credential page
Browse files Browse the repository at this point in the history
  • Loading branch information
w4ll3 committed Apr 8, 2021
1 parent dbbf9c3 commit 2ab4a00
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
__pycache__/*
key.*
23 changes: 21 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import stripe
import didkit
import errno
import json
from issue_credential import issueCredential
from flask import Flask, jsonify, render_template, request
from didkit import generateEd25519Key

app = Flask(__name__)

Expand All @@ -27,8 +31,9 @@ def index():

@app.route("/success")
def success():
# mail guy
return 'Success'
credential = json.dumps(issueCredential(request), indent=2, sort_keys=True)

return render_template('credential.html', credential=credential, didkit_version=didkit.getVersion())


@app.route("/cancelation")
Expand Down Expand Up @@ -76,3 +81,17 @@ def create_checkout_session():
return jsonify({"sessionId": checkout_session["id"]})
except Exception as e:
return jsonify(error=str(e)), 403


if __name__ == 'app':
flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
try:
file_handle = os.open('key.jwk', flags)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise
else:
with os.fdopen(file_handle, 'w') as file_obj:
file_obj.write(generateEd25519Key())
51 changes: 51 additions & 0 deletions issue_credential.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from datetime import datetime, timedelta
import didkit
import json
import stripe
import uuid


def issueCredential(request):
session_id = request.args.get("session_id")
session_data = stripe.checkout.Session.retrieve(session_id)

with open('key.jwk', "r") as f:
key = f.readline()
f.close()

did_key = request.form.get('subject_id', didkit.keyToDID("key", key))
verification_method = didkit.keyToVerificationMethod("key", key)
issuance_date = datetime.utcnow().replace(microsecond=0)
expiration_date = issuance_date + timedelta(weeks=4)

credential = {
"id": "urn:uuid:" + uuid.uuid4().__str__(),
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1",
],
"type": ["VerifiableCredential"],
"issuer": did_key,
"issuanceDate": issuance_date.isoformat() + "Z",
"expirationDate": expiration_date.isoformat() + "Z",
"credentialSubject": {
"@context": [
{
"email": "https://schema.org/Text"
}
],
"id": "urn:uuid:" + uuid.uuid4().__str__(),
"email": session_data["customer_details"]["email"]
},
}

didkit_options = {
"proofPurpose": "assertionMethod",
"verificationMethod": verification_method,
}

credential = didkit.issueCredential(
credential.__str__().replace("'", '"'),
didkit_options.__str__().replace("'", '"'),
key)
return json.loads(credential)
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<script src="https://js.stripe.com/v3/"></script>
<script src="{{ url_for('static', filename='main.js') }}"></script>
<script src="https://unpkg.com/[email protected]/dist/credential-handler-polyfill.min.js"></script>
</head>
<body class="w-screen h-screen items-center justify-center flex">
{% block content %}{% endblock %}
Expand Down
5 changes: 5 additions & 0 deletions templates/credential.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %}

{% block content %}
<pre class="whitespace-pre-wrap m-auto">{{ credential }}</pre>
{% endblock %}

0 comments on commit 2ab4a00

Please sign in to comment.