From c483ef6a4d858b2a1aa1c984e378d3afd95a75bb Mon Sep 17 00:00:00 2001
From: Luka Radenovic <luka@init.hr>
Date: Thu, 10 Feb 2022 13:04:54 +0100
Subject: [PATCH] Add Kratos user id to Hydra callback response

---
 areas/auth/auth.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/areas/auth/auth.py b/areas/auth/auth.py
index 4334be45..47a1a5bf 100644
--- a/areas/auth/auth.py
+++ b/areas/auth/auth.py
@@ -5,7 +5,7 @@ from datetime import timedelta
 
 from areas import api_v1
 from config import *
-from helpers import HydraOauth, BadRequest
+from helpers import HydraOauth, BadRequest, KratosApi
 
 
 @api_v1.route("/login", methods=["POST"])
@@ -28,6 +28,12 @@ def hydra_callback():
 
     token = HydraOauth.get_token(state, code)
     user_info = HydraOauth.get_user_info()
+    # Match Kratos identity with Hydra
+    identities = KratosApi.get("/identities")
+    identity = None
+    for i in identities.json():
+        if i["traits"]["email"] == user_info["email"]:
+            identity = i
 
     access_token = create_access_token(
         identity=token, expires_delta=timedelta(days=365)
@@ -37,6 +43,7 @@ def hydra_callback():
         {
             "accessToken": access_token,
             "userInfo": {
+                "id": identity["id"],
                 "email": user_info["email"],
                 "name": user_info["name"],
                 "preferredUsername": user_info["preferred_username"],
-- 
GitLab