2025-09-08 08:23:04 -07:00
|
|
|
import json
|
|
|
|
|
import secrets
|
|
|
|
|
from urllib.parse import unquote
|
|
|
|
|
|
|
|
|
|
from flask import current_app, request
|
|
|
|
|
|
|
|
|
|
from app import redis_client
|
2025-07-04 16:37:04 -07:00
|
|
|
from app.enums import InvitedOrgUserStatus
|
2025-07-04 17:52:03 -07:00
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user
|
2025-09-08 08:23:04 -07:00
|
|
|
from app.utils import hilite
|
|
|
|
|
from notifications_utils.url_safe_token import generate_token
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class OrgInviteApiClient(NotifyAdminAPIClient):
|
|
|
|
|
def init_app(self, app):
|
2018-03-07 18:09:33 +00:00
|
|
|
super().init_app(app)
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
self.admin_url = app.config["ADMIN_BASE_URL"]
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
def create_invite(self, invite_from_id, org_id, email_address):
|
|
|
|
|
data = {
|
2023-08-25 09:12:23 -07:00
|
|
|
"email_address": email_address,
|
|
|
|
|
"invited_by": invite_from_id,
|
|
|
|
|
"invite_link_host": self.admin_url,
|
2018-02-19 16:53:29 +00:00
|
|
|
}
|
|
|
|
|
data = _attach_current_user(data)
|
2025-09-08 08:23:04 -07:00
|
|
|
|
|
|
|
|
ttl = 24 * 60 * 60
|
|
|
|
|
# make and store the state
|
|
|
|
|
state = generate_token(
|
|
|
|
|
str(request.remote_addr),
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
)
|
|
|
|
|
state_key = f"login-state-{unquote(state)}"
|
|
|
|
|
redis_client.set(state_key, state, ex=ttl)
|
|
|
|
|
current_app.logger.debug(
|
|
|
|
|
hilite(f"SET THE STATE KEY TO {state} with state_key {state_key}")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# make and store the nonce
|
|
|
|
|
nonce = secrets.token_urlsafe()
|
|
|
|
|
nonce_key = f"login-nonce-{unquote(nonce)}"
|
|
|
|
|
redis_client.set(nonce_key, nonce, ex=ttl) # save the nonce to redis.
|
|
|
|
|
current_app.logger.debug(
|
|
|
|
|
hilite(f"SET THE STATE KEY TO {state} with state_key {state_key}")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
data["nonce"] = nonce # This is passed to api for the invite url.
|
|
|
|
|
data["state"] = state # This is passed to api for the invite url.
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
resp = self.post(url="/organization/{}/invite".format(org_id), data=data)
|
2025-09-08 08:23:04 -07:00
|
|
|
current_app.logger.debug(hilite(f"RESP is {resp}"))
|
|
|
|
|
|
|
|
|
|
invite_data_key = f"invitedata-{unquote(state)}"
|
2025-09-08 11:57:43 -07:00
|
|
|
# For historical reasons 'invite' signifies a service invite
|
|
|
|
|
# and 'data' signifies an org invite
|
|
|
|
|
if resp.get("invite"):
|
|
|
|
|
redis_invite_data = resp["invite"]
|
|
|
|
|
else:
|
|
|
|
|
redis_invite_data = resp["data"]
|
2025-09-08 08:23:04 -07:00
|
|
|
redis_invite_data = json.dumps(redis_invite_data)
|
|
|
|
|
redis_client.set(invite_data_key, redis_invite_data, ex=ttl)
|
|
|
|
|
current_app.logger.debug(
|
|
|
|
|
hilite(f"SET invite_data_key {invite_data_key} to {redis_invite_data}")
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return resp["data"]
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
def get_invites_for_organization(self, org_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
endpoint = "/organization/{}/invite".format(org_id)
|
2018-02-19 16:53:29 +00:00
|
|
|
resp = self.get(endpoint)
|
2023-08-25 09:12:23 -07:00
|
|
|
return resp["data"]
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2021-03-05 17:04:35 +00:00
|
|
|
def get_invited_user_for_org(self, org_id, invited_org_user_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
return self.get(f"/organization/{org_id}/invite/{invited_org_user_id}")["data"]
|
2020-08-17 14:30:09 +01:00
|
|
|
|
store invited org user ids in session
first of a two step process to remove invited user objects from the
session. we're removing them because they're of variable size, and with
a lot of folder permissions they can cause the session to exceed the 4kb
cookie size limit and not save properly.
this commit looks at invited org users only.
in this step, start saving the invited org user's id to the
session alongside the session object. Then, if the invited_org_user_id
is present in the next step of the invite flow, fetch the user object
from the API instead of from the session. If it's not present (due to a
session set by an older instance of the admin app), then just use the
old code to get the entire object out of the session.
For invites where the user is small enough to persist to the cookie,
this will still save both the old and the new way, but will always make
an extra check to the API, I think this minor performance hit is totally
fine. For invites where the user is too big to persist, they'll still
fail for now, and will need to wait until the next PR comes along and
stops saving the large invited user object to the session entirely.
2021-03-08 10:01:12 +00:00
|
|
|
def get_invited_user(self, invited_user_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
return self.get(f"/invite/organization/{invited_user_id}")["data"]
|
store invited org user ids in session
first of a two step process to remove invited user objects from the
session. we're removing them because they're of variable size, and with
a lot of folder permissions they can cause the session to exceed the 4kb
cookie size limit and not save properly.
this commit looks at invited org users only.
in this step, start saving the invited org user's id to the
session alongside the session object. Then, if the invited_org_user_id
is present in the next step of the invite flow, fetch the user object
from the API instead of from the session. If it's not present (due to a
session set by an older instance of the admin app), then just use the
old code to get the entire object out of the session.
For invites where the user is small enough to persist to the cookie,
this will still save both the old and the new way, but will always make
an extra check to the API, I think this minor performance hit is totally
fine. For invites where the user is too big to persist, they'll still
fail for now, and will need to wait until the next PR comes along and
stops saving the large invited user object to the session entirely.
2021-03-08 10:01:12 +00:00
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
def check_token(self, token):
|
2023-08-25 09:12:23 -07:00
|
|
|
resp = self.get(url="/invite/organization/check/{}".format(token))
|
|
|
|
|
return resp["data"]
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
def cancel_invited_user(self, org_id, invited_user_id):
|
2025-07-04 17:42:20 -07:00
|
|
|
data = {"status": InvitedOrgUserStatus.CANCELLED}
|
2018-02-19 16:53:29 +00:00
|
|
|
data = _attach_current_user(data)
|
2023-08-25 09:12:23 -07:00
|
|
|
self.post(
|
|
|
|
|
url="/organization/{0}/invite/{1}".format(org_id, invited_user_id),
|
|
|
|
|
data=data,
|
|
|
|
|
)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
def accept_invite(self, org_id, invited_user_id):
|
2025-07-04 17:42:20 -07:00
|
|
|
data = {"status": InvitedOrgUserStatus.ACCEPTED}
|
2023-08-25 09:12:23 -07:00
|
|
|
self.post(
|
|
|
|
|
url="/organization/{0}/invite/{1}".format(org_id, invited_user_id),
|
|
|
|
|
data=data,
|
|
|
|
|
)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2018-10-26 15:39:32 +01:00
|
|
|
|
|
|
|
|
org_invite_api_client = OrgInviteApiClient()
|