Skip to content

Commit

Permalink
Rename old label to abbreviated
Browse files Browse the repository at this point in the history
also fix db migration
also fix #1176
  • Loading branch information
moalshak committed Dec 18, 2024
1 parent b3aa0a2 commit 7ec5681
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ impl MigrationTrait for Migration {
ColumnDef::new(Alias::new("label"))
.string()
.not_null()
).to_owned()
.default("Public Key")
)
.to_owned()
)
.await
}
Expand Down
12 changes: 9 additions & 3 deletions warpgate-protocol-http/src/api/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,28 @@ struct NewPublicKeyCredential {
struct ExistingPublicKeyCredential {
id: Uuid,
label: String,
abbreviated: String,
}

fn abbreviate_public_key(k: &str) -> String {
let l = 10;
if k.len() <= l {
return k.to_string(); // Return the full key if it's shorter than or equal to `l`.
}

format!(
"{}...{}",
&k[..l.min(k.len())],
&k[(k.len() - l).max(l).min(k.len() - 1)..]
&k[..l.min(k.len())], // Take the first `l` characters.
&k[k.len().saturating_sub(l)..] // Take the last `l` characters safely.
)
}

impl From<entities::PublicKeyCredential::Model> for ExistingPublicKeyCredential {
fn from(credential: entities::PublicKeyCredential::Model) -> Self {
Self {
id: credential.id,
label: abbreviate_public_key(&credential.openssh_public_key),
label: credential.label,
abbreviated: abbreviate_public_key(&credential.openssh_public_key),
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion warpgate-web/src/gateway/CredentialManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
creds!.password = state
}
async function createPublicKey (opensshPublicKey: string) {
async function createPublicKey (label: string, opensshPublicKey: string) {
const credential = await api.addMyPublicKey({
newPublicKeyCredential: {
label,
opensshPublicKey,
},
})
Expand Down Expand Up @@ -156,6 +157,7 @@
<div class="list-group-item credential">
<Fa fw icon={faKey} />
<span class="label">{credential.label}</span>
<span class="text-muted ms-2">{credential.abbreviated}</span>
<span class="ms-auto"></span>
<a
class="hover-reveal ms-2"
Expand Down
6 changes: 5 additions & 1 deletion warpgate-web/src/gateway/lib/openapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@
"type": "object",
"required": [
"id",
"label"
"label",
"abbreviated"
],
"properties": {
"id": {
Expand All @@ -697,6 +698,9 @@
},
"label": {
"type": "string"
},
"abbreviated": {
"type": "string"
}
}
},
Expand Down

0 comments on commit 7ec5681

Please sign in to comment.