Skip to content

Commit 539ef0d

Browse files
committed
Add ruff formatting
1 parent cae7c85 commit 539ef0d

20 files changed

Lines changed: 989 additions & 743 deletions

.github/workflows/lint.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ jobs:
1818
with:
1919
flake8_version: 6.0.0
2020
plugins: flake8-isort==6.0.0
21+
22+
ruff-format:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
- uses: astral-sh/ruff-action@v3
28+
with:
29+
version: "~=0.13.3"
30+
args: format --check --diff --output-format=github

extra/policy_gen_sandbox.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ def handle_text():
4646
tk.Label(frm_content, text="Bucket map YAML").grid(row=0, column=0)
4747

4848
txt_bucketmap = tk.Text(frm_content)
49-
txt_bucketmap.bind(
50-
"<Key>",
51-
lambda _: window.after(1, handle_text)
52-
)
49+
txt_bucketmap.bind("<Key>", lambda _: window.after(1, handle_text))
5350
txt_bucketmap.grid(row=1, column=0, sticky="nsew")
5451

5552
# Policy panel
@@ -65,10 +62,7 @@ def handle_text():
6562
tk.Label(frm_groups, text="User Groups: ").grid(row=0, column=0)
6663
var_group = tk.StringVar(value="null")
6764
entry_groups = tk.Entry(frm_groups, textvariable=var_group)
68-
entry_groups.bind(
69-
"<Key>",
70-
lambda _: window.after(1, handle_text)
71-
)
65+
entry_groups.bind("<Key>", lambda _: window.after(1, handle_text))
7266
entry_groups.grid(row=0, column=1)
7367

7468
# Minified size indicator

src/rain_api_core/auth.py

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ class UserProfile:
2626
@classmethod
2727
def from_jwt_payload(cls, payload):
2828
return cls(
29-
user_id=payload.get('urs-user-id'),
30-
token=payload.get('urs-access-token'),
31-
groups=payload.get('urs-groups'),
32-
first_name=payload.get('first_name'),
33-
last_name=payload.get('last_name'),
34-
email=payload.get('email'),
35-
iat=payload.get('iat'),
36-
exp=payload.get('exp')
29+
user_id=payload.get("urs-user-id"),
30+
token=payload.get("urs-access-token"),
31+
groups=payload.get("urs-groups"),
32+
first_name=payload.get("first_name"),
33+
last_name=payload.get("last_name"),
34+
email=payload.get("email"),
35+
iat=payload.get("iat"),
36+
exp=payload.get("exp"),
3737
)
3838

3939
def to_jwt_payload(self):
4040
return {
41-
'urs-user-id': self.user_id,
42-
'urs-access-token': self.token,
43-
'urs-groups': self.groups,
44-
'first_name': self.first_name,
45-
'last_name': self.last_name,
46-
'email': self.email,
47-
'iat': self.iat,
48-
'exp': self.exp,
41+
"urs-user-id": self.user_id,
42+
"urs-access-token": self.token,
43+
"urs-groups": self.groups,
44+
"first_name": self.first_name,
45+
"last_name": self.last_name,
46+
"email": self.email,
47+
"iat": self.iat,
48+
"exp": self.exp,
4949
}
5050

5151

@@ -57,7 +57,7 @@ def __init__(
5757
private_key: str,
5858
cookie_name: str,
5959
blacklist={},
60-
session_ttl_in_hours: float = 7 * 24
60+
session_ttl_in_hours: float = 7 * 24,
6161
):
6262
self.algorithm = algorithm
6363
self.public_key = public_key
@@ -67,7 +67,9 @@ def __init__(
6767
self.black_list = blacklist
6868

6969
def _get_auth_cookie(self, headers: Mapping[str, str]):
70-
cookie_string = headers.get('cookie') or headers.get('Cookie') or headers.get('COOKIE')
70+
cookie_string = (
71+
headers.get("cookie") or headers.get("Cookie") or headers.get("COOKIE")
72+
)
7173
if not cookie_string:
7274
return {}
7375

@@ -80,32 +82,32 @@ def _decode_jwt(self, token: str):
8082
try:
8183
return jwt.decode(token.encode(), self.public_key, [self.algorithm])
8284
except jwt.ExpiredSignatureError:
83-
log.info('JWT has expired')
85+
log.info("JWT has expired")
8486
except jwt.InvalidSignatureError:
85-
log.info('JWT has failed verification')
87+
log.info("JWT has failed verification")
8688
return None
8789

8890
def _encode_jwt(self, payload: Mapping[str, str]) -> str:
8991
try:
9092
encoded = jwt.encode(payload, self.private_key, self.algorithm)
9193
except TypeError:
92-
log.error('unable to encode jwt cookie')
93-
return ''
94+
log.error("unable to encode jwt cookie")
95+
return ""
9496
return encoded
9597

9698
def _jwt_payload_from_user_profile(self, user_profile: Optional[UserProfile]):
9799
if user_profile is None:
98100
return {}
99101
now = int(time())
100102
return {
101-
'urs-user-id': user_profile.user_id,
102-
'first_name': user_profile.first_name,
103-
'last_name': user_profile.last_name,
104-
'email': user_profile.email,
105-
'urs-access-token': user_profile.token,
106-
'urs-groups': user_profile.groups,
107-
'iat': now,
108-
'exp': now + self.session_ttl
103+
"urs-user-id": user_profile.user_id,
104+
"first_name": user_profile.first_name,
105+
"last_name": user_profile.last_name,
106+
"email": user_profile.email,
107+
"urs-access-token": user_profile.token,
108+
"urs-groups": user_profile.groups,
109+
"iat": now,
110+
"exp": now + self.session_ttl,
109111
}
110112

111113
def _in_blacklist(self, user_profile: UserProfile):
@@ -117,7 +119,8 @@ def _in_blacklist(self, user_profile: UserProfile):
117119
return False
118120

119121
def get_profile_from_headers(
120-
self, headers: Mapping[str, str],
122+
self,
123+
headers: Mapping[str, str],
121124
) -> Optional[UserProfile]:
122125
"""Inspects headers for auth cookie and return user_profile if authenticated, None otherwise"""
123126
auth_cookie = self._get_auth_cookie(headers)
@@ -133,22 +136,26 @@ def get_profile_from_headers(
133136
return None
134137
return user_profile
135138

136-
def get_header_to_set_auth_cookie(self, user_profile: Optional[UserProfile], cookie_domain: str = ''):
137-
""" Gets a header to set auth-cookie
139+
def get_header_to_set_auth_cookie(
140+
self,
141+
user_profile: Optional[UserProfile],
142+
cookie_domain: str = "",
143+
):
144+
"""Gets a header to set auth-cookie
138145
139146
Parameters:
140147
UserProfile: UserProfile to use in construction of a cookie, if none will return header to unset/logout
141148
"""
142149
payload = self._jwt_payload_from_user_profile(user_profile)
143-
cookie_value = self._encode_jwt(payload) if payload else 'expired'
144-
cookie_domain = f'; Domain={cookie_domain}' if cookie_domain else ''
150+
cookie_value = self._encode_jwt(payload) if payload else "expired"
151+
cookie_domain = f"; Domain={cookie_domain}" if cookie_domain else ""
145152
if payload:
146-
expire_date = format_7231_date(payload['exp'])
153+
expire_date = format_7231_date(payload["exp"])
147154
else:
148-
expire_date = 'Thu, 01 Jan 1970 00:00:00 GMT'
155+
expire_date = "Thu, 01 Jan 1970 00:00:00 GMT"
149156
return {
150-
'SET-COOKIE': (
151-
f'{self.cookie_name}={cookie_value}; Expires={expire_date}; Path=/{cookie_domain}; Secure; '
152-
'HttpOnly; SameSite=Lax'
157+
"SET-COOKIE": (
158+
f"{self.cookie_name}={cookie_value}; Expires={expire_date}; Path=/{cookie_domain}; Secure; "
159+
"HttpOnly; SameSite=Lax"
153160
)
154161
}

0 commit comments

Comments
 (0)