Example #1
0
 def mutate(root, args, context, info):
     if functions.email_in_use(args.get('email')):
         raise exceptions.GenericError('Email in Use')
     user_id = functions.create_account(args.get('email'),
                                        args.get('password'))
     session = functions.create_session(user_id)
     return CreateAccount(session=session)
Example #2
0
 def mutate(root, args, context, info):
     if functions.verify_password(args.get('email'),
                                  args.get('password')) is False:
         raise exceptions.GenericError('Invalid Email/Password Combination')
     user_id = functions.get_user_id(email=args.get('email'))
     session = functions.create_session(user_id)
     return CreateSession(session=session)
Example #3
0
def get_access_token_and_refresh_token(
	client_id: str,
	client_secret: str,
	callback_url: str,
	code: str,
) -> Tuple[str, str, int]:
	"""

	:param client_id: the client id from the registered pavlok application
	:param client_secret: the client secret from the registered pavlok application
	:param callback_url: The callback url used to create the auth code
	:param code: The auth. code you get from authorizing the pavlok app
	:return: A tuple with three values [access_token: str, refresh_token: str, seconds until access token expires)
	"""
	payload = {
		"client_id": client_id,
		"client_secret": client_secret,
		"code": code,
		"grant_type": "authorization_code",
		"redirect_uri": callback_url,
	}

	r = requests.post(BASE_API_ENDPOINT + '/oauth/token', json=payload)
	response_data = r.json()
	if r.status_code != 200:
		raise exceptions.GenericError(response_data)
	return response_data.get('access_token'), response_data.get('refresh_token'), int(response_data.get('expires_in'))
Example #4
0
def pavlok_get_access_token_from_key_id(key_id: str, ) -> str:
    db_session = db.DBSession()
    row = db_session.query(db.PavlokKeyV1).filter(
        db.PavlokKeyV1.key_id == key_id).first()  # type: db.PavlokKeyV1
    db_session.close()
    if row is None:
        raise exceptions.GenericError('Invalid Pavlok Key ID')
    return row.access_token
Example #5
0
def get_user_id(
    session_id: str = None,
    email: str = None,
) -> str:
    if session_id is not None:
        db_session = db.DBSession()
        row = db_session.query(db.SessionV1).filter(
            db.SessionV1.session_id == session_id).first()
        if row is None:
            raise exceptions.GenericError('Invalid Session ID')
        db_session.close()
        return row.user_id
    if email is not None:
        db_session = db.DBSession()
        row = db_session.query(db.UserV1).filter(
            db.UserV1.email == email).first()  # type: db.UserV1
        if row is None:
            raise exceptions.GenericError('Unused Email Supplied')
        db_session.close()
        return row.user_id
Example #6
0
def shock(
	access_token: str,
	value: int,
	reason: str=None,
) -> None:
	"""
	Sends a shock stimuli request to pavlok
	:param access_token: acess_token required to authenticate with pavlok's api
	:param value: Intensifier for the stimulus (1-255)
	:param reason: Reason to show in the Pavlok App under logs.
	:return:
	"""
	payload = {
		"access_token": access_token,
	}

	if reason is not None:
		payload['reason'] = reason

	r = requests.post(f"{BASE_API_ENDPOINT}/api/v1/stimuli/shock/{value}", json=payload)
	response_data = r.text
	if r.status_code != 200:
		raise exceptions.GenericError(response_data)
Example #7
0
def led(
	access_token: str,
	value: int,
	reason: str=None
) -> None:
	"""
	Sends a LED stimulus to the associated pavlok
	:param access_token:
	:param value: The value intensifier for the stimulus (1-4)
	:param reason:
	:return:
	"""

	payload = {
		"access_token": access_token,
	}

	if reason is not None:
		payload['reason'] = reason

	r = requests.post(f"{BASE_API_ENDPOINT}/api/v1/stimuli/led/{value}", json=payload)
	response_data = r.text
	if r.status_code != 200:
		raise exceptions.GenericError(response_data)