Ejemplo n.º 1
0
def public_key_handler():
    data = {
        "public_key":
        signing_key.verify_key.encode(
            encoder=nacl.encoding.Base64Encoder).decode("utf8")
    }

    response = app.response_class(response=json.dumps(data),
                                  mimetype='application/json')

    return response
Ejemplo n.º 2
0
def verification_sms_handler():
    try:
        body = request.get_json()
        public_key = signing_key.verify_key.encode(
            encoder=nacl.encoding.HexEncoder)

        verify_key = nacl.signing.VerifyKey(public_key,
                                            encoder=nacl.encoding.HexEncoder)

        spi_decoded = base64.b64decode(body.get("signedPhoneIdentifier"))
        spi = verify_key.verify(spi_decoded)

        logger.debug("Successfully verified signature: %s",
                     spi.decode('utf-8'))

        response = app.response_class(response=spi,
                                      mimetype='application/json')

        return response
    except Exception as exception:
        logger.debug("Invalid or corrupted signature for sid: %s",
                     body.get("signedPhoneIdentifier"))
        return Response("Invalid or corrupted signature", status=500)
Ejemplo n.º 3
0
def get_signed_phone_identifier_handler(userid):
    userid = userid.lower()
    user = db.getPhoneUserByName(conn, userid)

    if user is None:
        logger.debug("User was not found.")
        return Response("User was not found.", status=404)

    signed_data_verification_response = verify_signed_data(
        user[0], request.headers.get('Jimber-Authorization'), user[4],
        "get-signedphoneidentifier")

    if isinstance(signed_data_verification_response, Response):
        logger.debug(
            "response of verification is of instance Response, failed to verify."
        )
        return signed_data_verification_response

    if len(user) >= 5 and not user[5]:
        logger.debug("We found an old account: %s", user[0])

        if user[3] == 1:
            logger.debug("Old account was verified, creating signature.")

            data = bytes('{ "phone": "' + user[1] + '", "identifier": "' +
                         user[0] + '" }',
                         encoding='utf8')
            signed_phone_identifier = signing_key.sign(
                data, encoder=nacl.encoding.Base64Encoder)

            spi = {
                "signed_phone_identifier":
                signed_phone_identifier.decode("utf-8")
            }

            response = app.response_class(response=json.dumps(spi),
                                          mimetype='application/json')

            logger.debug("SPI: %s", spi)
            return response

        else:
            logger.debug(
                "Old account was not verified. User needs to resend the sms.")
            return Response("something went wrong", status=404)

    if user[5]:
        logger.debug("We found an account: %s", user[0])
        logger.debug("Retrieved signed_phone_identifier for %s", userid)

        db.delete_phone_user(conn, user[0], user[1])

        data = {"signed_phone_identifier": user[5]}

        logger.debug("data: %s", data)

        response = app.response_class(response=json.dumps(data),
                                      mimetype='application/json')

        return response
    else:
        return Response("User not found in database.", status=404)