| index 1ada108..ebf7e5b 100644 | ||
| --- a/README.md | ||
| +++ b/README.md | ||
| @@ -58,7 +58,7 @@ curl -fsSL https://dist.scorpiox.net/bot-plugins/copilot-login/latest.json | ||
| 58 | 58 | |
| 59 | 59 | mkdir -p ~/.scorpiox/plugins |
| 60 | 60 | curl -fsSL -o ~/.scorpiox/plugins/plugin_copilot_login \ |
| 61 | - https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.0/linux-x86_64-musl/plugin_copilot_login | |
| 61 | + https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.1/linux-x86_64-musl/plugin_copilot_login | |
| 62 | 62 | chmod 0755 ~/.scorpiox/plugins/plugin_copilot_login |
| 63 | 63 | ``` |
| 64 | 64 |
| index 5380b2c..1db0bff 100755 | ||
| --- a/release-dist.sh | ||
| +++ b/release-dist.sh | ||
| @@ -31,7 +31,7 @@ set -euo pipefail | ||
| 31 | 31 | |
| 32 | 32 | SLUG="copilot-login" |
| 33 | 33 | ID="plugin_copilot_login" |
| 34 | -VERSION="1.0.0" | |
| 34 | +VERSION="1.0.1" | |
| 35 | 35 | DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" |
| 36 | 36 | |
| 37 | 37 | ROOT="$(cd "$(dirname "$0")" && pwd)" |
| @@ -339,11 +339,25 @@ PY" || die "index.json merge failed" | ||
| 339 | 339 | |
| 340 | 340 | # ---------------------------------------------------------------- 6. mirror |
| 341 | 341 | say "6/7 plugingit mirror" |
| 342 | -if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | |
| 342 | +# The native plugingit server needs three things a plain `git push` gets wrong: | |
| 343 | +# the create call must carry a body and an `Authorization: token` header, the | |
| 344 | +# push URL user must be the literal `token`, and HTTP/2 drops the request with | |
| 345 | +# PROTOCOL_ERROR. scorpiox-cli-botplugin knows all three, so prefer it. | |
| 346 | +if [ "$DO_MIRROR" -eq 1 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then | |
| 347 | + scorpiox-cli-botplugin mirror "$ROOT" \ | |
| 348 | + --public-name "scorpiox-bot-plugin-${SLUG}" \ | |
| 349 | + --plugingit-url "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}" \ | |
| 350 | + ${PLUGINGIT_TOKEN_FILE:+--token-file "$PLUGINGIT_TOKEN_FILE"} \ | |
| 351 | + || die "mirror push failed" | |
| 352 | + echo " mirrored to $PLUGINGIT_REMOTE" | |
| 353 | +elif [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | |
| 343 | 354 | TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") |
| 344 | 355 | AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|") |
| 345 | - git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed" | |
| 346 | - git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | |
| 356 | + curl -s --http1.1 -X POST -H "Authorization: token ${TOKEN}" --data '' \ | |
| 357 | + "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}/api?r=repos&name=scorpiox-bot-plugin-${SLUG}" \ | |
| 358 | + | sed 's/^/ /' || true | |
| 359 | + git -c http.version=HTTP/1.1 push "$AUTH_REMOTE" HEAD:refs/heads/main 2>&1 | sed 's/^/ /' || die "mirror push failed" | |
| 360 | + git -c http.version=HTTP/1.1 push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | |
| 347 | 361 | echo " mirrored to $PLUGINGIT_REMOTE" |
| 348 | 362 | else |
| 349 | 363 | echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" |
| index 8c68ea2..6e9aa3e 100644 | ||
| --- a/src/plugin_copilot_login.c | ||
| +++ b/src/plugin_copilot_login.c | ||
| @@ -81,7 +81,7 @@ | ||
| 81 | 81 | |
| 82 | 82 | #define PLUGIN_SLUG "copilot-login" |
| 83 | 83 | #define PLUGIN_ID "plugin_copilot_login" |
| 84 | -#define PLUGIN_VER "1.0.0" | |
| 84 | +#define PLUGIN_VER "1.0.1" | |
| 85 | 85 | #define PLUGIN_NAME "copilot" |
| 86 | 86 | #define PLUGIN_DESC "Copilot login" |
| 87 | 87 | |
| @@ -1287,11 +1287,19 @@ static int action_start(const char *name, int force) | ||
| 1287 | 1287 | interval = json_num_field(j, end, "interval"); |
| 1288 | 1288 | expires_in = json_num_field(j, end, "expires_in"); |
| 1289 | 1289 | if (!json_bool_field(j, end, "ok", 1) || !user_code[0]) { |
| 1290 | + char err[FIELD_SZ] = {0}; | |
| 1290 | 1291 | char detail[FIELD_SZ] = {0}; |
| 1291 | - json_str_field(j, end, "error", detail, sizeof(detail)); | |
| 1292 | + json_str_field(j, end, "error", err, sizeof(err)); | |
| 1293 | + json_str_field(j, end, "detail", detail, sizeof(detail)); | |
| 1292 | 1294 | buf_free(&out); |
| 1293 | - emit_error(200, "OK", detail[0] ? detail : "start_failed", | |
| 1294 | - "the login CLI did not return a device code"); | |
| 1295 | + /* The CLI's own words beat ours: "exists" means the node already | |
| 1296 | + * has a token and the caller needs force=1, and a user staring at | |
| 1297 | + * "did not return a device code" would never guess that. */ | |
| 1298 | + emit_error(200, "OK", err[0] ? err : "start_failed", | |
| 1299 | + (err[0] && strcmp(err, "exists") == 0) | |
| 1300 | + ? "this node already has Copilot credentials; use Re-login to replace them" | |
| 1301 | + : detail[0] ? detail | |
| 1302 | + : "the login CLI did not return a device code"); | |
| 1295 | 1303 | return 1; |
| 1296 | 1304 | } |
| 1297 | 1305 | } |
| index 113d374..dfb6433 100644 | ||
| --- a/src/ui.html | ||
| +++ b/src/ui.html | ||
| @@ -127,6 +127,7 @@ a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none} | ||
| 127 | 127 | <div class="step on" id="step1"> |
| 128 | 128 | <div class="row"> |
| 129 | 129 | <button class="btn" id="btnStart" type="button">Start GitHub Copilot Login</button> |
| 130 | + <button class="btn ghost" id="btnRelogin" type="button">Re-login (replace)</button> | |
| 130 | 131 | </div> |
| 131 | 132 | <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve |
| 132 | 133 | it in the browser.</p> |
| @@ -326,11 +327,11 @@ function startPolling(){ | ||
| 326 | 327 | },delay); |
| 327 | 328 | } |
| 328 | 329 | |
| 329 | -function startLogin(){ | |
| 330 | - var b=$('btnStart'); | |
| 330 | +function startLogin(force){ | |
| 331 | + var b=force?$('btnRelogin'):$('btnStart'); | |
| 331 | 332 | b.disabled=true;b.classList.add('loading'); |
| 332 | 333 | note($('startNote'),'Asking GitHub for a device code...'); |
| 333 | - api('start').then(function(d){ | |
| 334 | + api('start',force?'force=1':'').then(function(d){ | |
| 334 | 335 | b.disabled=false;b.classList.remove('loading'); |
| 335 | 336 | note($('startNote'),''); |
| 336 | 337 | if(!d||!d.ok){ |
| @@ -397,7 +398,8 @@ function refreshToken(){ | ||
| 397 | 398 | }); |
| 398 | 399 | } |
| 399 | 400 | |
| 400 | -$('btnStart').onclick=startLogin; | |
| 401 | +$('btnStart').onclick=function(){startLogin(false);}; | |
| 402 | +$('btnRelogin').onclick=function(){startLogin(true);}; | |
| 401 | 403 | $('btnCopyOpen').onclick=copyOpen; |
| 402 | 404 | $('btnCancel').onclick=cancelLogin; |
| 403 | 405 | $('btnRefresh').onclick=refreshToken; |
| index 97db5b6..d089dea 100644 | ||
| --- a/src/ui_html.h | ||
| +++ b/src/ui_html.h | ||
| @@ -132,6 +132,7 @@ static const char *UI_HTML = | ||
| 132 | 132 | " <div class=\"step on\" id=\"step1\">\n" |
| 133 | 133 | " <div class=\"row\">\n" |
| 134 | 134 | " <button class=\"btn\" id=\"btnStart\" type=\"button\">Start GitHub Copilot Login</button>\n" |
| 135 | + " <button class=\"btn ghost\" id=\"btnRelogin\" type=\"button\">Re-login (replace)</button>\n" | |
| 135 | 136 | " </div>\n" |
| 136 | 137 | " <p class=\"hint\">This asks GitHub for a one-time code. Nothing is stored until you approve\n" |
| 137 | 138 | " it in the browser.</p>\n" |
| @@ -331,11 +332,11 @@ static const char *UI_HTML = | ||
| 331 | 332 | " },delay);\n" |
| 332 | 333 | "}\n" |
| 333 | 334 | "\n" |
| 334 | - "function startLogin(){\n" | |
| 335 | - " var b=$('btnStart');\n" | |
| 335 | + "function startLogin(force){\n" | |
| 336 | + " var b=force?$('btnRelogin'):$('btnStart');\n" | |
| 336 | 337 | " b.disabled=true;b.classList.add('loading');\n" |
| 337 | 338 | " note($('startNote'),'Asking GitHub for a device code...');\n" |
| 338 | - " api('start').then(function(d){\n" | |
| 339 | + " api('start',force?'force=1':'').then(function(d){\n" | |
| 339 | 340 | " b.disabled=false;b.classList.remove('loading');\n" |
| 340 | 341 | " note($('startNote'),'');\n" |
| 341 | 342 | " if(!d||!d.ok){\n" |
| @@ -402,7 +403,8 @@ static const char *UI_HTML = | ||
| 402 | 403 | " });\n" |
| 403 | 404 | "}\n" |
| 404 | 405 | "\n" |
| 405 | - "$('btnStart').onclick=startLogin;\n" | |
| 406 | + "$('btnStart').onclick=function(){startLogin(false);};\n" | |
| 407 | + "$('btnRelogin').onclick=function(){startLogin(true);};\n" | |
| 406 | 408 | "$('btnCopyOpen').onclick=copyOpen;\n" |
| 407 | 409 | "$('btnCancel').onclick=cancelLogin;\n" |
| 408 | 410 | "$('btnRefresh').onclick=refreshToken;\n" |
| index dd28924..d8214ef 100755 | ||
| --- a/tests/mock_login.sh | ||
| +++ b/tests/mock_login.sh | ||
| @@ -77,6 +77,12 @@ case "$MODE" in | ||
| 77 | 77 | printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n' |
| 78 | 78 | exit 1 |
| 79 | 79 | fi |
| 80 | + # A node that already holds a token refuses a second login without --force, | |
| 81 | + # exactly like the real CLI. | |
| 82 | + if [ "$FORCE" = "0" ] && [ -f "$(creds_file "$NAME")" ]; then | |
| 83 | + printf '{"ok":false,"provider":"copilot","error":"exists","detail":"credentials already exist; pass --force to replace"}\n' | |
| 84 | + exit 1 | |
| 85 | + fi | |
| 80 | 86 | p=$(pending_file "$NAME") |
| 81 | 87 | mkdir -p "$(dirname "$p")" 2>/dev/null || true |
| 82 | 88 | printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p" |
| index fb0572f..e344ddd 100755 | ||
| --- a/tests/run.sh | ||
| +++ b/tests/run.sh | ||
| @@ -236,6 +236,7 @@ check "a path-traversal account name is refused" '"error":"bad_name"' "$BADNAME" | ||
| 236 | 236 | # ---------------------------------------------------------------- start |
| 237 | 237 | section "action=start" |
| 238 | 238 | rm -f "$FIX/pend"/*.json "$FIX/mock"/* |
| 239 | +rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json" | |
| 239 | 240 | OUT=$(api "action=start&$PWDQ") |
| 240 | 241 | json_ok "start returns ok" "$OUT" 'd["ok"] is True' |
| 241 | 242 | json_ok "start returns a device_code flow" "$OUT" 'd["flow"]=="device_code"' |
| @@ -247,9 +248,22 @@ check "start writes the pending file" "" "$(ls "$FIX/pend" | head -1)" | ||
| 247 | 248 | OUT=$(api "action=status&$PWDQ") |
| 248 | 249 | json_ok "status sees the pending flow" "$OUT" 'd["pending"] is True' |
| 249 | 250 | check_not "start never returns a token" 'SECRET' "$OUT" |
| 251 | +# A node that already holds credentials: the CLI answers "exists", not "no | |
| 252 | +# code". The plugin must relay that word, because it is the only hint that | |
| 253 | +# force=1 (the Re-login button) is what the user needs. | |
| 254 | +api "action=cancel&$PWDQ" >/dev/null | |
| 255 | +mkcreds "$FIX/.credentials.json" | |
| 256 | +OUT=$(api "action=start&$PWDQ") | |
| 257 | +check "start relays the CLI's exists error verbatim" '"error":"exists"' "$OUT" | |
| 258 | +check "the exists error tells the user to re-login" 'Re-login' "$OUT" | |
| 259 | +OUT=$(api "action=start&force=1&$PWDQ") | |
| 260 | +json_ok "start with force=1 replaces the credentials" "$OUT" 'd["ok"] is True and d["user_code"]=="1234-5678"' | |
| 261 | +api "action=cancel&$PWDQ" >/dev/null | |
| 250 | 262 | |
| 251 | 263 | # ---------------------------------------------------------------- poll |
| 252 | 264 | section "action=poll" |
| 265 | +rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json" | |
| 266 | +api "action=start&$PWDQ" >/dev/null | |
| 253 | 267 | OUT=$(api "action=poll&$PWDQ") |
| 254 | 268 | json_ok "poll answers ok while waiting" "$OUT" 'd["ok"] is True' |
| 255 | 269 | json_ok "poll reports the pending state" "$OUT" 'd["state"]=="pending"' |
| @@ -263,7 +277,7 @@ json_ok "poll re-reads state and reports logged in" "$OUT" 'd["logged_in"] is Tr | ||
| 263 | 277 | check_not "poll never echoes the token" 'SECRETGITHUBTOKEN' "$OUT" |
| 264 | 278 | |
| 265 | 279 | : > "$FIX/mock/poll_denied" |
| 266 | -api "action=start&$PWDQ" >/dev/null | |
| 280 | +api "action=start&force=1&$PWDQ" >/dev/null | |
| 267 | 281 | OUT=$(api "action=poll&$PWDQ") |
| 268 | 282 | check "a denied authorization surfaces the CLI error" '"error":"denied"' "$OUT" |
| 269 | 283 | OUT=$(api "action=poll&$PWDQ") |
| 270 | 284 |
| index 1ada108..ebf7e5b 100644 | index 1ada108..ebf7e5b 100644 | ||
| --- a/README.md | --- a/README.md | ||
| +++ b/README.md | +++ b/README.md | ||
| @@ -58,7 +58,7 @@ curl -fsSL https://dist.scorpiox.net/bot-plugins/copilot-login/latest.json | @@ -58,7 +58,7 @@ curl -fsSL https://dist.scorpiox.net/bot-plugins/copilot-login/latest.json | ||
| 58 | 58 | ||
| 59 | mkdir -p ~/.scorpiox/plugins | 59 | mkdir -p ~/.scorpiox/plugins |
| 60 | curl -fsSL -o ~/.scorpiox/plugins/plugin_copilot_login \ | 60 | curl -fsSL -o ~/.scorpiox/plugins/plugin_copilot_login \ |
| 61 | - https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.0/linux-x86_64-musl/plugin_copilot_login | 61 | + https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.1/linux-x86_64-musl/plugin_copilot_login |
| 62 | chmod 0755 ~/.scorpiox/plugins/plugin_copilot_login | 62 | chmod 0755 ~/.scorpiox/plugins/plugin_copilot_login |
| 63 | ``` | 63 | ``` |
| 64 | 64 |
| index 5380b2c..1db0bff 100755 | index 5380b2c..1db0bff 100755 | ||
| --- a/release-dist.sh | --- a/release-dist.sh | ||
| +++ b/release-dist.sh | +++ b/release-dist.sh | ||
| @@ -31,7 +31,7 @@ set -euo pipefail | @@ -31,7 +31,7 @@ set -euo pipefail | ||
| 31 | 31 | ||
| 32 | SLUG="copilot-login" | 32 | SLUG="copilot-login" |
| 33 | ID="plugin_copilot_login" | 33 | ID="plugin_copilot_login" |
| 34 | -VERSION="1.0.0" | 34 | +VERSION="1.0.1" |
| 35 | DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" | 35 | DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" |
| 36 | 36 | ||
| 37 | ROOT="$(cd "$(dirname "$0")" && pwd)" | 37 | ROOT="$(cd "$(dirname "$0")" && pwd)" |
| @@ -339,11 +339,25 @@ PY" || die "index.json merge failed" | @@ -339,11 +339,25 @@ PY" || die "index.json merge failed" | ||
| 339 | 339 | ||
| 340 | # ---------------------------------------------------------------- 6. mirror | 340 | # ---------------------------------------------------------------- 6. mirror |
| 341 | say "6/7 plugingit mirror" | 341 | say "6/7 plugingit mirror" |
| 342 | -if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | 342 | +# The native plugingit server needs three things a plain `git push` gets wrong: |
| 343 | +# the create call must carry a body and an `Authorization: token` header, the | ||
| 344 | +# push URL user must be the literal `token`, and HTTP/2 drops the request with | ||
| 345 | +# PROTOCOL_ERROR. scorpiox-cli-botplugin knows all three, so prefer it. | ||
| 346 | +if [ "$DO_MIRROR" -eq 1 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then | ||
| 347 | + scorpiox-cli-botplugin mirror "$ROOT" \ | ||
| 348 | + --public-name "scorpiox-bot-plugin-${SLUG}" \ | ||
| 349 | + --plugingit-url "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}" \ | ||
| 350 | + ${PLUGINGIT_TOKEN_FILE:+--token-file "$PLUGINGIT_TOKEN_FILE"} \ | ||
| 351 | + || die "mirror push failed" | ||
| 352 | + echo " mirrored to $PLUGINGIT_REMOTE" | ||
| 353 | +elif [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | ||
| 343 | TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") | 354 | TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") |
| 344 | AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|") | 355 | AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|") |
| 345 | - git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed" | 356 | + curl -s --http1.1 -X POST -H "Authorization: token ${TOKEN}" --data '' \ |
| 346 | - git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | 357 | + "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}/api?r=repos&name=scorpiox-bot-plugin-${SLUG}" \ |
| 358 | + | sed 's/^/ /' || true | ||
| 359 | + git -c http.version=HTTP/1.1 push "$AUTH_REMOTE" HEAD:refs/heads/main 2>&1 | sed 's/^/ /' || die "mirror push failed" | ||
| 360 | + git -c http.version=HTTP/1.1 push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | ||
| 347 | echo " mirrored to $PLUGINGIT_REMOTE" | 361 | echo " mirrored to $PLUGINGIT_REMOTE" |
| 348 | else | 362 | else |
| 349 | echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" | 363 | echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" |
| index 8c68ea2..6e9aa3e 100644 | index 8c68ea2..6e9aa3e 100644 | ||
| --- a/src/plugin_copilot_login.c | --- a/src/plugin_copilot_login.c | ||
| +++ b/src/plugin_copilot_login.c | +++ b/src/plugin_copilot_login.c | ||
| @@ -81,7 +81,7 @@ | @@ -81,7 +81,7 @@ | ||
| 81 | 81 | ||
| 82 | #define PLUGIN_SLUG "copilot-login" | 82 | #define PLUGIN_SLUG "copilot-login" |
| 83 | #define PLUGIN_ID "plugin_copilot_login" | 83 | #define PLUGIN_ID "plugin_copilot_login" |
| 84 | -#define PLUGIN_VER "1.0.0" | 84 | +#define PLUGIN_VER "1.0.1" |
| 85 | #define PLUGIN_NAME "copilot" | 85 | #define PLUGIN_NAME "copilot" |
| 86 | #define PLUGIN_DESC "Copilot login" | 86 | #define PLUGIN_DESC "Copilot login" |
| 87 | 87 | ||
| @@ -1287,11 +1287,19 @@ static int action_start(const char *name, int force) | @@ -1287,11 +1287,19 @@ static int action_start(const char *name, int force) | ||
| 1287 | interval = json_num_field(j, end, "interval"); | 1287 | interval = json_num_field(j, end, "interval"); |
| 1288 | expires_in = json_num_field(j, end, "expires_in"); | 1288 | expires_in = json_num_field(j, end, "expires_in"); |
| 1289 | if (!json_bool_field(j, end, "ok", 1) || !user_code[0]) { | 1289 | if (!json_bool_field(j, end, "ok", 1) || !user_code[0]) { |
| 1290 | + char err[FIELD_SZ] = {0}; | ||
| 1290 | char detail[FIELD_SZ] = {0}; | 1291 | char detail[FIELD_SZ] = {0}; |
| 1291 | - json_str_field(j, end, "error", detail, sizeof(detail)); | 1292 | + json_str_field(j, end, "error", err, sizeof(err)); |
| 1293 | + json_str_field(j, end, "detail", detail, sizeof(detail)); | ||
| 1292 | buf_free(&out); | 1294 | buf_free(&out); |
| 1293 | - emit_error(200, "OK", detail[0] ? detail : "start_failed", | 1295 | + /* The CLI's own words beat ours: "exists" means the node already |
| 1294 | - "the login CLI did not return a device code"); | 1296 | + * has a token and the caller needs force=1, and a user staring at |
| 1297 | + * "did not return a device code" would never guess that. */ | ||
| 1298 | + emit_error(200, "OK", err[0] ? err : "start_failed", | ||
| 1299 | + (err[0] && strcmp(err, "exists") == 0) | ||
| 1300 | + ? "this node already has Copilot credentials; use Re-login to replace them" | ||
| 1301 | + : detail[0] ? detail | ||
| 1302 | + : "the login CLI did not return a device code"); | ||
| 1295 | return 1; | 1303 | return 1; |
| 1296 | } | 1304 | } |
| 1297 | } | 1305 | } |
| index 113d374..dfb6433 100644 | index 113d374..dfb6433 100644 | ||
| --- a/src/ui.html | --- a/src/ui.html | ||
| +++ b/src/ui.html | +++ b/src/ui.html | ||
| @@ -127,6 +127,7 @@ a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none} | @@ -127,6 +127,7 @@ a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none} | ||
| 127 | <div class="step on" id="step1"> | 127 | <div class="step on" id="step1"> |
| 128 | <div class="row"> | 128 | <div class="row"> |
| 129 | <button class="btn" id="btnStart" type="button">Start GitHub Copilot Login</button> | 129 | <button class="btn" id="btnStart" type="button">Start GitHub Copilot Login</button> |
| 130 | + <button class="btn ghost" id="btnRelogin" type="button">Re-login (replace)</button> | ||
| 130 | </div> | 131 | </div> |
| 131 | <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve | 132 | <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve |
| 132 | it in the browser.</p> | 133 | it in the browser.</p> |
| @@ -326,11 +327,11 @@ function startPolling(){ | @@ -326,11 +327,11 @@ function startPolling(){ | ||
| 326 | },delay); | 327 | },delay); |
| 327 | } | 328 | } |
| 328 | 329 | ||
| 329 | -function startLogin(){ | 330 | +function startLogin(force){ |
| 330 | - var b=$('btnStart'); | 331 | + var b=force?$('btnRelogin'):$('btnStart'); |
| 331 | b.disabled=true;b.classList.add('loading'); | 332 | b.disabled=true;b.classList.add('loading'); |
| 332 | note($('startNote'),'Asking GitHub for a device code...'); | 333 | note($('startNote'),'Asking GitHub for a device code...'); |
| 333 | - api('start').then(function(d){ | 334 | + api('start',force?'force=1':'').then(function(d){ |
| 334 | b.disabled=false;b.classList.remove('loading'); | 335 | b.disabled=false;b.classList.remove('loading'); |
| 335 | note($('startNote'),''); | 336 | note($('startNote'),''); |
| 336 | if(!d||!d.ok){ | 337 | if(!d||!d.ok){ |
| @@ -397,7 +398,8 @@ function refreshToken(){ | @@ -397,7 +398,8 @@ function refreshToken(){ | ||
| 397 | }); | 398 | }); |
| 398 | } | 399 | } |
| 399 | 400 | ||
| 400 | -$('btnStart').onclick=startLogin; | 401 | +$('btnStart').onclick=function(){startLogin(false);}; |
| 402 | +$('btnRelogin').onclick=function(){startLogin(true);}; | ||
| 401 | $('btnCopyOpen').onclick=copyOpen; | 403 | $('btnCopyOpen').onclick=copyOpen; |
| 402 | $('btnCancel').onclick=cancelLogin; | 404 | $('btnCancel').onclick=cancelLogin; |
| 403 | $('btnRefresh').onclick=refreshToken; | 405 | $('btnRefresh').onclick=refreshToken; |
| index 97db5b6..d089dea 100644 | index 97db5b6..d089dea 100644 | ||
| --- a/src/ui_html.h | --- a/src/ui_html.h | ||
| +++ b/src/ui_html.h | +++ b/src/ui_html.h | ||
| @@ -132,6 +132,7 @@ static const char *UI_HTML = | @@ -132,6 +132,7 @@ static const char *UI_HTML = | ||
| 132 | " <div class=\"step on\" id=\"step1\">\n" | 132 | " <div class=\"step on\" id=\"step1\">\n" |
| 133 | " <div class=\"row\">\n" | 133 | " <div class=\"row\">\n" |
| 134 | " <button class=\"btn\" id=\"btnStart\" type=\"button\">Start GitHub Copilot Login</button>\n" | 134 | " <button class=\"btn\" id=\"btnStart\" type=\"button\">Start GitHub Copilot Login</button>\n" |
| 135 | + " <button class=\"btn ghost\" id=\"btnRelogin\" type=\"button\">Re-login (replace)</button>\n" | ||
| 135 | " </div>\n" | 136 | " </div>\n" |
| 136 | " <p class=\"hint\">This asks GitHub for a one-time code. Nothing is stored until you approve\n" | 137 | " <p class=\"hint\">This asks GitHub for a one-time code. Nothing is stored until you approve\n" |
| 137 | " it in the browser.</p>\n" | 138 | " it in the browser.</p>\n" |
| @@ -331,11 +332,11 @@ static const char *UI_HTML = | @@ -331,11 +332,11 @@ static const char *UI_HTML = | ||
| 331 | " },delay);\n" | 332 | " },delay);\n" |
| 332 | "}\n" | 333 | "}\n" |
| 333 | "\n" | 334 | "\n" |
| 334 | - "function startLogin(){\n" | 335 | + "function startLogin(force){\n" |
| 335 | - " var b=$('btnStart');\n" | 336 | + " var b=force?$('btnRelogin'):$('btnStart');\n" |
| 336 | " b.disabled=true;b.classList.add('loading');\n" | 337 | " b.disabled=true;b.classList.add('loading');\n" |
| 337 | " note($('startNote'),'Asking GitHub for a device code...');\n" | 338 | " note($('startNote'),'Asking GitHub for a device code...');\n" |
| 338 | - " api('start').then(function(d){\n" | 339 | + " api('start',force?'force=1':'').then(function(d){\n" |
| 339 | " b.disabled=false;b.classList.remove('loading');\n" | 340 | " b.disabled=false;b.classList.remove('loading');\n" |
| 340 | " note($('startNote'),'');\n" | 341 | " note($('startNote'),'');\n" |
| 341 | " if(!d||!d.ok){\n" | 342 | " if(!d||!d.ok){\n" |
| @@ -402,7 +403,8 @@ static const char *UI_HTML = | @@ -402,7 +403,8 @@ static const char *UI_HTML = | ||
| 402 | " });\n" | 403 | " });\n" |
| 403 | "}\n" | 404 | "}\n" |
| 404 | "\n" | 405 | "\n" |
| 405 | - "$('btnStart').onclick=startLogin;\n" | 406 | + "$('btnStart').onclick=function(){startLogin(false);};\n" |
| 407 | + "$('btnRelogin').onclick=function(){startLogin(true);};\n" | ||
| 406 | "$('btnCopyOpen').onclick=copyOpen;\n" | 408 | "$('btnCopyOpen').onclick=copyOpen;\n" |
| 407 | "$('btnCancel').onclick=cancelLogin;\n" | 409 | "$('btnCancel').onclick=cancelLogin;\n" |
| 408 | "$('btnRefresh').onclick=refreshToken;\n" | 410 | "$('btnRefresh').onclick=refreshToken;\n" |
| index dd28924..d8214ef 100755 | index dd28924..d8214ef 100755 | ||
| --- a/tests/mock_login.sh | --- a/tests/mock_login.sh | ||
| +++ b/tests/mock_login.sh | +++ b/tests/mock_login.sh | ||
| @@ -77,6 +77,12 @@ case "$MODE" in | @@ -77,6 +77,12 @@ case "$MODE" in | ||
| 77 | printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n' | 77 | printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n' |
| 78 | exit 1 | 78 | exit 1 |
| 79 | fi | 79 | fi |
| 80 | + # A node that already holds a token refuses a second login without --force, | ||
| 81 | + # exactly like the real CLI. | ||
| 82 | + if [ "$FORCE" = "0" ] && [ -f "$(creds_file "$NAME")" ]; then | ||
| 83 | + printf '{"ok":false,"provider":"copilot","error":"exists","detail":"credentials already exist; pass --force to replace"}\n' | ||
| 84 | + exit 1 | ||
| 85 | + fi | ||
| 80 | p=$(pending_file "$NAME") | 86 | p=$(pending_file "$NAME") |
| 81 | mkdir -p "$(dirname "$p")" 2>/dev/null || true | 87 | mkdir -p "$(dirname "$p")" 2>/dev/null || true |
| 82 | printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p" | 88 | printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p" |
| index fb0572f..e344ddd 100755 | index fb0572f..e344ddd 100755 | ||
| --- a/tests/run.sh | --- a/tests/run.sh | ||
| +++ b/tests/run.sh | +++ b/tests/run.sh | ||
| @@ -236,6 +236,7 @@ check "a path-traversal account name is refused" '"error":"bad_name"' "$BADNAME" | @@ -236,6 +236,7 @@ check "a path-traversal account name is refused" '"error":"bad_name"' "$BADNAME" | ||
| 236 | # ---------------------------------------------------------------- start | 236 | # ---------------------------------------------------------------- start |
| 237 | section "action=start" | 237 | section "action=start" |
| 238 | rm -f "$FIX/pend"/*.json "$FIX/mock"/* | 238 | rm -f "$FIX/pend"/*.json "$FIX/mock"/* |
| 239 | +rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json" | ||
| 239 | OUT=$(api "action=start&$PWDQ") | 240 | OUT=$(api "action=start&$PWDQ") |
| 240 | json_ok "start returns ok" "$OUT" 'd["ok"] is True' | 241 | json_ok "start returns ok" "$OUT" 'd["ok"] is True' |
| 241 | json_ok "start returns a device_code flow" "$OUT" 'd["flow"]=="device_code"' | 242 | json_ok "start returns a device_code flow" "$OUT" 'd["flow"]=="device_code"' |
| @@ -247,9 +248,22 @@ check "start writes the pending file" "" "$(ls "$FIX/pend" | head -1)" | @@ -247,9 +248,22 @@ check "start writes the pending file" "" "$(ls "$FIX/pend" | head -1)" | ||
| 247 | OUT=$(api "action=status&$PWDQ") | 248 | OUT=$(api "action=status&$PWDQ") |
| 248 | json_ok "status sees the pending flow" "$OUT" 'd["pending"] is True' | 249 | json_ok "status sees the pending flow" "$OUT" 'd["pending"] is True' |
| 249 | check_not "start never returns a token" 'SECRET' "$OUT" | 250 | check_not "start never returns a token" 'SECRET' "$OUT" |
| 251 | +# A node that already holds credentials: the CLI answers "exists", not "no | ||
| 252 | +# code". The plugin must relay that word, because it is the only hint that | ||
| 253 | +# force=1 (the Re-login button) is what the user needs. | ||
| 254 | +api "action=cancel&$PWDQ" >/dev/null | ||
| 255 | +mkcreds "$FIX/.credentials.json" | ||
| 256 | +OUT=$(api "action=start&$PWDQ") | ||
| 257 | +check "start relays the CLI's exists error verbatim" '"error":"exists"' "$OUT" | ||
| 258 | +check "the exists error tells the user to re-login" 'Re-login' "$OUT" | ||
| 259 | +OUT=$(api "action=start&force=1&$PWDQ") | ||
| 260 | +json_ok "start with force=1 replaces the credentials" "$OUT" 'd["ok"] is True and d["user_code"]=="1234-5678"' | ||
| 261 | +api "action=cancel&$PWDQ" >/dev/null | ||
| 250 | 262 | ||
| 251 | # ---------------------------------------------------------------- poll | 263 | # ---------------------------------------------------------------- poll |
| 252 | section "action=poll" | 264 | section "action=poll" |
| 265 | +rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json" | ||
| 266 | +api "action=start&$PWDQ" >/dev/null | ||
| 253 | OUT=$(api "action=poll&$PWDQ") | 267 | OUT=$(api "action=poll&$PWDQ") |
| 254 | json_ok "poll answers ok while waiting" "$OUT" 'd["ok"] is True' | 268 | json_ok "poll answers ok while waiting" "$OUT" 'd["ok"] is True' |
| 255 | json_ok "poll reports the pending state" "$OUT" 'd["state"]=="pending"' | 269 | json_ok "poll reports the pending state" "$OUT" 'd["state"]=="pending"' |
| @@ -263,7 +277,7 @@ json_ok "poll re-reads state and reports logged in" "$OUT" 'd["logged_in"] is Tr | @@ -263,7 +277,7 @@ json_ok "poll re-reads state and reports logged in" "$OUT" 'd["logged_in"] is Tr | ||
| 263 | check_not "poll never echoes the token" 'SECRETGITHUBTOKEN' "$OUT" | 277 | check_not "poll never echoes the token" 'SECRETGITHUBTOKEN' "$OUT" |
| 264 | 278 | ||
| 265 | : > "$FIX/mock/poll_denied" | 279 | : > "$FIX/mock/poll_denied" |
| 266 | -api "action=start&$PWDQ" >/dev/null | 280 | +api "action=start&force=1&$PWDQ" >/dev/null |
| 267 | OUT=$(api "action=poll&$PWDQ") | 281 | OUT=$(api "action=poll&$PWDQ") |
| 268 | check "a denied authorization surfaces the CLI error" '"error":"denied"' "$OUT" | 282 | check "a denied authorization surfaces the CLI error" '"error":"denied"' "$OUT" |
| 269 | OUT=$(api "action=poll&$PWDQ") | 283 | OUT=$(api "action=poll&$PWDQ") |
| 270 | 284 |