Skip to content

Commit

Permalink
Store the pres request in the db record to retrieve later
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas ONeil <[email protected]>
  • Loading branch information
loneil committed Feb 23, 2024
1 parent 72a8da6 commit bd87625
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 62 deletions.
3 changes: 2 additions & 1 deletion oidc-controller/api/authSessions/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
from enum import StrEnum, auto
from typing import Dict
from typing import Dict, Optional

from api.core.acapy.client import AcapyClient
from api.core.models import UUIDModel
Expand All @@ -25,6 +25,7 @@ class AuthSessionBase(BaseModel):
request_parameters: dict
pyop_auth_code: str
response_url: str
presentation_request_msg: Optional[dict] = None

class Config:
allow_population_by_field_name = True
Expand Down
73 changes: 68 additions & 5 deletions oidc-controller/api/routers/oidc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import io
import json
from typing import cast
import uuid
from datetime import datetime
Expand All @@ -18,6 +19,14 @@
from ..authSessions.crud import AuthSessionCreate, AuthSessionCRUD
from ..authSessions.models import AuthSessionPatch, AuthSessionState
from ..core.acapy.client import AcapyClient
from ..core.aries import (
OOBServiceDecorator,
OutOfBandMessage,
OutOfBandPresentProofAttachment,
PresentationRequestMessage,
PresentProofv10Attachment,
ServiceDecorator,
)
from ..core.config import settings
from ..core.logger_util import log_debug
from ..core.oidc import provider
Expand Down Expand Up @@ -105,19 +114,71 @@ async def get_authorize(request: Request, db: Database = Depends(get_db)):

# Create presentation_request to show on screen
response = client.create_presentation_request(ver_config.generate_proof_request())
pres_exch_dict = response.dict()

# Prepeare the presentation request
client = AcapyClient()
use_public_did = (
not settings.USE_OOB_PRESENT_PROOF
) and settings.USE_OOB_LOCAL_DID_SERVICE
wallet_did = client.get_wallet_did(public=use_public_did)

byo_attachment = PresentProofv10Attachment.build(
pres_exch_dict["presentation_request"]
)

msg = None
if settings.USE_OOB_PRESENT_PROOF:
if settings.USE_OOB_LOCAL_DID_SERVICE:
oob_s_d = OOBServiceDecorator(
service_endpoint=client.service_endpoint,
recipient_keys=[wallet_did.verkey],
).dict()
else:
wallet_did = client.get_wallet_did(public=True)
oob_s_d = wallet_did.verkey

msg = PresentationRequestMessage(
id=pres_exch_dict["thread_id"],
request=[byo_attachment],
)
oob_msg = OutOfBandMessage(
request_attachments=[
OutOfBandPresentProofAttachment(
id="request-0",
data={"json": msg.dict(by_alias=True)},
)
],
id=pres_exch_dict["thread_id"],
services=[oob_s_d],
)
msg_contents = oob_msg
else:
s_d = ServiceDecorator(
service_endpoint=client.service_endpoint, recipient_keys=[wallet_did.verkey]
)
msg = PresentationRequestMessage(
id=pres_exch_dict["thread_id"],
request=[byo_attachment],
service=s_d,
)
msg_contents = msg


# Create and save OIDC AuthSession
new_auth_session = AuthSessionCreate(
response_url=authn_response.request(auth_req["redirect_uri"]),
pyop_auth_code=authn_response["code"],
request_parameters=model.to_dict(),
ver_config_id=ver_config_id,
pres_exch_id=response.presentation_exchange_id,
presentation_exchange=response.dict(),
presentation_exchange=pres_exch_dict,
presentation_request_msg=msg_contents.dict(by_alias=True),
)

# save OIDC AuthSession
auth_session = await AuthSessionCRUD(db).create(new_auth_session)

formated_msg = json.dumps(msg_contents.dict(by_alias=True))

# QR CONTENTS
controller_host = settings.CONTROLLER_URL
url_to_message = (
Expand All @@ -130,8 +191,10 @@ async def get_authorize(request: Request, db: Database = Depends(get_db)):
callback_url = f"""{controller_host}{AuthorizeCallbackUri}?pid={auth_session.id}"""

# BC Wallet deep link
response_b64 = base64.b64encode(response.json().encode("utf-8")).decode("utf-8")
wallet_deep_link = f"bcwallet://aries_proof-request?oob={response_b64}"
# base64 encode the formated_msg
base64_msg = base64.b64encode(formated_msg.encode("utf-8")).decode("utf-8")
wallet_deep_link = f"bcwallet://aries_proof-request?c_i={base64_msg}"


# This is the payload to send to the template
data = {
Expand Down
60 changes: 4 additions & 56 deletions oidc-controller/api/routers/presentation_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@

from ..authSessions.crud import AuthSessionCRUD
from ..authSessions.models import AuthSession, AuthSessionState
from ..core.acapy.client import AcapyClient
from ..core.aries import (
OOBServiceDecorator,
OutOfBandMessage,
OutOfBandPresentProofAttachment,
PresentationRequestMessage,
PresentProofv10Attachment,
ServiceDecorator,
)

from ..core.config import settings
from ..routers.socketio import (sio, connections_reload)
from ..db.session import get_db
Expand Down Expand Up @@ -65,51 +57,7 @@ async def send_connectionless_proof_req(
await AuthSessionCRUD(db).patch(auth_session.id, auth_session)
await sio.emit('status', {'status': 'pending'}, to=sid)

client = AcapyClient()
use_public_did = (
not settings.USE_OOB_PRESENT_PROOF
) and settings.USE_OOB_LOCAL_DID_SERVICE
wallet_did = client.get_wallet_did(public=use_public_did)

byo_attachment = PresentProofv10Attachment.build(
auth_session.presentation_exchange["presentation_request"]
)
msg = auth_session.presentation_request_msg

msg = None
if settings.USE_OOB_PRESENT_PROOF:
if settings.USE_OOB_LOCAL_DID_SERVICE:
oob_s_d = OOBServiceDecorator(
service_endpoint=client.service_endpoint,
recipient_keys=[wallet_did.verkey],
).dict()
else:
wallet_did = client.get_wallet_did(public=True)
oob_s_d = wallet_did.verkey

msg = PresentationRequestMessage(
id=auth_session.presentation_exchange["thread_id"],
request=[byo_attachment],
)
oob_msg = OutOfBandMessage(
request_attachments=[
OutOfBandPresentProofAttachment(
id="request-0",
data={"json": msg.dict(by_alias=True)},
)
],
id=auth_session.presentation_exchange["thread_id"],
services=[oob_s_d],
)
msg_contents = oob_msg
else:
s_d = ServiceDecorator(
service_endpoint=client.service_endpoint, recipient_keys=[wallet_did.verkey]
)
msg = PresentationRequestMessage(
id=auth_session.presentation_exchange["thread_id"],
request=[byo_attachment],
service=s_d,
)
msg_contents = msg
logger.debug(msg_contents.dict(by_alias=True))
return JSONResponse(msg_contents.dict(by_alias=True))
logger.debug(msg)
return JSONResponse(msg)

0 comments on commit bd87625

Please sign in to comment.