Esempio n. 1
0
def test_quickstart_policy_3():
    oso = Oso()
    oso.register_class(Expense)
    oso.load_file("../polar/expenses-03-py.polar")
    expense = EXPENSES[1]
    assert oso.is_allowed("*****@*****.**", "GET", expense)
    assert not oso.is_allowed("*****@*****.**", "GET", expense)
Esempio n. 2
0
def test_quickstart_policy_4():
    oso = Oso()
    oso.register_class(Expense)
    oso.load_file("../polar/expenses-04.polar")
    assert oso.is_allowed("*****@*****.**", "GET", EXPENSES[1])
    assert not oso.is_allowed("*****@*****.**", "GET", EXPENSES[3])
    assert not oso.is_allowed("*****@*****.**", "GET", EXPENSES[1])
    assert oso.is_allowed("*****@*****.**", "GET", EXPENSES[3])
Esempio n. 3
0
def register_models(oso: Oso, base):
    """Register all models in model base class ``base`` with oso as classes."""
    # TODO (dhatch): Not sure this is legit b/c it uses an internal interface?
    for name, model in base._decl_class_registry.items():
        if name == "_sa_module_registry":
            continue

        oso.register_class(model)
Esempio n. 4
0
def test_quickstart_policy_2():
    oso = Oso()
    alice = "*****@*****.**"
    expense = EXPENSES[1]
    assert not oso.is_allowed(alice, "GET", expense)
    oso.register_class(Expense)
    oso.load_file("../polar/expenses-02.polar")
    assert oso.is_allowed(alice, "GET", expense)
    assert not oso.is_allowed("*****@*****.**", "GET", expense)
Esempio n. 5
0
def rmdir(path):
    import shutil
    import getpass
    from oso import Oso
    oso = Oso()
    oso.register_class(PathAttributes)
    oso.load_files(["rmdir.polar"])
    path_attributes = get_path_attributes(path)
    user_id = getpass.getuser()
    if oso.is_allowed(user_id, "can_remove", path_attributes):
        shutil.rmtree(path)
    else:
        raise PermissionError(f"You cannot delete {path}")
Esempio n. 6
0
def test_oso():
    oso = Oso()
    oso.register_class(Jwt)
    oso.register_class(Actor)
    oso.register_class(Widget)
    oso.register_class(Company)
    oso.load_file(Path(__file__).parent / "test_oso.polar")

    return oso
Esempio n. 7
0
def init_oso(app):
    from .expense import Expense
    from .organization import Organization
    from .user import Actor, Guest, User

    oso = Oso()
    oso.register_class(Actor)
    oso.register_class(Guest)
    oso.register_class(User)
    oso.register_class(Expense)
    oso.register_class(Organization)
    oso.register_class(Request)

    for policy in app.config.get("OSO_POLICIES", []):
        oso.load_file(policy)

    app.oso = oso
Esempio n. 8
0
def test_oso():
    oso = Oso()
    oso.register_class(Actor, name="test_oso::Actor")
    oso.register_class(Widget, name="test_oso::Widget")
    oso.register_class(Company, name="test_oso::Company")
    oso.load_file(Path(__file__).parent / "test_oso.polar")

    return oso
Esempio n. 9
0
from polar.exceptions import UnrecognizedEOF
from oso import Oso, OsoException, Variable

oso = Oso()

# Application class with default kwargs constructor, registered with the
# decorator.
class A:
    def __init__(self, x):
        self.x = x

    def foo(self):
        return -1


oso.register_class(A)


# Test inheritance; doesn't need to be registered.
class D(A):
    pass


# Namespaced application class (to be aliased) with custom
# constructor.
class B:
    class C:
        def __init__(self, y):
            self.y = y

        def foo(self):
Esempio n. 10
0
    def is_authenticated(self):
        return self.id is not None

    def is_active(self):
        return self.id is not None

    def is_anonymous(self):
        return self.id is None

    def get_id(self):
        return self.id


base_oso = Oso()
base_oso.register_class(User)
base_oso.load_file("policies.polar")


@login_manager.user_loader
def load_user(user_id):
    return User.get(user_id)


@app.route("/login", methods=["POST"])
def login():
    username = request.json.get("username")
    # no password check
    user = User(username)
    login_user(user, remember=True)
    return jsonify(msg="login was a success!")
Esempio n. 11
0
from flask import Flask
from oso import Oso, NotFoundError
from .models import User, Repository

# Initialize the Oso object. This object is usually used globally throughout
# an application.
oso = Oso()

# Tell Oso about the data you will authorize. These types can be referenced
# in the policy.
oso.register_class(User)
oso.register_class(Repository)

# Load your policy files.
oso.load_files(["app/main.polar"])

app = Flask(__name__)


@app.route("/repo/<name>")
def repo_show(name):
    repo = Repository.get_by_name(name)

    try:
        oso.authorize(User.get_current_user(), "read", repo)
        return f"<h1>A Repo</h1><p>Welcome to repo {repo.name}</p>", 200
    except NotFoundError:
        return f"<h1>Whoops!</h1><p>Repo named {name} was not found</p>", 404
Esempio n. 12
0
File: test.py Progetto: zmilan/oso
from oso import Oso, OsoError, Variable

oso = Oso()


# Application class with default kwargs constructor, registered with the
# decorator.
class A:
    def __init__(self, x):
        self.x = x

    def foo(self):
        return -1


oso.register_class(A)


# Test inheritance; doesn't need to be registered.
class D(A):
    pass


# Namespaced application class (to be aliased) with custom
# constructor.
class B:
    class C:
        def __init__(self, y):
            self.y = y

        def foo(self):
Esempio n. 13
0
#PATTERN
class Pattern(enum.Enum):
    HighCard = 1
    Pair = 2
    TwoPair = 3
    Trio = 4
    Straight = 5
    Flush = 6
    FullHouse = 7
    Poker = 8
    StraightFlush = 9


#------------------CONFIG & REGISTER CLASSES----------------------
oso = Oso()
oso.register_class(Card)
oso.register_class(Cards)
oso.register_class(Pattern)
oso.load_file("poker.polar")


def setHand(cards):  #Establish the pattern and the top 5 cards

    #fiveCards = []
    list_of_all_cards = cards
    list_of_all_faces = toNumbers(cards)
    list_of_all_suits = toSuits(cards)
    duplicate_list_of_all_faces = toNumbers(cards)
    top_cards = []
    pattern = 0  #: Pattern
    check_for_flush = ""
Esempio n. 14
0
def main():
    # parser = argparse.ArgumentParser(description="An epic Polar adventure.")
    # parser.add_argument(
    #     "-l", "--load", type=str, nargs=1, help="the filename of a saved game"
    # )
    # args = parser.parse_args()
    # if args.load:
    #     GAME.load_saved(args.load)

    oso = Oso()
    oso.register_class(Game)
    oso.register_class(Room)
    oso.register_class(Passage)
    oso.register_class(Player)
    oso.register_class(Collection)
    oso.register_class(Object)
    oso.register_class(Animal)
    oso.register_class(Food)
    oso.register_class(Container)
    oso.register_class(Takeable)
    oso.register_class(Mushroomy)
    oso.register_class(Soup)
    oso.register_class(Source)
    oso.register_class(Wand)
    oso.register_class(Wet)
    oso.register_class(OnFire)
    oso.register_class(Leafy)
    oso.register_constant(GAME, "GAME")
    oso.register_constant(PLAYER, "PLAYER")
    oso.register_constant(ROOMS, "Rooms")
    oso.register_constant(PASSAGES, "Passages")
    oso.register_constant(OBJECTS, "Objects")
    oso.load_file("world.polar")
    oso.load_file("commands.polar")
    oso.load_file("tests.polar")
    oso.repl()