2024-01-24 07:55:14 -08:00
|
|
|
|
import json
|
2024-03-19 13:23:22 -07:00
|
|
|
|
import os
|
2024-01-24 07:55:14 -08:00
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from flask import Blueprint, current_app, jsonify, request
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from itsdangerous import BadData, SignatureExpired
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
2024-01-24 07:55:14 -08:00
|
|
|
|
from app import redis_store
|
2018-02-16 10:56:12 +00:00
|
|
|
|
from app.config import QueueNames
|
|
|
|
|
|
from app.dao.invited_org_user_dao import (
|
2021-03-11 20:26:44 +00:00
|
|
|
|
get_invited_org_user as dao_get_invited_org_user,
|
|
|
|
|
|
)
|
|
|
|
|
|
from app.dao.invited_org_user_dao import (
|
2021-03-12 12:50:07 +00:00
|
|
|
|
get_invited_org_user_by_id,
|
2023-07-10 11:06:29 -07:00
|
|
|
|
get_invited_org_users_for_organization,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
save_invited_org_user,
|
2018-02-16 10:56:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2024-01-16 07:37:21 -05:00
|
|
|
|
from app.enums import KeyType, NotificationType
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
2024-01-18 10:28:50 -05:00
|
|
|
|
from app.models import InvitedOrganizationUser
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
|
)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
from app.organization.organization_schema import (
|
2018-02-19 15:03:36 +00:00
|
|
|
|
post_create_invited_org_user_status_schema,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
post_update_invited_org_user_status_schema,
|
2018-02-19 15:03:36 +00:00
|
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.schema_validation import validate
|
2025-06-26 10:35:46 -07:00
|
|
|
|
from app.utils import check_suspicious_id
|
2024-05-16 10:17:45 -04:00
|
|
|
|
from notifications_utils.url_safe_token import check_token, generate_token
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
organization_invite_blueprint = Blueprint("organization_invite", __name__)
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
2023-07-10 11:06:29 -07:00
|
|
|
|
register_errors(organization_invite_blueprint)
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@organization_invite_blueprint.route(
|
|
|
|
|
|
"/organization/<uuid:organization_id>/invite", methods=["POST"]
|
|
|
|
|
|
)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def invite_user_to_org(organization_id):
|
2025-06-26 10:35:46 -07:00
|
|
|
|
check_suspicious_id(organization_id)
|
2018-02-19 15:03:36 +00:00
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
validate(data, post_create_invited_org_user_status_schema)
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
2025-09-08 08:17:37 -07:00
|
|
|
|
request_json = request.get_json()
|
|
|
|
|
|
try:
|
|
|
|
|
|
nonce = request_json.pop("nonce")
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
current_app.logger.exception("nonce not found in submitted data.")
|
|
|
|
|
|
raise
|
|
|
|
|
|
try:
|
|
|
|
|
|
state = request_json.pop("state")
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
current_app.logger.exception("state not found in submitted data.")
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
2023-07-10 11:06:29 -07:00
|
|
|
|
invited_org_user = InvitedOrganizationUser(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
email_address=data["email_address"],
|
|
|
|
|
|
invited_by_id=data["invited_by"],
|
|
|
|
|
|
organization_id=organization_id,
|
2018-02-16 10:56:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
save_invited_org_user(invited_org_user)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template = dao_get_template_by_id(
|
|
|
|
|
|
current_app.config["ORGANIZATION_INVITATION_EMAIL_TEMPLATE_ID"]
|
|
|
|
|
|
)
|
2024-04-02 13:18:21 -07:00
|
|
|
|
url = os.environ["LOGIN_DOT_GOV_REGISTRATION_URL"]
|
2025-09-08 08:17:37 -07:00
|
|
|
|
url = url.replace("NONCE", nonce)
|
|
|
|
|
|
url = url.replace("STATE", state)
|
2024-04-02 13:18:21 -07:00
|
|
|
|
|
2024-01-22 10:55:09 -08:00
|
|
|
|
personalisation = {
|
|
|
|
|
|
"user_name": (
|
2024-03-05 11:21:38 -08:00
|
|
|
|
"The Notify.gov team"
|
2024-01-22 10:55:09 -08:00
|
|
|
|
if invited_org_user.invited_by.platform_admin
|
|
|
|
|
|
else invited_org_user.invited_by.name
|
|
|
|
|
|
),
|
|
|
|
|
|
"organization_name": invited_org_user.organization.name,
|
2024-04-02 13:18:21 -07:00
|
|
|
|
"url": url,
|
2024-01-22 10:55:09 -08:00
|
|
|
|
}
|
2018-02-16 10:56:12 +00:00
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
|
recipient=invited_org_user.email_address,
|
|
|
|
|
|
service=template.service,
|
2024-01-22 10:55:09 -08:00
|
|
|
|
personalisation={},
|
2024-02-28 12:40:52 -05:00
|
|
|
|
notification_type=NotificationType.EMAIL,
|
2018-02-16 10:56:12 +00:00
|
|
|
|
api_key_id=None,
|
2024-01-18 10:28:50 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
reply_to_text=invited_org_user.invited_by.email_address,
|
2018-02-16 10:56:12 +00:00
|
|
|
|
)
|
2024-03-22 11:18:47 -07:00
|
|
|
|
|
|
|
|
|
|
saved_notification.personalisation = personalisation
|
2024-01-24 07:55:14 -08:00
|
|
|
|
redis_store.set(
|
|
|
|
|
|
f"email-personalisation-{saved_notification.id}",
|
|
|
|
|
|
json.dumps(personalisation),
|
|
|
|
|
|
ex=1800,
|
|
|
|
|
|
)
|
2024-03-22 11:18:47 -07:00
|
|
|
|
|
2023-08-25 12:09:00 -07:00
|
|
|
|
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
|
|
|
|
|
return jsonify(data=invited_org_user.serialize()), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@organization_invite_blueprint.route(
|
|
|
|
|
|
"/organization/<uuid:organization_id>/invite", methods=["GET"]
|
|
|
|
|
|
)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def get_invited_org_users_by_organization(organization_id):
|
2025-06-26 10:35:46 -07:00
|
|
|
|
|
|
|
|
|
|
check_suspicious_id(organization_id)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
invited_org_users = get_invited_org_users_for_organization(organization_id)
|
2018-02-16 10:56:12 +00:00
|
|
|
|
return jsonify(data=[x.serialize() for x in invited_org_users]), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-10 11:06:29 -07:00
|
|
|
|
@organization_invite_blueprint.route(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/organization/<uuid:organization_id>/invite/<invited_org_user_id>", methods=["GET"]
|
2021-03-11 20:26:44 +00:00
|
|
|
|
)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def get_invited_org_user_by_organization(organization_id, invited_org_user_id):
|
2025-06-26 10:35:46 -07:00
|
|
|
|
check_suspicious_id(organization_id, invited_org_user_id)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
invited_org_user = dao_get_invited_org_user(organization_id, invited_org_user_id)
|
2020-08-17 11:59:55 +01:00
|
|
|
|
return jsonify(data=invited_org_user.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-10 11:06:29 -07:00
|
|
|
|
@organization_invite_blueprint.route(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/organization/<uuid:organization_id>/invite/<invited_org_user_id>",
|
|
|
|
|
|
methods=["POST"],
|
2021-03-11 20:26:44 +00:00
|
|
|
|
)
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def update_org_invite_status(organization_id, invited_org_user_id):
|
2025-06-26 10:35:46 -07:00
|
|
|
|
check_suspicious_id(organization_id, invited_org_user_id)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
fetched = dao_get_invited_org_user(
|
|
|
|
|
|
organization_id=organization_id, invited_org_user_id=invited_org_user_id
|
|
|
|
|
|
)
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
2018-02-19 15:03:36 +00:00
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
validate(data, post_update_invited_org_user_status_schema)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
fetched.status = data["status"]
|
2018-02-19 15:03:36 +00:00
|
|
|
|
save_invited_org_user(fetched)
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
2018-02-16 10:56:12 +00:00
|
|
|
|
return jsonify(data=fetched.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def invited_org_user_url(invited_org_user_id, invite_link_host=None):
|
2025-06-26 10:35:46 -07:00
|
|
|
|
|
2018-02-16 10:56:12 +00:00
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(invited_org_user_id),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
2018-02-16 10:56:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if invite_link_host is None:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
invite_link_host = current_app.config["ADMIN_BASE_URL"]
|
2018-02-16 10:56:12 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return "{0}/organization-invitation/{1}".format(invite_link_host, token)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@organization_invite_blueprint.route(
|
|
|
|
|
|
"/invite/organization/<uuid:invited_org_user_id>", methods=["GET"]
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
def get_invited_org_user(invited_org_user_id):
|
2025-06-26 10:35:46 -07:00
|
|
|
|
check_suspicious_id(invited_org_user_id)
|
2021-03-12 12:50:07 +00:00
|
|
|
|
invited_user = get_invited_org_user_by_id(invited_org_user_id)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
return jsonify(data=invited_user.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@organization_invite_blueprint.route("/invite/organization/<token>", methods=["GET"])
|
|
|
|
|
|
@organization_invite_blueprint.route(
|
|
|
|
|
|
"/invite/organization/check/<token>", methods=["GET"]
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
def validate_invitation_token(token):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
max_age_seconds = 60 * 60 * 24 * current_app.config["INVITATION_EXPIRATION_DAYS"]
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
try:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
invited_user_id = check_token(
|
|
|
|
|
|
token,
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
max_age_seconds,
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
except SignatureExpired:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
errors = {
|
2024-05-15 08:22:04 -07:00
|
|
|
|
"invitation": "Your invitation to Notify.gov has expired. "
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"Please ask the person that invited you to send you another one"
|
|
|
|
|
|
}
|
2021-03-11 20:26:44 +00:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
except BadData:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
errors = {
|
|
|
|
|
|
"invitation": "Something’s wrong with this link. Make sure you’ve copied the whole thing."
|
|
|
|
|
|
}
|
2021-03-11 20:26:44 +00:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
2021-03-12 12:50:07 +00:00
|
|
|
|
invited_user = get_invited_org_user_by_id(invited_user_id)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
return jsonify(data=invited_user.serialize()), 200
|