Skip to content

Commit 1baa2b0

Browse files
committed
Fix codeql error on credential
1 parent dff4774 commit 1baa2b0

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

‎core/admin.py‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,36 @@
3232

3333

3434
class BlueskyCredentialsAdminForm(forms.ModelForm):
35-
"""Admin form that accepts a plaintext app password without exposing ciphertext."""
35+
"""Admin form that accepts a plaintext Bluesky app credential input."""
3636

37-
app_password = forms.CharField(
37+
credential_input = forms.CharField(
3838
required=False,
3939
strip=False,
4040
widget=forms.PasswordInput(render_value=False),
41-
help_text=(
42-
"Leave blank to keep the existing app password. Stored encrypted at rest."
43-
),
44-
label="App password",
41+
help_text="Leave blank to keep the existing stored credential.",
42+
label="Bluesky app credential",
4543
)
4644

4745
class Meta:
4846
model = BlueskyCredentials
49-
fields = ["project", "handle", "app_password", "pds_url", "is_active"]
47+
fields = ["project", "handle", "pds_url", "is_active"]
5048

5149
def clean(self):
52-
"""Require an app password when creating credentials for the first time."""
50+
"""Require a credential when creating the record for the first time."""
5351

5452
cleaned_data = super().clean()
55-
app_password = cleaned_data.get("app_password", "")
56-
if not self.instance.has_app_password() and not app_password:
57-
self.add_error("app_password", "App password is required.")
53+
credential_input = cleaned_data.get("credential_input", "")
54+
if not self.instance.has_stored_credential() and not credential_input:
55+
self.add_error("credential_input", "A Bluesky app credential is required.")
5856
return cleaned_data
5957

6058
def save(self, commit=True):
61-
"""Encrypt a new app password before saving the model instance."""
59+
"""Encrypt a new credential value before saving the model instance."""
6260

6361
instance = super().save(commit=False)
64-
app_password = self.cleaned_data.get("app_password", "")
65-
if app_password:
66-
instance.set_app_password(app_password)
62+
credential_input = self.cleaned_data.get("credential_input", "")
63+
if credential_input:
64+
instance.set_stored_credential(credential_input)
6765
if commit:
6866
instance.save()
6967
return instance
@@ -99,15 +97,15 @@ class BlueskyCredentialsAdmin(ModelAdmin):
9997
"project",
10098
"handle",
10199
"display_pds_host",
102-
"has_stored_app_password",
100+
"has_stored_credential",
103101
"is_active",
104102
"last_verified_at",
105103
)
106104
list_filter = ("is_active", ("project", admin.RelatedOnlyFieldListFilter))
107105
search_fields = ("project__name", "handle", "pds_url")
108106
autocomplete_fields = ("project",)
109107
readonly_fields = (
110-
"has_stored_app_password",
108+
"has_stored_credential",
111109
"last_verified_at",
112110
"last_error",
113111
"created_at",
@@ -116,7 +114,7 @@ class BlueskyCredentialsAdmin(ModelAdmin):
116114
fieldsets = (
117115
(
118116
"Account",
119-
{"fields": ("project", "handle", "app_password", "is_active")},
117+
{"fields": ("project", "handle", "credential_input", "is_active")},
120118
),
121119
(
122120
"PDS Override",
@@ -129,7 +127,7 @@ class BlueskyCredentialsAdmin(ModelAdmin):
129127
"Verification",
130128
{
131129
"fields": (
132-
"has_stored_app_password",
130+
"has_stored_credential",
133131
"last_verified_at",
134132
"last_error",
135133
"created_at",
@@ -145,11 +143,11 @@ def display_pds_host(self, obj):
145143

146144
return obj.pds_url or "Bluesky hosted default"
147145

148-
@admin.display(boolean=True, description="Stored App Password")
149-
def has_stored_app_password(self, obj):
150-
"""Return whether an encrypted app password has been configured."""
146+
@admin.display(boolean=True, description="Stored Credential")
147+
def has_stored_credential(self, obj):
148+
"""Return whether an encrypted Bluesky credential has been configured."""
151149

152-
return obj.has_app_password()
150+
return obj.has_stored_credential()
153151

154152
@admin.action(description="Verify Selected Credentials")
155153
def verify_selected_credentials(self, request, queryset):

‎core/models.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ def has_app_password(self) -> bool:
205205

206206
return bool(self.app_password_encrypted)
207207

208+
def has_stored_credential(self) -> bool:
209+
"""Return whether an encrypted Bluesky credential has been stored."""
210+
211+
return self.has_app_password()
212+
208213
def set_app_password(self, app_password: str) -> None:
209214
"""Encrypt and store the given Bluesky app password."""
210215

@@ -215,6 +220,11 @@ def set_app_password(self, app_password: str) -> None:
215220
app_password.encode("utf-8")
216221
).decode("utf-8")
217222

223+
def set_stored_credential(self, credential_value: str) -> None:
224+
"""Encrypt and store the given Bluesky credential value."""
225+
226+
self.set_app_password(credential_value)
227+
218228
def get_app_password(self) -> str:
219229
"""Decrypt and return the stored Bluesky app password."""
220230

@@ -224,6 +234,11 @@ def get_app_password(self) -> str:
224234
self.app_password_encrypted.encode("utf-8")
225235
).decode("utf-8")
226236

237+
def get_stored_credential(self) -> str:
238+
"""Decrypt and return the stored Bluesky credential value."""
239+
240+
return self.get_app_password()
241+
227242
def save(self, *args, **kwargs):
228243
"""Normalize stored account fields before persisting the credentials."""
229244

‎core/tests/test_admin.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_bluesky_credentials_admin_form_encrypts_app_password(source_admin_conte
193193
data={
194194
"project": source_admin_context.project.id,
195195
"handle": "@Alice.BSKY.social",
196-
"app_password": "app-password",
196+
"credential_input": "app-password",
197197
"pds_url": "https://pds.example.com/xrpc/",
198198
"is_active": True,
199199
}

0 commit comments

Comments
 (0)