コード例 #1
0
ファイル: FC_as_FS.py プロジェクト: sakho3600/Aidants_Connect
def get_user_info(connection: Connection) -> tuple:
    fc_base = settings.FC_AS_FS_BASE_URL
    fc_user_info = python_request.get(
        f"{fc_base}/userinfo?schema=openid",
        headers={"Authorization": f"Bearer {connection.access_token}"},
    )
    user_info = fc_user_info.json()

    if user_info.get("birthplace") == "":
        user_info["birthplace"] = None

    usager_sub = generate_sha256_hash(
        f"{user_info['sub']}{settings.FC_AS_FI_HASH_SALT}".encode())

    try:
        usager = Usager.objects.get(sub=usager_sub)

        if usager.email != user_info.get("email"):
            usager.email = user_info.get("email")
            usager.save()

            Journal.log_update_email_usager(aidant=connection.aidant,
                                            usager=usager)

        return usager, None

    except Usager.DoesNotExist:
        try:
            usager = Usager.objects.create(
                given_name=user_info.get("given_name"),
                family_name=user_info.get("family_name"),
                birthdate=user_info.get("birthdate"),
                gender=user_info.get("gender"),
                birthplace=user_info.get("birthplace"),
                birthcountry=user_info.get("birthcountry"),
                sub=usager_sub,
                email=user_info.get("email"),
            )
            return usager, None

        except IntegrityError as e:
            log.error("Error happened in Recap")
            log.error(e)
            return None, f"The FranceConnect ID is not complete: {e}"
コード例 #2
0
def get_user_info(connection: Connection) -> tuple:
    fc_base = settings.FC_AS_FS_BASE_URL
    fc_user_info = python_request.get(
        f"{fc_base}/userinfo?schema=openid",
        headers={"Authorization": f"Bearer {connection.access_token}"},
    )
    user_info = fc_user_info.json()

    user_phone = connection.user_phone if len(
        connection.user_phone) > 0 else None

    if user_info.get("birthplace") == "":
        user_info["birthplace"] = None

    user_sub = user_info.get("sub")
    if not user_sub:
        return None, "Unable to find sub in FC user info"

    usager_sub = generate_sha256_hash(
        f"{user_sub}{settings.FC_AS_FI_HASH_SALT}".encode())

    try:
        usager = Usager.objects.get(sub=usager_sub)

        if usager.email != user_info.get("email"):
            usager.email = user_info.get("email")
            usager.save()
            Journal.log_update_email_usager(aidant=connection.aidant,
                                            usager=usager)

        if user_phone is not None and usager.phone != user_phone:
            usager.phone = user_phone
            Journal.log_update_phone_usager(aidant=connection.aidant,
                                            usager=usager)
            usager.save()

        if not usager.preferred_username and user_info.get(
                "preferred_username"):
            usager.preferred_username = user_info.get("preferred_username")
            usager.save()

        return usager, None

    except Usager.DoesNotExist:
        kwargs = {
            "given_name": user_info.get("given_name"),
            "family_name": user_info.get("family_name"),
            "birthdate": user_info.get("birthdate"),
            "gender": user_info.get("gender"),
            "birthplace": user_info.get("birthplace"),
            "birthcountry": user_info.get("birthcountry"),
            "preferred_username": user_info.get("preferred_username"),
            "sub": usager_sub,
            "email": user_info.get("email"),
        }

        if user_phone is not None:
            kwargs["phone"] = user_phone

        try:
            usager = Usager.objects.create(**kwargs)
            return usager, None

        except IntegrityError as e:
            log.error("Error happened in Recap")
            log.error(e)
            return None, f"The FranceConnect ID is not complete: {e}"