forked from tecladocode/python-refresher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
96 lines (57 loc) · 2.39 KB
/
code.py
File metadata and controls
96 lines (57 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
user = {"username": "jose", "access_level": "guest"}
def get_admin_password():
return "1234"
print(get_admin_password()) # Can do this even though I'm a "guest"
# Now this only runs if I'm an admin... but
if user["access_level"] == "admin":
print(get_admin_password())
print(get_admin_password()) # The function itself is still unsecured
# -- "secure" function --
def secure_get_admin():
if user["access_level"] == "admin":
print(get_admin_password())
# Now secure_get_admin() is secure.
# But get_admin_password() is still around, and I could call it:
secure_get_admin()
print(get_admin_password())
# We want to get rid of get_admin_password so that only the secure function remains!
# Maybe something like this?
def secure_function(func):
if user["access_level"] == "admin":
return func
user = {"username": "bob", "access_level": "admin"}
get_admin_password = secure_function(get_admin_password)
print(get_admin_password()) # Error!
# When we ran `secure_function`, we checked the user's access level. Because at that point the user was not an admin, the function did not `return func`. Therefore `get_admin_password` is set to `None`.
# We want to delay overwriting until we run the function
def get_admin_password():
return "1234"
def make_secure(func):
def secure_function():
if user["access_level"] == "admin":
return func()
return secure_function
get_admin_password = make_secure(
get_admin_password
) # `get_admin_password` is now `secure_func` from above
user = {"username": "jose", "access_level": "guest"}
print(get_admin_password()) # Now we check access level
user = {"username": "bob", "access_level": "admin"}
print(get_admin_password()) # Now we check access level
# -- More information or error handling --
def get_admin_password():
return "1234"
def make_secure(func):
def secure_function():
if user["access_level"] == "admin":
return func()
else:
return f"No admin permissions for {user['username']}."
return secure_function
get_admin_password = make_secure(
get_admin_password
) # `get_admin_password` is now `secure_func` from above
user = {"username": "jose", "access_level": "guest"}
print(get_admin_password()) # Now we check access level
user = {"username": "bob", "access_level": "admin"}
print(get_admin_password()) # Now we check access level