Skip to content

Commit 8775492

Browse files
committed
sshkeyexists 1st draft
1 parent 3e849f1 commit 8775492

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

resources/lib/UnityUser.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,33 @@ public function getSSHKeys(): array
300300
return $result;
301301
}
302302

303+
/* checks if key exists, ignoring the optional comment suffix */
304+
public function SSHKeyExists(string $key): bool
305+
{
306+
// if a key is 3 words, assume the 3rd word is the optional comment suffix
307+
$keyMatches = [];
308+
if (preg_match("/^\s*(\S+\s+\S+)\s+(\S+)\s*$/", $key, $keyMatches)) {
309+
$keyNoComment = $keyMatches[1];
310+
} else {
311+
$keyNoComment = $key;
312+
}
313+
foreach ($this->getSSHKeys() as $foundKey) {
314+
if ($key === $foundKey) {
315+
return true;
316+
}
317+
$foundKeyMatches = [];
318+
if (preg_match("/^\s*(\S+\s+\S+)\s+(\S+)\s*$/", $foundKey, $foundKeyMatches)) {
319+
$foundKeyNoComment = $foundKeyMatches[1];
320+
} else {
321+
$foundKeyNoComment = $foundKey;
322+
}
323+
if ($keyNoComment === $foundKeyNoComment) {
324+
return true;
325+
}
326+
}
327+
return false;
328+
}
329+
303330
/**
304331
* Sets the login shell for the account
305332
*/

0 commit comments

Comments
 (0)