"""User id handling helpers.""" from assets.constants import field_const HEADERS_AUTH = 'headers_auth' BODY_AUTH = 'body_auth' def get_user_id_from_headers_or_body(request): """Get user id from request object. Args: request (werkzeug.local.LocalProxy): request object from the handler. Returns: tuple: Tuple of user id and auth type """ if request.headers.get(field_const.ORCHARD_USER_ID): full_user_id = request.headers[field_const.ORCHARD_USER_ID] return full_user_id, HEADERS_AUTH body = request.get_json(silent=True) if body: full_user_id = body.get(field_const.ORCHARD_USER_ID) if (full_user_id and (full_user_id.startswith('alw:') or full_user_id.startswith('oa:'))): return full_user_id, BODY_AUTH return None, None def get_user_data(user_info_string): """Return orchard user id and type from user info string. Args: user_info_string(str): String with user info. Returns: dict: Dictionary with user id and type. """ data = user_info_string.split(':') if len(data) == 1: data.append('0') return { 'id': int(data[1]), 'type': data[0] }