Skip to content

Add option to load SSH keys from GitHub #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: qml
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 96 additions & 6 deletions src/OptionsPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Popup {
if (chkSetUser.checked && fieldUserName.text == "pi" && fieldUserPassword.text.length == 0) {
chkSetUser.checked = false
}
fieldPublicKey.forceActiveFocus()
radioPubKeyString.checked = true
}
}
}
Expand All @@ -188,14 +188,46 @@ Popup {
rowSpacing: -5
enabled: radioPubKeyAuthentication.checked

Text {
ImRadioButton {
id: radioPubKeyString
text: qsTr("Set authorized_keys for '%1':").arg(fieldUserName.text)
color: parent.enabled ? "black" : "grey"
onCheckedChanged: {
fieldPublicKey.forceActiveFocus()
}
}

TextField {
id: fieldPublicKey
enabled: radioPubKeyString.checked
Layout.minimumWidth: 200
}

ImRadioButton {
id: radioPubKeyGh
text: qsTr("Get authorized keys for '%1' from GitHub:").arg(fieldUserName.text)
contentItem: Text {
text: radioPubKeyGh.text
color: parent.enabled ? (fieldGithubUsername.indicateError ? "red" : "black") : "grey"
leftPadding: radioPubKeyGh.indicator.width + radioPubKeyGh.spacing
verticalAlignment: Text.AlignVCenter
}
onCheckedChanged: {
fieldGithubUsername.forceActiveFocus()
}
}

TextField {
id: fieldGithubUsername
enabled: radioPubKeyGh.checked
placeholderText: qsTr("Username")

property bool indicateError: false
property string keyString

onTextEdited: {
indicateError = false
}
}
}
}

Expand Down Expand Up @@ -432,6 +464,15 @@ Popup {
}
}

if (chkSSH.checked && radioPubKeyGh.checked && radioPubKeyAuthentication.checked){
fieldGithubUsername.keyString = getSshKeysFromGh(fieldGithubUsername.text) || ""
if (fieldGithubUsername.keyString.length == 0){
fieldGithubUsername.indicateError = true
fieldGithubUsername.forceActiveFocus()
return
}
}

applySettings()
saveSettings()
popup.close()
Expand All @@ -445,6 +486,7 @@ Popup {
}

function initialize() {
var error = false
chkBeep.checked = imageWriter.getBoolSetting("beep")
chkTelemtry.checked = imageWriter.getBoolSetting("telemetry")
chkEject.checked = imageWriter.getBoolSetting("eject")
Expand All @@ -465,8 +507,22 @@ Popup {
fieldHostname.text = settings.hostname
chkHostname.checked = true
}

// SSH Keys
if ('sshAuthorizedKeys' in settings) {
fieldPublicKey.text = settings.sshAuthorizedKeys
radioPubKeyString.checked = true
radioPubKeyAuthentication.checked = true
chkSSH.checked = true
}

else if ('githubUserName' in settings){
fieldGithubUsername.text = settings.githubUserName
fieldGithubUsername.keyString = getSshKeysFromGh(settings.githubUserName) || ""
if (fieldGithubUsername.keyString.length == 0){
error = true
}
radioPubKeyGh.checked = true
radioPubKeyAuthentication.checked = true
chkSSH.checked = true
}
Expand Down Expand Up @@ -552,7 +608,21 @@ Popup {
chkSkipFirstUse.checked = true
}

initialized = true
if (!error){
// Initialization failed
initialized = true
}
}

function validate(){
// Validate settings at write-time
if (chkSSH.checked &&
radioPubKeyAuthentication.checked &&
radioPubKeyGh.checked &&
fieldGithubUsername.keyString.length == 0){
window.onError(qsTr("Cannot fetch the key from GitHub."))
error = true
}
}

function openPopup() {
Expand Down Expand Up @@ -590,6 +660,21 @@ Popup {
cloudinitrun += "- "+cmd+"\n"
}

function getSshKeysFromGh(username){
// Load SSH public keys from github.com
// GET on https://github.com/<username>.keys
// load response as plain text

const url = "https://github.com/"+username+".keys"
var xhr = new XMLHttpRequest()
xhr.timeout = 5000
xhr.open("GET", url, false) // sync
xhr.send()
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status == 200){
return xhr.responseText.toString()
}
}

function applySettings()
{
cmdline = ""
Expand Down Expand Up @@ -639,7 +724,7 @@ Popup {
}

if (chkSSH.checked && radioPubKeyAuthentication.checked) {
var pubkey = fieldPublicKey.text
var pubkey = radioPubKeyString.checked ? fieldPublicKey.text : fieldGithubUsername.keyString
var pubkeyArr = pubkey.split("\n")

if (pubkey.length) {
Expand Down Expand Up @@ -799,7 +884,12 @@ Popup {

settings.sshEnabled = chkSSH.checked
if (chkSSH.checked && radioPubKeyAuthentication.checked) {
settings.sshAuthorizedKeys = fieldPublicKey.text
if (radioPubKeyString.checked){
settings.sshAuthorizedKeys = fieldPublicKey.text
}
else if (radioPubKeyGh.checked) {
settings.githubUserName = fieldGithubUsername.text
}
}
if (chkWifi.checked) {
settings.wifiSSID = fieldWifiSSID.text
Expand Down
4 changes: 4 additions & 0 deletions src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,10 @@ ApplicationWindow {
id: usesavedsettingspopup
onYes: {
optionspopup.initialize()
if (!optionspopup.initialized){
optionspopup.validate()
return
}
optionspopup.applySettings()
confirmwritepopup.askForConfirmation()
}
Expand Down