Example #1
0
def get_uuid(uuid):
    device = DEVICES.get_device_by_uuid(uuid)
    if device is not None:
        if request.method == 'PATCH':
            result = request.json
            DEVICES.add_device(uuid=uuid,
                               name=result.get("name"),
                               expires_at=result.get("expires_at"),
                               accessToken=result.get("accessToken"),
                               refreshToken=result.get("refreshToken"))

        result = model_to_dict(device)
    else:
        result = {}
    return nice_json(result)
Example #2
0
def activate():
    uuid = request.json["state"]

    # paired?
    device = DEVICES.get_device_by_uuid(uuid)
    if device is None or not device.paired:
        return Response(
            'Could not verify your access level for that URL.\n'
            'You have to authenticate with proper credentials', 401,
            {'WWW-Authenticate': 'Basic realm="NOT PAIRED"'})

    # generate access tokens
    DEVICES.add_device(uuid=uuid,
                       expires_at=time.time() + 72000,
                       accessToken=gen_api(),
                       refreshToken=gen_api())
    result = model_to_dict(device)
    return nice_json(result)
Example #3
0
def pair(code, name="unknown_device"):
    device = DEVICES.get_unpaired_by_code(code)
    if device:
        user = get_user()
        if DEVICES.add_device(uuid=device.uuid, name=name, mail=user.mail):
            DEVICES.remove_unpaired(device.uuid)
            msg = Message("Device was paired", recipients=[user.mail])
            mail.send(msg)
            return True
    return False
Example #4
0
def pair(code, uuid, name, mail):
    # pair
    result = {"paired": False}
    device = DEVICES.get_unpaired_by_uuid(uuid)
    if device and device.code == code:
        user = DEVICES.get_user_by_mail(mail)
        # create user if it doesnt exist
        if not user:
            DEVICES.add_user(mail, name, "666")
        if DEVICES.add_device(uuid, mail=mail):
            DEVICES.remove_unpaired(uuid)
            result = {"paired": True}
    return nice_json(result)
Example #5
0
def token():
    api = request.headers.get('Authorization', '').replace("Bearer ", "")
    device = DEVICES.get_device_by_token(api)
    if not device:
        return Response(
            'Could not verify your access level for that URL.\n'
            'You have to authenticate with proper credentials', 401,
            {'WWW-Authenticate': 'Basic realm="NOT PAIRED"'})
    # token to refresh expired token
    if device.refreshToken is None or device.refreshToken != api:
        return Response(
            'Could not verify your access level for that URL.\n'
            'You have to authenticate with proper credentials', 401,
            {'WWW-Authenticate': 'Basic realm="BAD REFRESH CODE"'})
    # new tokens to access
    access_token = gen_api()
    new_refresh_token = gen_api()

    DEVICES.add_device(uuid=device.uuid,
                       expires_at=time.time() + 72000,
                       accessToken=access_token,
                       refreshToken=new_refresh_token)

    return nice_json(model_to_dict(device))