Merge branch 'main' into feat/batch-create-users

This commit is contained in:
Maarten de Waard 2022-07-21 17:08:41 +02:00
commit 2baae3e61e
No known key found for this signature in database
GPG key ID: 1D3E893A657CC8DA
4 changed files with 54 additions and 16 deletions

View file

@ -1,3 +1,9 @@
import ory_kratos_client
from ory_kratos_client.model.submit_self_service_recovery_flow_body \
import SubmitSelfServiceRecoveryFlowBody
from ory_kratos_client.api import v0alpha2_api as kratos_api
from config import KRATOS_ADMIN_URL
from database import db from database import db
from areas.apps import App, AppRole, AppsService from areas.apps import App, AppRole, AppsService
from areas.roles import Role, RoleService from areas.roles import Role, RoleService
@ -7,6 +13,10 @@ from flask import current_app
from helpers.error_handler import KratosError from helpers.error_handler import KratosError
kratos_admin_api_configuration = \
ory_kratos_client.Configuration(host=KRATOS_ADMIN_URL, discard_unknown_keys=True)
KRATOS_ADMIN = \
kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(kratos_admin_api_configuration))
class UserService: class UserService:
@staticmethod @staticmethod
@ -27,7 +37,10 @@ class UserService:
def post_user(data): def post_user(data):
kratos_data = { kratos_data = {
"schema_id": "default", "schema_id": "default",
"traits": {"email": data["email"], "name": data["name"]}, "traits": {
"name": data["name"],
"email": data["email"],
},
} }
res = KratosApi.post("/admin/identities", kratos_data).json() res = KratosApi.post("/admin/identities", kratos_data).json()
@ -55,8 +68,32 @@ class UserService:
db.session.add(app_role) db.session.add(app_role)
db.session.commit() db.session.commit()
UserService.__start_recovery_flow(data["email"])
return UserService.get_user(res["id"]) return UserService.get_user(res["id"])
@staticmethod
def __start_recovery_flow(email):
"""
Start a Kratos recovery flow for the user's email address.
This sends out an email to the user that explains to them how they can
set their password. Make sure the user exists inside Kratos before you
use this function.
:param email: Email to send recovery link to
:type email: str
"""
api_response = KRATOS_ADMIN.initialize_self_service_recovery_flow_without_browser()
flow = api_response['id']
# Submit the recovery flow to send an email to the new user.
submit_self_service_recovery_flow_body = \
SubmitSelfServiceRecoveryFlowBody(method="link", email=email)
api_response = KRATOS_ADMIN.submit_self_service_recovery_flow(flow,
submit_self_service_recovery_flow_body=
submit_self_service_recovery_flow_body)
@staticmethod @staticmethod
def put_user(id, user_editing_id, data): def put_user(id, user_editing_id, data):
kratos_data = { kratos_data = {

View file

@ -27,11 +27,15 @@ HYDRA = hydra_client.HydraAdmin(HYDRA_ADMIN_URL)
# Kratos has an admin and public end-point. We create an API for them # Kratos has an admin and public end-point. We create an API for them
# both. The kratos implementation has bugs, which forces us to set # both. The kratos implementation has bugs, which forces us to set
# the discard_unknown_keys to True. # the discard_unknown_keys to True.
tmp = ory_kratos_client.Configuration(host=KRATOS_ADMIN_URL, discard_unknown_keys=True) kratos_admin_api_configuration = \
KRATOS_ADMIN = kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(tmp)) ory_kratos_client.Configuration(host=KRATOS_ADMIN_URL, discard_unknown_keys=True)
KRATOS_ADMIN = \
kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(kratos_admin_api_configuration))
tmp = ory_kratos_client.Configuration(host=KRATOS_PUBLIC_URL, discard_unknown_keys=True) kratos_public_api_configuration = \
KRATOS_PUBLIC = kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(tmp)) ory_kratos_client.Configuration(host=KRATOS_PUBLIC_URL, discard_unknown_keys=True)
KRATOS_PUBLIC = \
kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(kratos_public_api_configuration))
############################################################################## ##############################################################################
# CLI INTERFACE # # CLI INTERFACE #

View file

@ -31,11 +31,15 @@ HYDRA = hydra_client.HydraAdmin(HYDRA_ADMIN_URL)
# Kratos has an admin and public end-point. We create an API for them # Kratos has an admin and public end-point. We create an API for them
# both. The kratos implementation has bugs, which forces us to set # both. The kratos implementation has bugs, which forces us to set
# the discard_unknown_keys to True. # the discard_unknown_keys to True.
tmp = ory_kratos_client.Configuration(host=KRATOS_ADMIN_URL, discard_unknown_keys=True) kratos_admin_api_configuration = \
KRATOS_ADMIN = kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(tmp)) ory_kratos_client.Configuration(host=KRATOS_ADMIN_URL, discard_unknown_keys=True)
KRATOS_ADMIN = \
kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(kratos_admin_api_configuration))
tmp = ory_kratos_client.Configuration(host=KRATOS_PUBLIC_URL, discard_unknown_keys=True) kratos_public_api_configuration = \
KRATOS_PUBLIC = kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(tmp)) ory_kratos_client.Configuration(host=KRATOS_PUBLIC_URL, discard_unknown_keys=True)
KRATOS_PUBLIC = \
kratos_api.V0alpha2Api(ory_kratos_client.ApiClient(kratos_public_api_configuration))
ADMIN_ROLE_ID = 1 ADMIN_ROLE_ID = 1
NO_ACCESS_ROLE_ID = 3 NO_ACCESS_ROLE_ID = 3

View file

@ -1,5 +1,3 @@
/* base.js /* base.js
This is the base JS file to render the user interfaces of kratos and provide This is the base JS file to render the user interfaces of kratos and provide
the end user with flows for login, recovery etc. the end user with flows for login, recovery etc.
@ -433,8 +431,3 @@ $.urlParam = function(name) {
} }
return decodeURI(results[1]) || 0; return decodeURI(results[1]) || 0;
}; };