Skip to content

Commit aede122

Browse files
committed
Add ruff formatting
1 parent 8c7011a commit aede122

20 files changed

Lines changed: 891 additions & 931 deletions

.github/workflows/lint.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ jobs:
1515
with:
1616
flake8_version: 6.0.0
1717
plugins: flake8-isort==6.0.0
18+
19+
ruff-format:
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
- uses: astral-sh/ruff-action@v3
25+
with:
26+
version: "~=0.13.3"
27+
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: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ class UserProfile:
2525
@classmethod
2626
def from_jwt_payload(cls, payload):
2727
return cls(
28-
user_id=payload.get('urs-user-id'),
29-
token=payload.get('urs-access-token'),
30-
groups=payload.get('urs-groups'),
31-
first_name=payload.get('first_name'),
32-
last_name=payload.get('last_name'),
33-
email=payload.get('email'),
34-
iat=payload.get('iat'),
35-
exp=payload.get('exp')
28+
user_id=payload.get("urs-user-id"),
29+
token=payload.get("urs-access-token"),
30+
groups=payload.get("urs-groups"),
31+
first_name=payload.get("first_name"),
32+
last_name=payload.get("last_name"),
33+
email=payload.get("email"),
34+
iat=payload.get("iat"),
35+
exp=payload.get("exp"),
3636
)
3737

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

5050

@@ -56,7 +56,7 @@ def __init__(
5656
private_key: str,
5757
cookie_name: str,
5858
blacklist={},
59-
session_ttl_in_hours: float = 7 * 24
59+
session_ttl_in_hours: float = 7 * 24,
6060
):
6161
self.algorithm = algorithm
6262
self.public_key = public_key
@@ -66,7 +66,9 @@ def __init__(
6666
self.black_list = blacklist
6767

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

@@ -79,32 +81,32 @@ def _decode_jwt(self, token: str):
7981
try:
8082
return jwt.decode(token.encode(), self.public_key, [self.algorithm])
8183
except jwt.ExpiredSignatureError:
82-
log.info('JWT has expired')
84+
log.info("JWT has expired")
8385
except jwt.InvalidSignatureError:
84-
log.info('JWT has failed verification')
86+
log.info("JWT has failed verification")
8587
return None
8688

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

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

110112
def _in_blacklist(self, user_profile: UserProfile):
@@ -130,22 +132,24 @@ def get_profile_from_headers(self, headers) -> Optional[UserProfile]:
130132
return None
131133
return user_profile
132134

133-
def get_header_to_set_auth_cookie(self, user_profile: Optional[UserProfile], cookie_domain=''):
134-
""" Gets a header to set auth-cookie
135+
def get_header_to_set_auth_cookie(
136+
self, user_profile: Optional[UserProfile], cookie_domain=""
137+
):
138+
"""Gets a header to set auth-cookie
135139
136140
Parameters:
137141
UserProfile: UserProfile to use in construction of a cookie, if none will return header to unset/logout
138142
"""
139143
payload = self._jwt_payload_from_user_profile(user_profile)
140-
cookie_value = self._encode_jwt(payload) if payload else 'expired'
141-
cookie_domain = f'; Domain={cookie_domain}' if cookie_domain else ''
144+
cookie_value = self._encode_jwt(payload) if payload else "expired"
145+
cookie_domain = f"; Domain={cookie_domain}" if cookie_domain else ""
142146
if payload:
143-
expire_date = format_7231_date(payload['exp'])
147+
expire_date = format_7231_date(payload["exp"])
144148
else:
145-
expire_date = 'Thu, 01 Jan 1970 00:00:00 GMT'
149+
expire_date = "Thu, 01 Jan 1970 00:00:00 GMT"
146150
return {
147-
'SET-COOKIE': (
148-
f'{self.cookie_name}={cookie_value}; Expires={expire_date}; Path=/{cookie_domain}; Secure; '
149-
'HttpOnly; SameSite=Lax'
151+
"SET-COOKIE": (
152+
f"{self.cookie_name}={cookie_value}; Expires={expire_date}; Path=/{cookie_domain}; Secure; "
153+
"HttpOnly; SameSite=Lax"
150154
)
151155
}

0 commit comments

Comments
 (0)