예제 #1
0
 def delete(self, id):
     data = secrets.get_all("api-keys")
     for x in data:
         if x["key"] == id:
             data.remove(x)
             secrets.save()
             break
     return Response(status=204)
예제 #2
0
파일: auth.py 프로젝트: wrestrtdr/kraken
def verify(token=None):
    """
    Verify a provided JSON Web Token (JWT) for authentication.

    :param str token: JSON Web Token (JWT)
    :returns: True if valid, False if not
    """
    if config.get("genesis", "anonymous"):
        return

    if request.headers.get("X-API-Key", None):
        api_key = request.headers.get("X-API-Key")
        data = secrets.get_all("api-keys")
        for x in data:
            if x["key"] == api_key:
                user = users.get(name=x["user"])
                if not user or not user.admin:
                    resp = jsonify(message="Authorization required")
                    resp.status_code = 401
                    return resp
                else:
                    return

    if not token:
        token = request.headers.get("Authorization", None)
        if not token:
            resp = jsonify(message="Authorization required")
            resp.status_code = 401
            return resp

        token = token.split()
        if token[0] != "Bearer" or len(token) > 2:
            resp = jsonify(message="Malformed token")
            resp.status_code = 400
            return resp
        token = token[1]

    try:
        tjwss = TimedJSONWebSignatureSerializer(
            secret_key=current_app.config["SECRET_KEY"],
            expires_in=3600,
            algorithm_name="HS256")
        payload = tjwss.loads(token)
    except SignatureExpired:
        resp = jsonify(message="Token expired")
        resp.status_code = 401
        return resp
    except BadSignature:
        resp = jsonify(message="Malformed token signature")
        resp.status_code = 401
        return resp
    user = users.get(name=payload["uid"])
    if not user or not user.admin:
        resp = jsonify(message="Authorization required")
        resp.status_code = 401
        return resp
예제 #3
0
파일: apikeys.py 프로젝트: ns408/core
def revoke(key):
    """Revoke an API key."""
    try:
        data = secrets.get_all("api-keys")
        for x in data:
            if x["key"] == key:
                data.remove(x)
                secrets.save()
                break
        logger.info('ctl:keys:revoke', 'API key revoked')
    except Exception as e:
        raise CLIException(str(e))
예제 #4
0
파일: apikeys.py 프로젝트: ns408/core
def list_keys():
    """List all API keys."""
    try:
        keys = secrets.get_all("api-keys")
        if not keys:
            logger.info('ctl:keys:list', 'No keys found')
            return
        llen = len(sorted(keys, key=lambda x: len(x["user"]))[-1].name)
        for x in keys:
            click.echo(
                click.style(
                    '{name: <45}'.format(
                        name=x["key"]), fg="white", bold=True) +
                click.style('{name: <{fill}}'.format(name=x["user"],
                                                     fill=llen + 3),
                            fg="green") + "   " +
                click.style(x["comment"], fg="yellow"))
    except Exception as e:
        raise CLIException(str(e))
예제 #5
0
 def get(self):
     keys = secrets.get_all("api-keys")
     return jsonify(api_keys=keys)