scorpiox-bot-plugin-copilot-login
←ui.htmlhtml
409 lines · 16 KBHEADBlameDownload
1<!DOCTYPE html>
2<html lang="en"><head><meta charset="utf-8">
3<meta name="viewport" content="width=device-width,initial-scale=1">
4<title>copilot</title>
5<!--TOKENS-->
6<style>
7*{box-sizing:border-box}
8html,body{margin:0;min-height:100%}
9body{background:var(--bg);color:var(--text);font-family:var(--font);font-size:14px;
10 line-height:1.5;-webkit-font-smoothing:antialiased}
11#app{max-width:840px;margin:0 auto;padding:16px 16px 40px}
12.hd{display:flex;align-items:center;gap:10px;flex-wrap:wrap;margin-bottom:14px}
13.hd .grow{flex:1 1 auto;min-width:0}
14.hd h1{margin:0;font-size:18px;font-weight:650;letter-spacing:-.01em}
15.hd .sub{font-size:11.5px;color:var(--text-2);margin-top:2px;white-space:nowrap;
16 overflow:hidden;text-overflow:ellipsis}
17.pill{display:inline-flex;align-items:center;gap:6px;font-size:11.5px;font-weight:650;
18 padding:4px 10px;border-radius:999px;background:var(--surface-2);color:var(--text-2);
19 border:1px solid var(--line);white-space:nowrap}
20.pill .dot{width:7px;height:7px;border-radius:50%;background:var(--gray);flex:0 0 auto}
21.pill.ok{color:var(--green);border-color:rgba(48,209,88,.35)}
22.pill.ok .dot{background:var(--green);box-shadow:0 0 0 3px rgba(48,209,88,.15)}
23.pill.warn{color:var(--orange);border-color:rgba(255,159,10,.35)}
24.pill.warn .dot{background:var(--orange)}
25.pill.off .dot{background:var(--gray)}
26.badge{display:inline-flex;align-items:center;gap:6px;font-size:12px;font-weight:600;
27 padding:4px 10px;border-radius:999px;background:var(--surface-2);color:var(--text);
28 border:1px solid var(--line);font-family:var(--mono);max-width:100%;overflow:hidden;
29 text-overflow:ellipsis;white-space:nowrap}
30.card{background:var(--bg-card);border:1px solid var(--line);border-radius:14px;
31 padding:16px;margin-bottom:12px}
32.card h2{margin:0 0 4px;font-size:13px;font-weight:700;letter-spacing:.08em;
33 text-transform:uppercase;color:var(--text-2)}
34.card .lead{margin:0 0 12px;font-size:12.5px;color:var(--text-2)}
35.row{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
36.btn{background:var(--blue);color:#fff;border:0;border-radius:980px;padding:7px 16px;
37 font-weight:600;font-size:13px;cursor:pointer;font-family:inherit;
38 transition:transform .12s var(--ease),opacity .12s}
39.btn:active{transform:scale(.98)}
40.btn:disabled{opacity:.5;cursor:default}
41.btn.ghost{background:var(--surface-2);color:var(--text);border:1px solid var(--line)}
42.btn.ghost:hover{background:var(--surface-hover);border-color:var(--line-2)}
43.btn.danger{background:transparent;color:var(--red);border:1px solid rgba(255,69,58,.35)}
44a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none}
45.kv{display:grid;grid-template-columns:132px minmax(0,1fr);gap:6px 12px;font-size:13px}
46.kv .k{color:var(--text-2);font-size:12px}
47.kv .v{font-family:var(--mono);font-size:12px;word-break:break-all}
48.kv .v.plain{font-family:var(--font);font-size:13px}
49.step{display:none}
50.step.on{display:block}
51.hint{margin:10px 0 0;font-size:12px;color:var(--text-2)}
52.note{font-size:12.5px;margin-top:10px;display:none}
53.note.show{display:block}
54.note.err{color:var(--red)}
55.note.ok{color:var(--green)}
56.usercode{font-family:var(--mono);font-size:34px;font-weight:700;letter-spacing:.16em;
57 color:var(--text);background:var(--code-bg);border:1px solid var(--line-2);
58 border-radius:12px;padding:16px 10px;margin-top:12px;text-align:center;
59 word-break:break-all;user-select:all}
60.count{font-size:34px;font-weight:700;letter-spacing:-.02em;line-height:1.1}
61.count small{font-size:13px;font-weight:600;color:var(--text-2);letter-spacing:0}
62.wait{display:flex;align-items:center;gap:9px;margin-top:12px;font-size:12.5px;
63 color:var(--text-2)}
64.wait .spin{display:inline-block;width:13px;height:13px;border-radius:50%;
65 border:2px solid var(--line-2);border-top-color:var(--blue);animation:sp .8s linear infinite}
66@keyframes sp{to{transform:rotate(360deg)}}
67.spin2{width:12px;height:12px;border-radius:50%;border:2px solid rgba(255,255,255,.35);
68 border-top-color:#fff;display:none;animation:sp .7s linear infinite}
69.btn.loading .spin2{display:inline-block}
70.acct{display:flex;align-items:center;gap:10px;padding:9px 0;border-top:1px solid var(--line);
71 font-size:13px}
72.acct:first-child{border-top:0}
73.acct .nm{font-family:var(--mono);font-size:12.5px;flex:1 1 auto;min-width:0;
74 overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
75.acct .tag{font-size:11px;font-weight:650;padding:3px 9px;border-radius:999px;
76 background:var(--surface-2);color:var(--text-2);white-space:nowrap}
77.acct .tag.ok{color:var(--green)}
78.acct .tag.warn{color:var(--orange)}
79.empty{font-size:12.5px;color:var(--text-2)}
80.toast{position:fixed;left:50%;bottom:24px;transform:translate(-50%,12px);opacity:0;
81 pointer-events:none;background:var(--modal-bg);border:1px solid var(--line-2);
82 color:var(--text);border-radius:999px;padding:8px 16px;font-size:12.5px;
83 box-shadow:var(--shadow-lg);transition:opacity .18s var(--ease),transform .18s var(--ease);
84 z-index:9;max-width:calc(100vw - 32px)}
85.toast.show{opacity:1;transform:translate(-50%,0)}
86#themeToggle{background:var(--surface-2);border:1px solid var(--line);color:var(--text);
87 border-radius:980px;padding:5px 12px;font-size:12px;font-weight:600;cursor:pointer;
88 font-family:inherit}
89@media (max-width:520px){
90 #app{padding:12px 12px 32px}
91 .kv{grid-template-columns:1fr;gap:2px 0}
92 .kv .k{margin-top:8px}
93 .card{padding:13px;border-radius:12px}
94 .count{font-size:28px}
95 .usercode{font-size:27px;letter-spacing:.1em;padding:14px 6px}
96}
97</style>
98</head>
99<body>
100<div id="app">
101 <div class="hd">
102 <div class="grow">
103 <h1>GitHub Copilot</h1>
104 <div class="sub" id="sub">Device code login on this node</div>
105 </div>
106 <span class="badge" id="userBadge" style="display:none"></span>
107 <span class="pill off" id="pill"><span class="dot"></span><span id="pillText">Checking</span></span>
108 <button id="themeToggle" class="embed-hide" type="button">Theme</button>
109 </div>
110
111 <div class="card">
112 <h2>Session</h2>
113 <p class="lead" id="sessionLead">Reading the local Copilot credentials.</p>
114 <div class="kv" id="session"></div>
115 <div class="row" style="margin-top:14px">
116 <button class="btn ghost" id="btnRefresh" type="button"><span class="spin2"></span>Refresh Token Now</button>
117 <button class="btn ghost" id="btnReload" type="button">Reload</button>
118 </div>
119 <div class="note" id="refreshNote"></div>
120 </div>
121
122 <div class="card">
123 <h2>Device login</h2>
124 <p class="lead">Sign in with GitHub. The login CLI opens the device flow and writes the
125 credential file on this node; no token ever passes through this page.</p>
126
127 <div class="step on" id="step1">
128 <div class="row">
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>
131 </div>
132 <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve
133 it in the browser.</p>
134 <div class="note" id="startNote"></div>
135 </div>
136
137 <div class="step" id="step2">
138 <p class="hint" style="margin-top:0">Enter this code at
139 <span style="font-family:var(--mono)">github.com/login/device</span>:</p>
140 <div class="usercode" id="userCode">----</div>
141 <div class="row" style="margin-top:12px">
142 <button class="btn" id="btnCopyOpen" type="button">Copy Code &amp; Open GitHub</button>
143 <a class="btn ghost" id="linkOpen" href="https://github.com/login/device"
144 target="_blank" rel="noopener noreferrer">Open GitHub</a>
145 <button class="btn danger" id="btnCancel" type="button">Cancel</button>
146 </div>
147 <div class="wait" id="waitBox"><span class="spin"></span>
148 <span id="waitText">Waiting for GitHub confirmation...</span></div>
149 <div class="note" id="loginNote"></div>
150 </div>
151 </div>
152
153 <div class="card">
154 <h2>Named accounts</h2>
155 <p class="lead">Every account file in the local accounts directory.</p>
156 <div id="accounts"><div class="empty">No named accounts.</div></div>
157 </div>
158</div>
159
160<div class="toast" id="toast"></div>
161
162<script>
163var q=new URLSearchParams(location.search);
164var node=q.get('node')||'';
165var sid=q.get('id')||'';
166var pwd=q.get('pwd')||(node&&localStorage.getItem('bot_pwd_'+node))||
167 localStorage.getItem('bot_active_pwd')||'';
168
169/* Two call routes. Getting this wrong is the most common plugin bug:
170 * page served through the hub (bot.scorpiox.net/plugins?plugin=...&node=..)
171 * -> call the hub proxy back: /plugins?plugin=plugin_copilot_login&action=..
172 * opened directly on the node
173 * -> the plugin itself is the route: /plugin_copilot_login?action=..
174 * Calling /plugins?plugin= on the node returns the discovery list, not us. */
175var HUB=q.get('plugin')||'';
176function url(action,extra){
177 return (HUB?'/plugins?plugin='+encodeURIComponent(HUB):'/plugin_copilot_login')+
178 '?action='+encodeURIComponent(action)+
179 (extra?('&'+extra):'')+
180 (node?'&node='+encodeURIComponent(node):'')+
181 (sid?'&id='+encodeURIComponent(sid):'')+
182 (pwd?'&pwd='+encodeURIComponent(pwd):'');
183}
184function hdr(){
185 var h={};
186 if(pwd)h['X-Bot-Password']=pwd;
187 return h;
188}
189function api(action,extra){
190 return fetch(url(action,extra),{headers:hdr()}).then(function(r){
191 return r.json().catch(function(){return {ok:false,error:'bad response'};});
192 });
193}
194
195var $=function(id){return document.getElementById(id);};
196function toast(msg){
197 var t=$('toast');t.textContent=msg;t.classList.add('show');
198 clearTimeout(t._t);t._t=setTimeout(function(){t.classList.remove('show');},2200);
199}
200function note(el,msg,kind){
201 el.textContent=msg||'';
202 el.className='note'+(msg?' show '+(kind||''):'');
203}
204function kv(el,rows){
205 el.innerHTML='';
206 rows.forEach(function(r){
207 var a=document.createElement('div');a.className='k';a.textContent=r[0];
208 var b=document.createElement('div');b.className='v'+(r[2]?' plain':'');
209 b.textContent=r[1];el.appendChild(a);el.appendChild(b);
210 });
211}
212function fmtExpiry(sec){
213 if(typeof sec!=='number')return '-';
214 if(sec<=0)return 'expired';
215 if(sec<90)return sec+'s';
216 if(sec<5400)return Math.round(sec/60)+' min';
217 if(sec<172800)return Math.round(sec/3600)+' h';
218 if(sec<63072000)return Math.round(sec/86400)+' d';
219 return Math.round(sec/31536000)+' yr';
220}
221function setPill(st){
222 var p=$('pill'),t=$('pillText');
223 if(st.logged_in&&st.expired){p.className='pill warn';t.textContent='Expired';}
224 else if(st.logged_in){p.className='pill ok';t.textContent='Connected';}
225 else if(st.pending){p.className='pill warn';t.textContent='Signing in';}
226 else {p.className='pill off';t.textContent='Not Connected';}
227}
228function render(st){
229 setPill(st);
230 var badge=$('userBadge');
231 if(st.logged_in&&st.user){badge.style.display='';badge.textContent='@'+st.user;}
232 else badge.style.display='none';
233 $('sub').textContent=st.account&&st.account!=='default'
234 ? 'Device code login - '+st.account : 'Device code login on this node';
235
236 if(st.logged_in){
237 $('sessionLead').textContent=st.expired
238 ? 'A Copilot session is stored but its derived token has expired. Use Refresh Token Now.'
239 : 'This node holds a GitHub Copilot session.';
240 }else{
241 $('sessionLead').textContent='No Copilot session on this node yet. Start a login below.';
242 }
243
244 kv($('session'),[
245 ['Account', st.account||'-', true],
246 ['GitHub user', st.user||'-', true],
247 ['Logged in', st.logged_in?'yes':'no', true],
248 ['Derived token', st.logged_in?(st.expired?'expired':'valid'):'-', true],
249 ['Token expires', st.logged_in&&st.cache_present?fmtExpiry(st.expires_in_sec):'-', true],
250 ['Login pending', st.pending?'yes':'no', true],
251 ['Login CLI', st.cli_present?'found':'not found', true],
252 ['Scope', st.scope||'-', true],
253 ['Profile model', st.profile_exists?(st.profile||'-'):'not created', true],
254 ['Credentials', st.credentials_path||'-']
255 ]);
256
257 var box=$('accounts');
258 box.innerHTML='';
259 var list=st.accounts||[];
260 if(!list.length){
261 var e=document.createElement('div');e.className='empty';
262 e.textContent='No named accounts.';box.appendChild(e);
263 }else{
264 list.forEach(function(a){
265 var row=document.createElement('div');row.className='acct';
266 var nm=document.createElement('div');nm.className='nm';nm.textContent=a.name;
267 var tg=document.createElement('div');
268 tg.className='tag '+(a.logged_in?(a.expired?'warn':'ok'):'');
269 tg.textContent=a.logged_in?(a.expired?'expired':'valid'):'no token';
270 row.appendChild(nm);row.appendChild(tg);box.appendChild(row);
271 });
272 }
273
274 if(st.pending){showStep2();}
275}
276function load(){
277 return api('status').then(function(d){
278 if(d&&d.ok)render(d);
279 else toast((d&&d.error)||'status failed');
280 }).catch(function(e){toast('Status failed: '+e.message);});
281}
282
283/* ---- device flow ---- */
284var userCode='',VerifyUrl='https://github.com/login/device';
285var pollTimer=null,pollInterval=5,pollStop=0;
286
287function showStep2(){
288 $('step1').classList.remove('on');
289 $('step2').classList.add('on');
290}
291function hideStep2(){
292 $('step1').classList.add('on');
293 $('step2').classList.remove('on');
294}
295function stopPolling(){
296 if(pollTimer){clearTimeout(pollTimer);pollTimer=null;}
297}
298function startPolling(){
299 stopPolling();
300 $('waitBox').style.display='';
301 $('waitText').textContent='Waiting for GitHub confirmation...';
302 var delay=Math.max(3,pollInterval||5)*1000;
303 pollTimer=setTimeout(function(){
304 api('poll').then(function(d){
305 if(d&&d.ok&&d.state==='done'){
306 stopPolling();
307 $('waitText').textContent='Authorized.';
308 note($('loginNote'),'Signed in'+(d.user?(' as @'+d.user):'')+'.','ok');
309 hideStep2();
310 render(d);
311 toast('GitHub Copilot connected');
312 return;
313 }
314 if(!d||!d.ok){
315 stopPolling();
316 $('waitBox').style.display='none';
317 var m=(d&&(d.detail||d.error))||'poll failed';
318 note($('loginNote'),m,'err');
319 return;
320 }
321 if(d.interval)pollInterval=d.interval;
322 pollTimer=setTimeout(function(){startPolling();},Math.max(3,pollInterval)*1000);
323 }).catch(function(e){
324 note($('loginNote'),'Poll failed: '+e.message,'err');
325 pollTimer=setTimeout(function(){startPolling();},Math.max(5,pollInterval)*1000);
326 });
327 },delay);
328}
329
330function startLogin(force){
331 var b=force?$('btnRelogin'):$('btnStart');
332 b.disabled=true;b.classList.add('loading');
333 note($('startNote'),'Asking GitHub for a device code...');
334 api('start',force?'force=1':'').then(function(d){
335 b.disabled=false;b.classList.remove('loading');
336 note($('startNote'),'');
337 if(!d||!d.ok){
338 note($('startNote'),(d&&(d.detail||d.error))||'start failed','err');
339 return;
340 }
341 userCode=d.user_code||'';
342 VerifyUrl=d.verify_url||'https://github.com/login/device';
343 $('userCode').textContent=userCode||'----';
344 $('linkOpen').setAttribute('href',VerifyUrl);
345 pollInterval=d.interval||5;
346 showStep2();
347 note($('loginNote'),'');
348 startPolling();
349 }).catch(function(e){
350 b.disabled=false;b.classList.remove('loading');
351 note($('startNote'),'Start failed: '+e.message,'err');
352 });
353}
354
355function copyOpen(){
356 var go=function(){window.open(VerifyUrl,'_blank','noopener');};
357 if(navigator.clipboard&&navigator.clipboard.writeText){
358 navigator.clipboard.writeText(userCode).then(function(){
359 toast('Code copied: '+userCode);go();
360 }).catch(function(){toast('Copy failed - select the code manually');go();});
361 }else{
362 try{
363 var r=document.createRange();r.selectNodeContents($('userCode'));
364 var s=window.getSelection();s.removeAllRanges();s.addRange(r);
365 document.execCommand('copy');
366 toast('Code copied: '+userCode);
367 }catch(e){toast('Copy not available - select the code manually');}
368 go();
369 }
370}
371
372function cancelLogin(){
373 stopPolling();
374 api('cancel').then(function(){
375 hideStep2();
376 $('waitBox').style.display='';
377 note($('loginNote'),'');
378 toast('Pending login cancelled');
379 load();
380 }).catch(function(e){toast('Cancel failed: '+e.message);});
381}
382
383function refreshToken(){
384 var b=$('btnRefresh');
385 b.disabled=true;b.classList.add('loading');
386 note($('refreshNote'),'Asking Copilot for a new derived token...');
387 api('refresh').then(function(d){
388 b.disabled=false;b.classList.remove('loading');
389 if(!d||!d.ok){
390 note($('refreshNote'),(d&&(d.detail||d.error))||'refresh failed','err');
391 return;
392 }
393 note($('refreshNote'),'Token refreshed. Expires in '+fmtExpiry(d.expires_in_sec)+'.','ok');
394 render(d);
395 }).catch(function(e){
396 b.disabled=false;b.classList.remove('loading');
397 note($('refreshNote'),'Refresh failed: '+e.message,'err');
398 });
399}
400
401$('btnStart').onclick=function(){startLogin(false);};
402$('btnRelogin').onclick=function(){startLogin(true);};
403$('btnCopyOpen').onclick=copyOpen;
404$('btnCancel').onclick=cancelLogin;
405$('btnRefresh').onclick=refreshToken;
406$('btnReload').onclick=load;
407load();
408</script>
409</body></html>