def agent_profile_put(req_dict): rogueparams = set(req_dict["params"]) - set(["agent", "profileId"]) if rogueparams: raise ParamError("The put agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if "agent" in req_dict["params"]: try: agent = json.loads(req_dict["params"]["agent"]) req_dict["params"]["agent"] = agent except Exception: raise ParamError("agent param for agent profile is not valid") validator.validate_agent(agent, "agent param for agent profile") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict["method"] raise ParamError(err_msg) if not "profileId" in req_dict["params"]: err_msg = "Error -- agent_profile - method = %s, but profileId parameter missing.." % req_dict["method"] raise ParamError(err_msg) if "body" not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Extra validation if oauth if req_dict["auth"]["type"] == "oauth": validate_oauth_state_or_profile_agent(req_dict, "profile") req_dict["profile"] = req_dict.pop("raw_body", req_dict.pop("body", None)) return req_dict
def agent_profile_delete(req_dict): rogueparams = set(req_dict['params']) - set(["agent", "profileId"]) if rogueparams: raise ParamError("The delete agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = json.loads(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param for agent profile is not valid") validator.validate_agent(agent, "agent param for agent profile") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict['method'] raise ParamError(err_msg) if not 'profileId' in req_dict['params']: err_msg = "Error -- agent_profile - method = %s, but profileId parameter missing.." % req_dict['method'] raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_state_or_profile_agent(req_dict, "profile") return req_dict
def activity_profile_put(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "profileId"]) if rogueparams: raise ParamError( "The put activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri( req_dict['params']['activityId'], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'profileId' not in req_dict['params']: err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Set profile - req_parse converts all request bodies to dict, act profile needs it as string and need to replace single quotes with double quotes # b/c of quotation issue when using javascript with activity profile req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
def agent_profile_get(req_dict): rogueparams = set(req_dict["params"]) - set(["agent", "profileId", "since"]) if rogueparams: raise ParamError("The get agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if "agent" in req_dict["params"]: try: agent = json.loads(req_dict["params"]["agent"]) req_dict["params"]["agent"] = agent except Exception: raise ParamError("agent param for agent profile is not valid") validator.validate_agent(agent, "agent param for agent profile") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict["method"] raise ParamError(err_msg) if "since" in req_dict["params"]: try: parse_datetime(req_dict["params"]["since"]) except (Exception, ISO8601Error): raise ParamError("Since parameter was not a valid ISO8601 timestamp") # Extra validation if oauth if req_dict["auth"]["type"] == "oauth": validate_oauth_state_or_profile_agent(req_dict, "profile") return req_dict
def agent_profile_put(req_dict): rogueparams = set(req_dict['params']) - set(["agent", "profileId"]) if rogueparams: raise ParamError( "The put agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'profileId' not in req_dict['params']: err_msg = "Error -- agent_profile - method = %s, but profileId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "agent profile") req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
def activities_get(req_dict): rogueparams = set(req_dict['params']) - set(["activityId"]) if rogueparams: raise ParamError( "The get activities request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() try: activity_id = req_dict['params']['activityId'] except KeyError: err_msg = "Error -- activities - method = %s, but activityId parameter is missing" % req_dict[ 'method'] raise ParamError(err_msg) else: validator.validate_iri( activity_id, "activityId param") # Try to retrieve activity, if DNE then return empty else return activity # info try: Activity.objects.get(activity_id=activity_id, authority__isnull=False) except Activity.DoesNotExist: err_msg = "No activity found with ID %s" % activity_id raise IDNotFoundError(err_msg) return req_dict
def server_validation(stmt_set, auth, payload_sha2s): auth_validated = False if type(stmt_set) is list: for stmt in stmt_set: server_validation(stmt, auth, payload_sha2s) else: if 'id' in stmt_set: statement_id = stmt_set['id'] if check_for_existing_statementId(statement_id): err_msg = "A statement with ID %s already exists" % statement_id raise ParamConflict(err_msg) server_validate_statement_object(stmt_set['object'], auth) if stmt_set['verb']['id'] == 'http://adlnet.gov/expapi/verbs/voided': validate_void_statement(stmt_set['object']['id']) if not 'objectType' in stmt_set['object'] or stmt_set['object']['objectType'] == 'Activity': get_act_def_data(stmt_set['object']) try: validator = StatementValidator(None) validator.validate_activity(stmt_set['object']) except Exception, e: raise BadRequest(e.message) except ParamError, e: raise ParamError(e.message)
def agent_profile_get(req_dict): rogueparams = set(req_dict['params']) - \ set(["agent", "profileId", "since"]) if rogueparams: raise ParamError( "The get agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'since' in req_dict['params']: try: parse_datetime(req_dict['params']['since']) except (Exception, ISO8601Error): raise ParamError( "Since parameter was not a valid ISO8601 timestamp") # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "agent profile") return req_dict
def activity_profile_post(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "profileId"]) if rogueparams: raise ParamError("The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_uri(req_dict['params']['activityId'], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method'] raise ParamError(err_msg) if not 'profileId' in req_dict['params']: err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict['method'] raise ParamError(err_msg) if 'headers' not in req_dict or ('CONTENT_TYPE' not in req_dict['headers'] or req_dict['headers']['CONTENT_TYPE'] != "application/json"): err_msg = "The content type for activity profile POSTs must be application/json" raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
def agents_get(req_dict): rogueparams = set(req_dict['params']) - set(["agent"]) if rogueparams: raise ParamError( "The get agent request contained unexpected parameters: %s" % ", ".join(rogueparams)) try: req_dict['params']['agent'] except KeyError: err_msg = "Error -- agents url, but no agent parameter.. the agent parameter is required" raise ParamError(err_msg) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") params = get_agent_ifp(agent) if not Agent.objects.filter(**params).exists(): raise IDNotFoundError( "Error with Agent. The agent partial did not match any agents on record") req_dict['agent_ifp'] = params return req_dict
def activity_state_put(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError("The put activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_uri(req_dict['params']['activityId'], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict['method'] raise ParamError(err_msg) if not 'stateId' in req_dict['params']: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict['method'] raise ParamError(err_msg) if 'registration' in req_dict['params']: if not validate_uuid(req_dict['params']['registration']): raise ParamError("%s is not a valid uuid for the registration parameter") if 'agent' in req_dict['params']: try: agent = json.loads(req_dict['params']['agent']) except Exception, e: raise ParamError("agent param for activity state is not valid") validator.validate_agent(agent, "Activity state agent param")
def statements_post(req_dict): if req_dict['params'].keys(): raise ParamError("The post statements request contained unexpected parameters: %s" % ", ".join(req_dict['params'].keys())) try: validator = StatementValidator(req_dict['body']) validator.validate() except Exception, e: raise BadRequest(e.message)
def statements_post(req_dict): if req_dict['params'].keys() and not ignore_rogue_params: raise ParamError("The post statements request contained unexpected parameters: %s" % ", ".join(req_dict['params'].keys())) if isinstance(req_dict['body'], basestring): req_dict['body'] = convert_to_dict(req_dict['body']) try: validator = StatementValidator(req_dict['body']) validator.validate() except Exception, e: raise BadRequest(e.message)
def agent_profile_post(req_dict): rogueparams = set(req_dict['params']) - set(["agent", "profileId"]) if rogueparams: raise ParamError( "The post agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = json.loads(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param for agent profile is not valid") validator.validate_agent(agent, "agent param for agent profile") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'profileId' not in req_dict['params']: err_msg = "Error -- agent_profile - method = %s, but profileId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_state_or_profile_agent(req_dict, "profile") # Check the content type if the document already exists exists = False agent = req_dict['params']['agent'] a = Agent.objects.retrieve_or_create(**agent)[0] try: p = AgentProfile.objects.get( profile_id=req_dict['params']['profileId'], agent=a) exists = True except AgentProfile.DoesNotExist: pass if exists: if str(p.content_type) != "application/json" or ("application/json" not in req_dict['headers']['CONTENT_TYPE'] or req_dict['headers']['CONTENT_TYPE'] != "application/json"): raise ParamError( "Neither original document or document to be posted has a Content-Type of 'application/json'") # Set profile req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
def agent_profile_delete(req_dict): rogueparams = set(req_dict['params']) - set(["agent", "profileId"]) if rogueparams: raise ParamError("The delete agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = json.loads(req_dict['params']['agent']) except Exception, e: raise ParamError("agent param for agent profile is not valid") validator.validate_agent(agent, "agent param for agent profile")
def activity_profile_post(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "profileId"]) if rogueparams: raise ParamError( "The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri( req_dict['params']['activityId'], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'profileId' not in req_dict['params']: err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Check json for incoming POSTed document if "application/json" not in req_dict['headers']['CONTENT_TYPE']: raise ParamError( "Activity profile document to be posted does not have a Content-Type of 'application/json'") # expected to be json try: raw_profile = req_dict.pop('raw_body', req_dict.pop('body', None)) convert_to_datatype(raw_profile) except Exception: raise ParamError("Activity profile document is not valid JSON") else: req_dict['profile'] = raw_profile # Check the content type if the document already exists exists = False try: p = ActivityProfile.objects.get(activity_id=req_dict['params']['activityId'], profile_id=req_dict['params']['profileId']) exists = True except ActivityProfile.DoesNotExist: pass # Since document to be POSTed has to be json, so does the existing document if exists and str(p.content_type) != "application/json": raise ParamError("Activity profile already exists but is not JSON, cannot update it with new JSON document") return req_dict
def activity_profile_delete(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "profileId"]) if rogueparams: raise ParamError("The delete activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method'] raise ParamError(err_msg) if not 'profileId' in req_dict['params']: err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict['method'] raise ParamError(err_msg) return req_dict
def statements_put(req_dict): # Find any unexpected parameters rogueparams = set(req_dict['params']) - set(["statementId"]) if rogueparams: raise ParamError( "The put statements request contained unexpected parameters: %s" % ", ".join(rogueparams)) # Statement id can must be supplied in query param. If in the body too, it # must be the same if 'statementId' not in req_dict['params']: raise ParamError( "Error -- statements - method = %s, but no statementId parameter or ID given in statement" % req_dict['method']) else: statement_id = req_dict['params']['statementId'] # Try to get id if in body try: statement_body_id = req_dict['body']['id'] except Exception as e: statement_body_id = None # If ids exist in both places, check if they are equal if statement_body_id and statement_id != statement_body_id: raise ParamError( "Error -- statements - method = %s, param and body ID both given, but do not match" % req_dict['method']) # Set id inside of statement with param id if not statement_body_id: req_dict['body']['id'] = statement_id # If there are no other params-raise param error since nothing else is # supplied if not check_for_no_other_params_supplied(req_dict['body']): raise ParamError("No other params are supplied with statementId.") # Validate statement in body try: validator = StatementValidator(req_dict['body']) validator.validate() except Exception as e: raise BadRequest(e.message) except ParamError as e: raise ParamError(e.message) validate_body([req_dict['body']], req_dict['auth'], req_dict.get( 'payload_sha2s', None), req_dict['headers']['CONTENT_TYPE']) return req_dict
def activity_state_delete(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError("The delete activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict['method'] raise ParamError(err_msg) if 'registration' in req_dict['params']: validator.validate_uuid(req_dict['params']['registration'], "registration param for activity state") if 'agent' in req_dict['params']: try: agent = json.loads(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param for activity state is not valid") validator.validate_agent(agent, "Activity state agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict['method'] raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_state_or_profile_agent(req_dict, "state") return req_dict
def activity_profile_post(req_dict): rogueparams = set(req_dict["params"]) - set(["activityId", "profileId"]) if rogueparams: raise ParamError( "The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams) ) validator = StatementValidator() if "activityId" in req_dict["params"]: validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict["method"] raise ParamError(err_msg) if not "profileId" in req_dict["params"]: err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict["method"] raise ParamError(err_msg) if "body" not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Check the content type if the document already exists exists = False try: p = ActivityProfile.objects.get( activityId=req_dict["params"]["activityId"], profileId=req_dict["params"]["profileId"] ) exists = True except ActivityProfile.DoesNotExist: pass if exists: if str(p.content_type) != "application/json" or ( "application/json" not in req_dict["headers"]["CONTENT_TYPE"] or req_dict["headers"]["CONTENT_TYPE"] != "application/json" ): raise ParamError( "Neither original document or document to be posted has a Content-Type of 'application/json'" ) req_dict["profile"] = req_dict.pop("raw_body", req_dict.pop("body", None)) return req_dict
def activity_profile_get(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "profileId", "since"]) if rogueparams: raise ParamError("The get activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method'] raise ParamError(err_msg) if 'since' in req_dict['params']: try: parse_datetime(req_dict['params']['since']) except (Exception, ISO8601Error): raise ParamError("Since parameter was not a valid ISO8601 timestamp") return req_dict
def statements_post(req_dict): if req_dict['params'].keys(): raise ParamError("The post statements request contained unexpected parameters: %s" % ", ".join( req_dict['params'].keys())) try: validator = StatementValidator(req_dict['body']) validator.validate() except Exception as e: raise BadRequest(e.message) except ParamError as e: raise ParamError(e.message) if isinstance(req_dict['body'], dict): body = [req_dict['body']] else: body = req_dict['body'] validate_body(body, req_dict['auth'], req_dict['headers']['CONTENT_TYPE']) return req_dict
def activity_profile_post(req_dict): rogueparams = set(req_dict['params']) - set(["activityId", "profileId"]) if rogueparams and not ignore_rogue_params: raise ParamError("The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity profile") else: err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method'] raise ParamError(err_msg) if not 'profileId' in req_dict['params']: err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict['method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
def activity_state_put(req_dict): rogueparams = set(req_dict['params']) - \ set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError( "The put activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri( req_dict['params']['activityId'], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'stateId' not in req_dict['params']: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'registration' in req_dict['params']: validator.validate_uuid( req_dict['params']['registration'], "registration param for activity state") if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) # Must have body included for state if 'body' not in req_dict: err_msg = "Could not find the state" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "state") # Set state req_dict['state'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
def activity_state_put(req_dict): rogueparams = set(req_dict["params"]) - set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError("The put activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if "activityId" in req_dict["params"]: validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict["method"] raise ParamError(err_msg) if not "stateId" in req_dict["params"]: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict["method"] raise ParamError(err_msg) if "registration" in req_dict["params"]: validator.validate_uuid(req_dict["params"]["registration"], "registration param for activity state") if "agent" in req_dict["params"]: try: agent = json.loads(req_dict["params"]["agent"]) req_dict["params"]["agent"] = agent except Exception: raise ParamError("agent param for activity state is not valid") validator.validate_agent(agent, "Activity state agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict["method"] raise ParamError(err_msg) # Must have body included for state if "body" not in req_dict: err_msg = "Could not find the state" raise ParamError(err_msg) # Extra validation if oauth if req_dict["auth"]["type"] == "oauth": validate_oauth_state_or_profile_agent(req_dict, "state") # Set state req_dict["state"] = req_dict.pop("raw_body", req_dict.pop("body", None)) return req_dict
def activity_state_post(req_dict): rogueparams = set(req_dict['params']) - \ set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError( "The post activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'stateId' not in req_dict['params']: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'registration' in req_dict['params']: validator.validate_uuid(req_dict['params']['registration'], "registration param for activity state") if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) # Must have body included for state if 'body' not in req_dict: err_msg = "Could not find the state" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "state") # Check json for incoming POSTed document if "application/json" not in req_dict['headers']['CONTENT_TYPE']: raise ParamError( "Activity state document to be posted does not have a Content-Type of 'application/json'" ) # expected to be json try: raw_state = req_dict.pop('raw_body', req_dict.pop('body', None)) convert_to_datatype(raw_state) except Exception: raise ParamError("Activity state document is not valid JSON") else: req_dict['state'] = raw_state # Check the content type if the document already exists registration = req_dict['params'].get('registration', None) agent = req_dict['params']['agent'] a = Agent.objects.retrieve_or_create(**agent)[0] exists = False if registration: try: s = ActivityState.objects.get( state_id=req_dict['params']['stateId'], agent=a, activity_id=req_dict['params']['activityId'], registration_id=req_dict['params']['registration']) exists = True except ActivityState.DoesNotExist: pass else: try: s = ActivityState.objects.get( state_id=req_dict['params']['stateId'], agent=a, activity_id=req_dict['params']['activityId']) exists = True except ActivityState.DoesNotExist: pass if exists and str(s.content_type) != "application/json": raise ParamError( "Activity state already exists but is not JSON, cannot update it with new JSON document" ) return req_dict
def activity_state_post(req_dict): rogueparams = set(req_dict['params']) - set( ["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError( "The post activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if not 'stateId' in req_dict['params']: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'registration' in req_dict['params']: validator.validate_uuid(req_dict['params']['registration'], "registration param for activity state") if 'agent' in req_dict['params']: try: agent = json.loads(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param for activity state is not valid") validator.validate_agent(agent, "Activity state agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) # Must have body included for state if 'body' not in req_dict: err_msg = "Could not find the state" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_state_or_profile_agent(req_dict, "state") # Check the content type if the document already exists registration = req_dict['params'].get('registration', None) agent = req_dict['params']['agent'] a = Agent.objects.retrieve_or_create(**agent)[0] exists = False if registration: try: s = ActivityState.objects.get( state_id=req_dict['params']['stateId'], agent=a, activity_id=req_dict['params']['activityId'], registration_id=req_dict['params']['registration']) exists = True except ActivityState.DoesNotExist: pass else: try: s = ActivityState.objects.get( state_id=req_dict['params']['stateId'], agent=a, activity_id=req_dict['params']['activityId']) exists = True except ActivityState.DoesNotExist: pass if exists: if str(s.content_type) != "application/json" or ("application/json" not in req_dict['headers']['CONTENT_TYPE'] or \ req_dict['headers']['CONTENT_TYPE'] != "application/json"): raise ParamError( "Neither original document or document to be posted has a Content-Type of 'application/json'" ) # Set state req_dict['state'] = req_dict.pop('raw_body', req_dict.pop('body', None)) return req_dict
# If statement with that ID already exists-raise conflict error if check_for_existing_statementId(statement_id): raise ParamConflict("A statement with ID %s already exists" % statement_id) # Set id inside of statement with param id if not statement_body_id: req_dict['body']['id'] = statement_id # If there are no other params-raise param error since nothing else is supplied if not check_for_no_other_params_supplied(req_dict['body']): raise ParamError("No other params are supplied with statementId.") # Validate statement in body try: validator = StatementValidator(req_dict['body']) validator.validate() except Exception, e: raise BadRequest(e.message) except ParamError, e: raise ParamError(e.message) validate_body([req_dict['body']], req_dict['auth'], req_dict.get('payload_sha2s', None)) return req_dict def validate_attachments(attachment_data, payload_sha2s): # For each attachment that is in the actual statement for attachment in attachment_data: # If the attachment data has a sha2 field, must validate it against the payload data if 'sha2' in attachment: sha2 = attachment['sha2'] # Check if the sha2 field is a key in the payload dict
def activity_state_post(req_dict): rogueparams = set(req_dict["params"]) - set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError("The post activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if "activityId" in req_dict["params"]: validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict["method"] raise ParamError(err_msg) if not "stateId" in req_dict["params"]: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict["method"] raise ParamError(err_msg) if "registration" in req_dict["params"]: validator.validate_uuid(req_dict["params"]["registration"], "registration param for activity state") if "agent" in req_dict["params"]: try: agent = json.loads(req_dict["params"]["agent"]) req_dict["params"]["agent"] = agent except Exception: raise ParamError("agent param for activity state is not valid") validator.validate_agent(agent, "Activity state agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict["method"] raise ParamError(err_msg) # Must have body included for state if "body" not in req_dict: err_msg = "Could not find the state" raise ParamError(err_msg) # Extra validation if oauth if req_dict["auth"]["type"] == "oauth": validate_oauth_state_or_profile_agent(req_dict, "state") # Check the content type if the document already exists registration = req_dict["params"].get("registration", None) agent = req_dict["params"]["agent"] a = Agent.objects.retrieve_or_create(**agent)[0] exists = False if registration: try: s = ActivityState.objects.get( state_id=req_dict["params"]["stateId"], agent=a, activity_id=req_dict["params"]["activityId"], registration_id=req_dict["params"]["registration"], ) exists = True except ActivityState.DoesNotExist: pass else: try: s = ActivityState.objects.get( state_id=req_dict["params"]["stateId"], agent=a, activity_id=req_dict["params"]["activityId"] ) exists = True except ActivityState.DoesNotExist: pass if exists: if str(s.content_type) != "application/json" or ( "application/json" not in req_dict["headers"]["CONTENT_TYPE"] or req_dict["headers"]["CONTENT_TYPE"] != "application/json" ): raise ParamError( "Neither original document or document to be posted has a Content-Type of 'application/json'" ) # Set state req_dict["state"] = req_dict.pop("raw_body", req_dict.pop("body", None)) return req_dict
def agent_profile_post(req_dict): rogueparams = set(req_dict['params']) - set(["agent", "profileId"]) if rogueparams: raise ParamError( "The post agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'profileId' not in req_dict['params']: err_msg = "Error -- agent_profile - method = %s, but profileId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "agent profile") # Check json for incoming POSTed document if "application/json" not in req_dict['headers']['CONTENT_TYPE']: raise ParamError( "Agent profile document to be posted does not have a Content-Type of 'application/json'") # expected to be json try: raw_profile = req_dict.pop('raw_body', req_dict.pop('body', None)) convert_to_datatype(raw_profile) except Exception: raise ParamError("Agent profile document is not valid JSON") else: req_dict['profile'] = raw_profile # Check the content type if the document already exists exists = False agent = req_dict['params']['agent'] a = Agent.objects.retrieve_or_create(**agent)[0] try: p = AgentProfile.objects.get( profile_id=req_dict['params']['profileId'], agent=a) exists = True except AgentProfile.DoesNotExist: pass # Since document to be POSTed has to be json, so does the existing document if exists and str(p.content_type) != "application/json": raise ParamError("Agent profile already exists but is not JSON, cannot update it with new JSON document") return req_dict
if statement_body_id and statement_id != statement_body_id: raise ParamError( "Error -- statements - method = %s, param and body ID both given, but do not match" % req_dict['method']) # Set id inside of statement with param id if not statement_body_id: req_dict['body']['id'] = statement_id # If there are no other params-raise param error since nothing else is supplied if not check_for_no_other_params_supplied(req_dict['body']): raise ParamError("No other params are supplied with statementId.") # Validate statement in body try: validator = StatementValidator(req_dict['body']) validator.validate() except Exception, e: raise BadRequest(e.message) except ParamError, e: raise ParamError(e.message) validate_body([req_dict['body']], req_dict['auth'], req_dict.get('payload_sha2s', None), req_dict['headers']['CONTENT_TYPE']) return req_dict def validate_attachments(attachment_data, payload_sha2s, content_type): if "multipart/mixed" in content_type: for attachment in attachment_data: # If the attachment data has a sha2 field, must validate it against the payload data
def statements_get(req_dict): rogueparams = set(req_dict['params']) - set(["statementId", "voidedStatementId", "agent", "verb", "activity", "registration", "related_activities", "related_agents", "since", "until", "limit", "format", "attachments", "ascending"]) if rogueparams: raise ParamError( "The get statements request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") formats = ['exact', 'canonical', 'ids'] if 'format' in req_dict['params']: if req_dict['params']['format'] not in formats: raise ParamError("The format filter value (%s) was not one of the known values: %s" % ( req_dict['params']['format'], ','.join(formats))) else: req_dict['params']['format'] = 'exact' # StatementId could be for voided statement as well if 'statementId' in req_dict['params'] or 'voidedStatementId' in req_dict['params']: req_dict['statementId'] = validate_statementId(req_dict) if 'since' in req_dict['params']: try: parse_datetime(req_dict['params']['since']) except (Exception, ISO8601Error): raise ParamError( "since parameter was not a valid ISO8601 timestamp") if 'until' in req_dict['params']: try: parse_datetime(req_dict['params']['until']) except (Exception, ISO8601Error): raise ParamError( "until parameter was not a valid ISO8601 timestamp") if 'ascending' in req_dict['params']: if req_dict['params']['ascending'].lower() == 'true': req_dict['params']['ascending'] = True elif req_dict['params']['ascending'].lower() == 'false': req_dict['params']['ascending'] = False else: raise ParamError( "ascending parameter was not a boolean value") else: req_dict['params']['ascending'] = False if 'related_agents' in req_dict['params']: if req_dict['params']['related_agents'].lower() == 'true': req_dict['params']['related_agents'] = True elif req_dict['params']['related_agents'].lower() == 'false': req_dict['params']['related_agents'] = False else: raise ParamError( "related_agents parameter was not a boolean value") else: req_dict['params']['related_agents'] = False if 'related_activities' in req_dict['params']: if req_dict['params']['related_activities'].lower() == 'true': req_dict['params']['related_activities'] = True elif req_dict['params']['related_activities'].lower() == 'false': req_dict['params']['related_activities'] = False else: raise ParamError( "related_activities parameter was not a boolean value") else: req_dict['params']['related_activities'] = False if 'attachments' in req_dict['params']: if req_dict['params']['attachments'].lower() == 'true': req_dict['params']['attachments'] = True elif req_dict['params']['attachments'].lower() == 'false': req_dict['params']['attachments'] = False else: raise ParamError( "attachments parameter was not a boolean value") else: req_dict['params']['attachments'] = False if 'limit' in req_dict['params']: try: req_dict['params']['limit'] = int(req_dict['params']['limit']) except Exception: raise ParamError( "limit parameter was not a non-negative integer") else: if req_dict['params']['limit'] < 0: raise ParamError( "limit parameter was not a non-negative integer") else: req_dict['params']['limit'] = 0 if 'registration' in req_dict['params']: validator.validate_uuid(req_dict['params']['registration'], "Registration param") if 'verb' in req_dict['params']: validator.validate_iri( req_dict['params']['verb'], "verb param") return req_dict
def activity_state_post(req_dict): rogueparams = set(req_dict['params']) - \ set(["activityId", "agent", "stateId", "registration"]) if rogueparams: raise ParamError( "The post activity state request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'activityId' in req_dict['params']: validator.validate_iri( req_dict['params']['activityId'], "activityId param for activity state") else: err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'stateId' not in req_dict['params']: err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'registration' in req_dict['params']: validator.validate_uuid( req_dict['params']['registration'], "registration param for activity state") if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[ 'method'] raise ParamError(err_msg) # Must have body included for state if 'body' not in req_dict: err_msg = "Could not find the state" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "state") # Check json for incoming POSTed document if "application/json" not in req_dict['headers']['CONTENT_TYPE']: raise ParamError( "Activity state document to be posted does not have a Content-Type of 'application/json'") # expected to be json try: raw_state = req_dict.pop('raw_body', req_dict.pop('body', None)) convert_to_datatype(raw_state) except Exception: raise ParamError("Activity state document is not valid JSON") else: req_dict['state'] = raw_state # Check the content type if the document already exists registration = req_dict['params'].get('registration', None) agent = req_dict['params']['agent'] a = Agent.objects.retrieve_or_create(**agent)[0] exists = False if registration: try: s = ActivityState.objects.get(state_id=req_dict['params']['stateId'], agent=a, activity_id=req_dict['params']['activityId'], registration_id=req_dict['params']['registration']) exists = True except ActivityState.DoesNotExist: pass else: try: s = ActivityState.objects.get(state_id=req_dict['params']['stateId'], agent=a, activity_id=req_dict['params']['activityId']) exists = True except ActivityState.DoesNotExist: pass if exists and str(s.content_type) != "application/json": raise ParamError("Activity state already exists but is not JSON, cannot update it with new JSON document") return req_dict
def statements_get(req_dict): rogueparams = set(req_dict['params']) - set([ "statementId", "voidedStatementId", "agent", "verb", "activity", "registration", "related_activities", "related_agents", "since", "until", "limit", "format", "attachments", "ascending" ]) if rogueparams: raise ParamError( "The get statements request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") formats = ['exact', 'canonical', 'ids'] if 'format' in req_dict['params']: if req_dict['params']['format'] not in formats: raise ParamError( "The format filter value (%s) was not one of the known values: %s" % (req_dict['params']['format'], ','.join(formats))) else: req_dict['params']['format'] = 'exact' # StatementId could be for voided statement as well if 'statementId' in req_dict['params'] or 'voidedStatementId' in req_dict[ 'params']: req_dict['statementId'] = validate_statementId(req_dict) if 'since' in req_dict['params']: try: parse_datetime(req_dict['params']['since']) except (Exception, ISO8601Error): raise ParamError( "since parameter was not a valid ISO8601 timestamp") if 'until' in req_dict['params']: try: parse_datetime(req_dict['params']['until']) except (Exception, ISO8601Error): raise ParamError( "until parameter was not a valid ISO8601 timestamp") if 'ascending' in req_dict['params']: if req_dict['params']['ascending'].lower() == 'true': req_dict['params']['ascending'] = True elif req_dict['params']['ascending'].lower() == 'false': req_dict['params']['ascending'] = False else: raise ParamError("ascending parameter was not a boolean value") else: req_dict['params']['ascending'] = False if 'related_agents' in req_dict['params']: if req_dict['params']['related_agents'].lower() == 'true': req_dict['params']['related_agents'] = True elif req_dict['params']['related_agents'].lower() == 'false': req_dict['params']['related_agents'] = False else: raise ParamError( "related_agents parameter was not a boolean value") else: req_dict['params']['related_agents'] = False if 'related_activities' in req_dict['params']: if req_dict['params']['related_activities'].lower() == 'true': req_dict['params']['related_activities'] = True elif req_dict['params']['related_activities'].lower() == 'false': req_dict['params']['related_activities'] = False else: raise ParamError( "related_activities parameter was not a boolean value") else: req_dict['params']['related_activities'] = False if 'attachments' in req_dict['params']: if req_dict['params']['attachments'].lower() == 'true': req_dict['params']['attachments'] = True elif req_dict['params']['attachments'].lower() == 'false': req_dict['params']['attachments'] = False else: raise ParamError("attachments parameter was not a boolean value") else: req_dict['params']['attachments'] = False if 'limit' in req_dict['params']: try: req_dict['params']['limit'] = int(req_dict['params']['limit']) except Exception: raise ParamError("limit parameter was not a non-negative integer") else: if req_dict['params']['limit'] < 0: raise ParamError( "limit parameter was not a non-negative integer") else: req_dict['params']['limit'] = 0 if 'registration' in req_dict['params']: validator.validate_uuid(req_dict['params']['registration'], "Registration param") if 'verb' in req_dict['params']: validator.validate_iri(req_dict['params']['verb'], "verb param") return req_dict
def agent_profile_post(req_dict): rogueparams = set(req_dict['params']) - set(["agent", "profileId"]) if rogueparams: raise ParamError( "The post agent profile request contained unexpected parameters: %s" % ", ".join(rogueparams)) validator = StatementValidator() if 'agent' in req_dict['params']: try: agent = convert_to_datatype(req_dict['params']['agent']) req_dict['params']['agent'] = agent except Exception: raise ParamError("agent param %s is not valid" % \ req_dict['params']['agent']) validator.validate_agent(agent, "Agent param") else: err_msg = "Error -- agent_profile - method = %s, but agent parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'profileId' not in req_dict['params']: err_msg = "Error -- agent_profile - method = %s, but profileId parameter missing.." % req_dict[ 'method'] raise ParamError(err_msg) if 'body' not in req_dict: err_msg = "Could not find the profile document" raise ParamError(err_msg) # Extra validation if oauth if req_dict['auth']['type'] == 'oauth': validate_oauth_for_documents(req_dict, "agent profile") # Check json for incoming POSTed document if "application/json" not in req_dict['headers']['CONTENT_TYPE']: raise ParamError( "Agent profile document to be posted does not have a Content-Type of 'application/json'" ) # expected to be json try: raw_profile = req_dict.pop('raw_body', req_dict.pop('body', None)) convert_to_datatype(raw_profile) except Exception: raise ParamError("Agent profile document is not valid JSON") else: req_dict['profile'] = raw_profile # Check the content type if the document already exists exists = False agent = req_dict['params']['agent'] a = Agent.objects.retrieve_or_create(**agent)[0] try: p = AgentProfile.objects.get( profile_id=req_dict['params']['profileId'], agent=a) exists = True except AgentProfile.DoesNotExist: pass # Since document to be POSTed has to be json, so does the existing document if exists and str(p.content_type) != "application/json": raise ParamError( "Agent profile already exists but is not JSON, cannot update it with new JSON document" ) return req_dict