Skip to content

Commit 37e2328

Browse files
committed
split auth code on its own file
1 parent 5b3ea91 commit 37e2328

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

homu/auth.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
def verify_level(username, repo_cfg, state, toml_keys):
2+
authorized = False
3+
if repo_cfg.get('auth_collaborators', False):
4+
authorized = state.get_repo().is_collaborator(username)
5+
if not authorized:
6+
authorized = username.lower() == state.delegate.lower()
7+
for toml_key in toml_keys:
8+
if not authorized:
9+
authorized = username in repo_cfg.get(toml_key, [])
10+
return authorized
11+
12+
13+
def verify(username, repo_cfg, state, auth, realtime, my_username):
14+
# The import is inside the function to prevent circular imports: main.py
15+
# requires auth.py and auth.py requires main.py
16+
from .main import AuthState
17+
18+
# In some cases (e.g. non-fully-qualified r+) we recursively talk to
19+
# ourself via a hidden markdown comment in the message. This is so that
20+
# when re-synchronizing after shutdown we can parse these comments and
21+
# still know the SHA for the approval.
22+
#
23+
# So comments from self should always be allowed
24+
if username == my_username:
25+
return True
26+
27+
authorized = False
28+
if auth == AuthState.REVIEWER:
29+
authorized = verify_level(username, repo_cfg, state, ['reviewers'])
30+
elif auth == AuthState.TRY:
31+
authorized = verify_level(
32+
username, repo_cfg, state, ['reviewers', 'try_users'],
33+
)
34+
35+
if authorized:
36+
return True
37+
else:
38+
if realtime:
39+
reply = '@{}: :key: Insufficient privileges: '.format(username)
40+
if auth == AuthState.REVIEWER:
41+
if repo_cfg.get('auth_collaborators', False):
42+
reply += 'Collaborator required'
43+
else:
44+
reply += 'Not in reviewers'
45+
elif auth == AuthState.TRY:
46+
reply += 'not in try users'
47+
state.add_comment(reply)
48+
return False

homu/main.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import functools
77
from . import comments
88
from . import utils
9+
from .auth import verify as verify_auth
910
from .utils import lazy_debug
1011
import logging
1112
from threading import Thread, Lock, Timer
@@ -408,46 +409,6 @@ class LabelEvent(Enum):
408409
PUSHED = 'pushed'
409410

410411

411-
def verify_auth(username, repo_cfg, state, auth, realtime, my_username):
412-
# In some cases (e.g. non-fully-qualified r+) we recursively talk to
413-
# ourself via a hidden markdown comment in the message. This is so that
414-
# when re-synchronizing after shutdown we can parse these comments and
415-
# still know the SHA for the approval.
416-
#
417-
# So comments from self should always be allowed
418-
if username == my_username:
419-
return True
420-
is_reviewer = False
421-
auth_collaborators = repo_cfg.get('auth_collaborators', False)
422-
if auth_collaborators:
423-
is_reviewer = state.get_repo().is_collaborator(username)
424-
if not is_reviewer:
425-
is_reviewer = username in repo_cfg.get('reviewers', [])
426-
if not is_reviewer:
427-
is_reviewer = username.lower() == state.delegate.lower()
428-
429-
if is_reviewer:
430-
have_auth = AuthState.REVIEWER
431-
elif username in repo_cfg.get('try_users', []):
432-
have_auth = AuthState.TRY
433-
else:
434-
have_auth = AuthState.NONE
435-
if have_auth >= auth:
436-
return True
437-
else:
438-
if realtime:
439-
reply = '@{}: :key: Insufficient privileges: '.format(username)
440-
if auth == AuthState.REVIEWER:
441-
if auth_collaborators:
442-
reply += 'Collaborator required'
443-
else:
444-
reply += 'Not in reviewers'
445-
elif auth == AuthState.TRY:
446-
reply += 'not in try users'
447-
state.add_comment(reply)
448-
return False
449-
450-
451412
PORTAL_TURRET_DIALOG = ["Target acquired", "Activated", "There you are"]
452413
PORTAL_TURRET_IMAGE = "https://cloud.githubusercontent.com/assets/1617736/22222924/c07b2a1c-e16d-11e6-91b3-ac659550585c.png" # noqa
453414

0 commit comments

Comments
 (0)