Exemplo n.º 1
0
def create_key_details(migrator):
    """ Modifies the Key table to add several columns:
          short_name text indexed not null
          visibility text not null
    """

    short_name = peewee.CharField(default=get_simple_name(),
                                  index=True,
                                  unique=False,
                                  null=False)

    visibility = peewee.CharField(default="self",
                                  null=False,
                                  choices=[(
                                      'self',
                                      'self',
                                  ), (
                                      'private',
                                      'private',
                                  ), (
                                      'public',
                                      'public',
                                  )])

    migrate(migrator.add_column('key', 'short_name', short_name),
            migrator.add_column('key', 'visibility', visibility))
Exemplo n.º 2
0
def create_key_details(migrator):
    """ Modifies the Key table to add several columns:
          short_name text indexed not null
          visibility text not null
    """

    short_name = peewee.CharField(default=get_simple_name(), index=True, unique=False, null=False)

    visibility = peewee.CharField(
        default="self", null=False, choices=[("self", "self"), ("private", "private"), ("public", "public")]
    )

    migrate(migrator.add_column("key", "short_name", short_name), migrator.add_column("key", "visibility", visibility))
Exemplo n.º 3
0
    def key_put():
        """ PUT /key

            Inserts a key into the database. The PUT request should look
            something like this:

              {
                "content": "ssh-rsa ...",
                "short_name": "...",
                "visibility": "public|private|self"
              }
        """

        token = token_by_header_data(request.headers.get("X-Keydom-Session"))

        if not token:
            resp = routing.base.generate_error_response(code=401)
            resp["message"] = "Invalid authentication token."
            return json.dumps(resp) + "\n"

        if token.has_expired:
            resp = routing.base.generate_error_response(code=403)
            resp[
                "message"] = "Authentication token has expired. Request another."
            return json.dumps(resp) + "\n"

        user = token.for_user
        key_data = {
            "content": request.forms.get("content") or None,
            "visibility": request.forms.get("visibility") or None,
            "short_name": request.forms.get("short_name") or None,
        }

        if not key_data["content"]:
            resp = routing.base.generate_error_response(code=400)
            resp["message"] = "Missing PUT request data: 'content'"
            return json.dumps(resp) + "\n"

        if not key_data["short_name"]:
            key_data["short_name"] = get_simple_name()

        if not key_data["visibility"]:
            key_data["visibility"] = "self"

        res = (Key.select().where(Key.content == key_data["content"]
                                  & Key.short_name == key_data["short_name"]
                                  & Key.belongs_to == user))

        if res.count() > 0:
            resp = routing.base.generate_error_response(code=409)
            resp["message"] = "Key already exists for this user."
            return json.dumps(resp) + "\n"

        new_key = Key.create(belongs_to=user, **key_data)
        new_key.save()

        try:
            new_key.fingerprint()
        except TypeError:
            resp = routing.base.generate_error_response(code=409)
            resp["message"] = "Invalid key content."
            return json.dumps(resp) + "\n"

        resp = routing.base.generate_bare_response()
        resp["key"] = {
            "short_name": new_key.short_name,
            "fingerprint": new_key.fingerprint(),
            "visibility": new_key.visibility,
        }

        return json.dumps(resp) + "\n"
Exemplo n.º 4
0
    def key_put():
        """ PUT /key

            Inserts a key into the database. The PUT request should look
            something like this:

              {
                "content": "ssh-rsa ...",
                "short_name": "...",
                "visibility": "public|private|self"
              }
        """

        token = token_by_header_data(request.headers.get("X-Keydom-Session"))

        if not token:
            resp = routing.base.generate_error_response(code=401)
            resp["message"] = "Invalid authentication token."
            return json.dumps(resp) + "\n"

        if token.has_expired:
            resp = routing.base.generate_error_response(code=403)
            resp["message"] = "Authentication token has expired. Request another."
            return json.dumps(resp) + "\n"

        user = token.for_user
        key_data = {
            "content": request.forms.get("content") or None,
            "visibility": request.forms.get("visibility") or None,
            "short_name": request.forms.get("short_name") or None,
        }

        if not key_data["content"]:
            resp = routing.base.generate_error_response(code=400)
            resp["message"] = "Missing PUT request data: 'content'"
            return json.dumps(resp) + "\n"

        if not key_data["short_name"]:
            key_data["short_name"] = get_simple_name()

        if not key_data["visibility"]:
            key_data["visibility"] = "self"

        res = (Key
               .select()
               .where(Key.content == key_data["content"] &
                      Key.short_name == key_data["short_name"] &
                      Key.belongs_to == user))

        if res.count() > 0:
            resp = routing.base.generate_error_response(code=409)
            resp["message"] = "Key already exists for this user."
            return json.dumps(resp) + "\n"

        new_key = Key.create(
            belongs_to=user,
            **key_data)
        new_key.save()

        try:
            new_key.fingerprint()
        except TypeError:
            resp = routing.base.generate_error_response(code=409)
            resp["message"] = "Invalid key content."
            return json.dumps(resp) + "\n"

        resp = routing.base.generate_bare_response()
        resp["key"] = {
            "short_name": new_key.short_name,
            "fingerprint": new_key.fingerprint(),
            "visibility": new_key.visibility,
        }

        return json.dumps(resp) + "\n"