forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot.rs
More file actions
877 lines (775 loc) · 28.5 KB
/
copilot.rs
File metadata and controls
877 lines (775 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{LazyLock, RwLock};
static GITHUB_TOKEN_CACHE: LazyLock<RwLock<Option<String>>> = LazyLock::new(|| RwLock::new(None));
const FAILED_VALIDATION_AUTO_USE_TTL_MS: i64 = 24 * 60 * 60 * 1000;
fn cached_github_token() -> Option<String> {
GITHUB_TOKEN_CACHE
.read()
.ok()
.and_then(|value| value.clone())
}
fn cache_github_token(token: &str) {
if let Ok(mut cache) = GITHUB_TOKEN_CACHE.write() {
*cache = Some(token.to_string());
}
}
pub fn invalidate_github_token_cache() {
if let Ok(mut cache) = GITHUB_TOKEN_CACHE.write() {
*cache = None;
}
}
/// VSCode's OAuth client ID for GitHub Copilot device flow.
/// This is the well-known client ID used by VS Code, OpenCode, and other tools.
pub const GITHUB_COPILOT_CLIENT_ID: &str = "Iv1.b507a08c87ecfe98";
/// GitHub endpoints for Copilot auth
pub const GITHUB_DEVICE_CODE_URL: &str = "https://github.com/login/device/code";
pub const GITHUB_ACCESS_TOKEN_URL: &str = "https://github.com/login/oauth/access_token";
pub const COPILOT_TOKEN_URL: &str = "https://api.github.com/copilot_internal/v2/token";
/// Copilot API base URL
pub const COPILOT_API_BASE: &str = "https://api.githubcopilot.com";
pub const COPILOT_CONFIG_JSON_SOURCE_ID: &str = "copilot_config_json";
pub const COPILOT_HOSTS_AUTH_SOURCE_ID: &str = "copilot_hosts_json";
pub const COPILOT_APPS_AUTH_SOURCE_ID: &str = "copilot_apps_json";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExternalCopilotAuthSource {
ConfigJson,
HostsJson,
AppsJson,
OpenCodeAuth,
PiAuth,
}
impl ExternalCopilotAuthSource {
pub fn source_id(self) -> &'static str {
match self {
Self::ConfigJson => COPILOT_CONFIG_JSON_SOURCE_ID,
Self::HostsJson => COPILOT_HOSTS_AUTH_SOURCE_ID,
Self::AppsJson => COPILOT_APPS_AUTH_SOURCE_ID,
Self::OpenCodeAuth => crate::auth::external::OPENCODE_AUTH_JSON_SOURCE_ID,
Self::PiAuth => crate::auth::external::PI_AUTH_JSON_SOURCE_ID,
}
}
pub fn display_name(self) -> &'static str {
match self {
Self::ConfigJson => "GitHub Copilot CLI ~/.copilot/config.json",
Self::HostsJson => "GitHub Copilot CLI hosts.json",
Self::AppsJson => "GitHub Copilot apps.json",
Self::OpenCodeAuth => "OpenCode auth.json",
Self::PiAuth => "pi auth.json",
}
}
pub fn path(self) -> PathBuf {
match self {
Self::ConfigJson => copilot_cli_dir().join("config.json"),
Self::HostsJson => legacy_copilot_config_dir().join("hosts.json"),
Self::AppsJson => legacy_copilot_config_dir().join("apps.json"),
Self::OpenCodeAuth => crate::auth::external::ExternalAuthSource::OpenCode
.path()
.unwrap_or_default(),
Self::PiAuth => crate::auth::external::ExternalAuthSource::Pi
.path()
.unwrap_or_default(),
}
}
}
/// Required headers for Copilot API requests
pub const EDITOR_VERSION: &str = "jcode/1.0";
pub const EDITOR_PLUGIN_VERSION: &str = "jcode/1.0";
pub const COPILOT_INTEGRATION_ID: &str = "vscode-chat";
/// Response from GitHub device code endpoint
#[derive(Debug, Deserialize)]
pub struct DeviceCodeResponse {
pub device_code: String,
pub user_code: String,
pub verification_uri: String,
pub expires_in: u64,
pub interval: u64,
}
/// Response from GitHub access token endpoint
#[derive(Debug, Deserialize)]
pub struct AccessTokenResponse {
pub access_token: Option<String>,
pub token_type: Option<String>,
pub scope: Option<String>,
pub error: Option<String>,
pub error_description: Option<String>,
}
/// Response from Copilot token exchange endpoint
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CopilotTokenResponse {
pub token: String,
pub expires_at: i64,
}
/// Cached Copilot API token with expiry
#[derive(Debug, Clone)]
pub struct CopilotApiToken {
pub token: String,
pub expires_at: i64,
}
impl CopilotApiToken {
pub fn is_expired(&self) -> bool {
let now = chrono::Utc::now().timestamp();
// Refresh 60 seconds before actual expiry
now >= self.expires_at - 60
}
}
/// Load a GitHub OAuth token from standard Copilot/CLI config locations.
///
/// Checks in order:
/// 1. COPILOT_GITHUB_TOKEN environment variable
/// 2. GH_TOKEN environment variable
/// 3. GITHUB_TOKEN environment variable
/// 4. ~/.copilot/config.json (official Copilot CLI plaintext fallback)
/// 5. ~/.config/github-copilot/hosts.json (legacy Copilot CLI)
/// 6. ~/.config/github-copilot/apps.json (legacy VS Code)
/// 7. trusted OpenCode/pi auth.json OAuth entries
/// 8. optional `gh auth token` fallback when JCODE_COPILOT_ALLOW_GH_AUTH_TOKEN=1
pub fn load_github_token() -> Result<String> {
if let Some(token) = cached_github_token() {
return Ok(token);
}
for env_key in ["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"] {
if let Ok(token) = std::env::var(env_key)
&& !token.trim().is_empty()
{
let token = token.trim().to_string();
cache_github_token(&token);
return Ok(token);
}
}
let config_path = ExternalCopilotAuthSource::ConfigJson.path();
if crate::config::Config::external_auth_source_allowed_for_path(
COPILOT_CONFIG_JSON_SOURCE_ID,
&config_path,
) && let Ok(token) = load_token_from_config_json(&config_path)
{
cache_github_token(&token);
return Ok(token);
}
let hosts_path = ExternalCopilotAuthSource::HostsJson.path();
if crate::config::Config::external_auth_source_allowed_for_path(
COPILOT_HOSTS_AUTH_SOURCE_ID,
&hosts_path,
) && let Ok(token) = load_token_from_json(&hosts_path)
{
cache_github_token(&token);
return Ok(token);
}
let apps_path = ExternalCopilotAuthSource::AppsJson.path();
if crate::config::Config::external_auth_source_allowed_for_path(
COPILOT_APPS_AUTH_SOURCE_ID,
&apps_path,
) && let Ok(token) = load_token_from_json(&apps_path)
{
cache_github_token(&token);
return Ok(token);
}
if let Some(token) = crate::auth::external::load_copilot_oauth_token() {
cache_github_token(&token);
return Ok(token);
}
if allow_gh_cli_fallback() {
if let Some(token) = load_token_from_gh_cli() {
cache_github_token(&token);
return Ok(token);
}
}
anyhow::bail!(
"GitHub Copilot token not found. \
Set COPILOT_GITHUB_TOKEN/GH_TOKEN/GITHUB_TOKEN, run `jcode login --provider copilot`, \
or set JCODE_COPILOT_ALLOW_GH_AUTH_TOKEN=1 to explicitly reuse `gh auth token`."
)
}
fn allow_gh_cli_fallback() -> bool {
std::env::var("JCODE_COPILOT_ALLOW_GH_AUTH_TOKEN")
.ok()
.map(|value| {
let value = value.trim();
!value.is_empty() && value != "0" && !value.eq_ignore_ascii_case("false")
})
.unwrap_or(false)
}
fn copilot_env_token_present() -> bool {
["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"]
.into_iter()
.any(|env_key| {
std::env::var(env_key)
.ok()
.map(|token| !token.trim().is_empty())
.unwrap_or(false)
})
}
/// Return true when a recent `auth-test` proved the discovered Copilot token is
/// not exchangeable for a Copilot API token.
///
/// Copilot is unusual because a local GitHub OAuth token can exist while the
/// account is not entitled to Copilot, or the token is otherwise rejected by the
/// Copilot token service. Presence-only checks are still useful for explicit
/// diagnostics, but they must not cause startup/default-provider selection to
/// silently choose Copilot as a usable provider after a known token-exchange
/// failure. Environment tokens are treated as an explicit override because they
/// may be a newly supplied credential that is not represented by the saved
/// validation record.
pub fn validation_failure_blocks_auto_use() -> bool {
if copilot_env_token_present() {
return false;
}
let Some(record) = crate::auth::validation::get("copilot") else {
return false;
};
if record.success {
return false;
}
let age_ms = chrono::Utc::now()
.timestamp_millis()
.saturating_sub(record.checked_at_ms);
if age_ms > FAILED_VALIDATION_AUTO_USE_TTL_MS {
return false;
}
let summary = record.summary.to_ascii_lowercase();
summary.contains("copilot token exchange failed")
&& (summary.contains("http 401")
|| summary.contains("http 403")
|| summary.contains("feature_flag_blocked")
|| summary.contains("resource not accessible"))
}
/// Check if Copilot credentials are available (without loading the full token)
pub fn has_copilot_credentials() -> bool {
load_github_token().is_ok()
}
/// Fast local Copilot credential probe for startup-sensitive paths.
///
/// This intentionally avoids the `gh auth token` fallback because spawning the
/// GitHub CLI is too expensive for the fast auth snapshot.
pub fn has_copilot_credentials_fast() -> bool {
use crate::auth::external::{ExternalAuthSource, source_has_copilot_oauth};
for env_key in ["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"] {
if let Ok(token) = std::env::var(env_key)
&& !token.trim().is_empty()
{
cache_github_token(token.trim());
return true;
}
}
let config_path = ExternalCopilotAuthSource::ConfigJson.path();
if config_path.exists()
&& crate::config::Config::external_auth_source_allowed_for_path_cached(
COPILOT_CONFIG_JSON_SOURCE_ID,
&config_path,
)
&& let Ok(token) = load_token_from_config_json(&config_path)
{
cache_github_token(&token);
return true;
}
let hosts_path = ExternalCopilotAuthSource::HostsJson.path();
if hosts_path.exists()
&& crate::config::Config::external_auth_source_allowed_for_path_cached(
COPILOT_HOSTS_AUTH_SOURCE_ID,
&hosts_path,
)
&& let Ok(token) = load_token_from_json(&hosts_path)
{
cache_github_token(&token);
return true;
}
let apps_path = ExternalCopilotAuthSource::AppsJson.path();
if apps_path.exists()
&& crate::config::Config::external_auth_source_allowed_for_path_cached(
COPILOT_APPS_AUTH_SOURCE_ID,
&apps_path,
)
&& let Ok(token) = load_token_from_json(&apps_path)
{
cache_github_token(&token);
return true;
}
for source in [ExternalAuthSource::OpenCode, ExternalAuthSource::Pi] {
let Ok(path) = source.path() else {
continue;
};
if !path.exists() {
continue;
}
if crate::config::Config::external_auth_source_allowed_for_path_cached(
source.source_id(),
&path,
) && source_has_copilot_oauth(source)
{
if let Some(token) = crate::auth::external::load_copilot_oauth_token() {
cache_github_token(&token);
}
return true;
}
}
false
}
pub fn preferred_external_auth_source() -> Option<ExternalCopilotAuthSource> {
[
ExternalCopilotAuthSource::ConfigJson,
ExternalCopilotAuthSource::HostsJson,
ExternalCopilotAuthSource::AppsJson,
ExternalCopilotAuthSource::OpenCodeAuth,
ExternalCopilotAuthSource::PiAuth,
]
.into_iter()
.find(|source| match source {
ExternalCopilotAuthSource::OpenCodeAuth => {
let path = source.path();
path.exists()
&& !crate::auth::external::source_allowed(
crate::auth::external::ExternalAuthSource::OpenCode,
)
&& crate::auth::external::source_has_copilot_oauth(
crate::auth::external::ExternalAuthSource::OpenCode,
)
}
ExternalCopilotAuthSource::PiAuth => {
let path = source.path();
path.exists()
&& !crate::auth::external::source_allowed(
crate::auth::external::ExternalAuthSource::Pi,
)
&& crate::auth::external::source_has_copilot_oauth(
crate::auth::external::ExternalAuthSource::Pi,
)
}
_ => source.path().exists(),
})
}
pub fn has_unconsented_external_auth() -> Option<ExternalCopilotAuthSource> {
let source = preferred_external_auth_source()?;
let allowed = match source {
ExternalCopilotAuthSource::OpenCodeAuth => crate::auth::external::source_allowed(
crate::auth::external::ExternalAuthSource::OpenCode,
),
ExternalCopilotAuthSource::PiAuth => {
crate::auth::external::source_allowed(crate::auth::external::ExternalAuthSource::Pi)
}
_ => crate::config::Config::external_auth_source_allowed_for_path(
source.source_id(),
&source.path(),
),
};
if allowed { None } else { Some(source) }
}
pub fn trust_external_auth_source(source: ExternalCopilotAuthSource) -> Result<()> {
match source {
ExternalCopilotAuthSource::OpenCodeAuth => {
crate::auth::external::trust_external_auth_source(
crate::auth::external::ExternalAuthSource::OpenCode,
)?;
}
ExternalCopilotAuthSource::PiAuth => {
crate::auth::external::trust_external_auth_source(
crate::auth::external::ExternalAuthSource::Pi,
)?;
}
_ => {
crate::config::Config::allow_external_auth_source_for_path(
source.source_id(),
&source.path(),
)?;
}
}
super::AuthStatus::invalidate_cache();
Ok(())
}
fn copilot_cli_dir() -> PathBuf {
if let Ok(path) = std::env::var("JCODE_HOME") {
return PathBuf::from(path).join("external").join(".copilot");
}
let home = std::env::var("HOME").unwrap_or_default();
PathBuf::from(home).join(".copilot")
}
fn legacy_copilot_config_dir() -> PathBuf {
if let Ok(path) = std::env::var("JCODE_HOME") {
return PathBuf::from(path)
.join("external")
.join(".config")
.join("github-copilot");
}
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
PathBuf::from(xdg).join("github-copilot")
} else if cfg!(windows) {
let local_app_data = std::env::var("LOCALAPPDATA").unwrap_or_else(|_| {
let home = std::env::var("HOME").unwrap_or_default();
format!("{}/AppData/Local", home)
});
PathBuf::from(local_app_data).join("github-copilot")
} else {
let home = std::env::var("HOME").unwrap_or_default();
PathBuf::from(home).join(".config").join("github-copilot")
}
}
pub fn saved_hosts_path() -> PathBuf {
legacy_copilot_config_dir().join("hosts.json")
}
fn load_token_from_config_json(path: &Path) -> Result<String> {
let path = crate::storage::validate_external_auth_file(path)?;
let data = std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read {}", path.display()))?;
let value: Value = serde_json::from_str(&data)
.with_context(|| format!("Failed to parse {}", path.display()))?;
find_token_in_value(&value)
.ok_or_else(|| anyhow::anyhow!("No GitHub token found in {}", path.display()))
}
fn find_token_in_value(value: &Value) -> Option<String> {
match value {
Value::String(token) => normalize_candidate_token(token),
Value::Array(items) => items.iter().find_map(find_token_in_value),
Value::Object(map) => {
for key in ["oauth_token", "token", "github_token", "access_token"] {
if let Some(token) = map.get(key).and_then(find_token_in_value) {
return Some(token);
}
}
map.values().find_map(find_token_in_value)
}
_ => None,
}
}
fn normalize_candidate_token(token: &str) -> Option<String> {
let token = token.trim();
if token.is_empty() {
return None;
}
if token.starts_with("gho_")
|| token.starts_with("ghu_")
|| token.starts_with("github_pat_")
|| token.starts_with("ghs_")
{
return Some(token.to_string());
}
None
}
fn load_token_from_gh_cli() -> Option<String> {
if !crate::auth::command_exists("gh") {
return None;
}
let output = Command::new("gh").args(["auth", "token"]).output().ok()?;
if !output.status.success() {
return None;
}
let token = String::from_utf8(output.stdout).ok()?;
normalize_candidate_token(&token)
}
/// Parse a Copilot config JSON file to extract the oauth_token.
/// Format: { "github.com": { "oauth_token": "gho_xxxx", "user": "..." } }
fn load_token_from_json(path: &Path) -> Result<String> {
let path = crate::storage::validate_external_auth_file(path)?;
let data = std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read {}", path.display()))?;
let config: HashMap<String, HashMap<String, serde_json::Value>> =
serde_json::from_str(&data)
.with_context(|| format!("Failed to parse {}", path.display()))?;
let token = select_preferred_token(&config)
.ok_or_else(|| anyhow::anyhow!("No oauth_token found in {}", path.display()))?;
Ok(token.clone())
}
fn select_preferred_token(
config: &HashMap<String, HashMap<String, serde_json::Value>>,
) -> Option<&String> {
config
.iter()
.filter_map(|(host, value)| {
let token = match value.get("oauth_token") {
Some(serde_json::Value::String(token)) if !token.is_empty() => token,
_ => return None,
};
let normalized_host = normalize_github_host_key(host)?;
let raw_host = host.trim().to_ascii_lowercase();
Some((
github_host_priority(&raw_host, &normalized_host),
normalized_host,
raw_host,
token,
))
})
.min_by(|left, right| {
left.0
.cmp(&right.0)
.then_with(|| left.1.cmp(&right.1))
.then_with(|| left.2.cmp(&right.2))
})
.map(|(_, _, _, token)| token)
}
fn github_host_priority(raw_host: &str, normalized_host: &str) -> u8 {
if raw_host == "github.com" {
0
} else if normalized_host == "github.com" {
1
} else if raw_host == "api.github.com" {
2
} else if normalized_host == "api.github.com" {
3
} else {
4
}
}
fn normalize_github_host_key(host: &str) -> Option<String> {
let host = host.trim();
if host.is_empty() {
return None;
}
let host = host
.strip_prefix("https://")
.or_else(|| host.strip_prefix("http://"))
.unwrap_or(host)
.trim_end_matches('/');
let host = host.split('/').next().unwrap_or_default().trim();
let host = host.to_ascii_lowercase();
if host == "github.com" || host == "api.github.com" || host.ends_with(".github.com") {
Some(host)
} else {
None
}
}
/// Exchange a GitHub OAuth token for a short-lived Copilot API bearer token.
pub async fn exchange_github_token(
client: &reqwest::Client,
github_token: &str,
) -> Result<CopilotApiToken> {
let resp = client
.get(COPILOT_TOKEN_URL)
.header("Authorization", format!("Token {}", github_token))
.header("User-Agent", EDITOR_VERSION)
.send()
.await
.context("Failed to exchange GitHub token for Copilot token")?;
if !resp.status().is_success() {
let status = resp.status();
let body = crate::util::http_error_body(resp, "HTTP error").await;
anyhow::bail!("Copilot token exchange failed (HTTP {}): {}", status, body);
}
let token_resp: CopilotTokenResponse = resp
.json()
.await
.context("Failed to parse Copilot token response")?;
Ok(CopilotApiToken {
token: token_resp.token,
expires_at: token_resp.expires_at,
})
}
/// Initiate GitHub OAuth device flow for Copilot authentication.
/// Returns the device code response with user instructions.
pub async fn initiate_device_flow(client: &reqwest::Client) -> Result<DeviceCodeResponse> {
let resp = client
.post(GITHUB_DEVICE_CODE_URL)
.header("Accept", "application/json")
.form(&[
("client_id", GITHUB_COPILOT_CLIENT_ID),
("scope", "read:user"),
])
.send()
.await
.context("Failed to initiate GitHub device flow")?;
if !resp.status().is_success() {
let body = crate::util::http_error_body(resp, "HTTP error").await;
anyhow::bail!("GitHub device flow failed: {}", body);
}
resp.json::<DeviceCodeResponse>()
.await
.context("Failed to parse device code response")
}
/// Poll for the access token after user has authorized the device.
/// Returns the GitHub OAuth token (gho_xxx format).
pub async fn poll_for_access_token(
client: &reqwest::Client,
device_code: &str,
interval: u64,
) -> Result<String> {
loop {
tokio::time::sleep(std::time::Duration::from_secs(interval)).await;
let resp = client
.post(GITHUB_ACCESS_TOKEN_URL)
.header("Accept", "application/json")
.form(&[
("client_id", GITHUB_COPILOT_CLIENT_ID),
("device_code", device_code),
("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
])
.send()
.await
.context("Failed to poll for access token")?;
let token_resp: AccessTokenResponse = resp
.json()
.await
.context("Failed to parse access token response")?;
if let Some(token) = token_resp.access_token {
return Ok(token);
}
match token_resp.error.as_deref() {
Some("authorization_pending") => continue,
Some("slow_down") => {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
continue;
}
Some("expired_token") => {
anyhow::bail!("Device code expired. Please try again.");
}
Some("access_denied") => {
anyhow::bail!("Authorization was denied by the user.");
}
Some(err) => {
let desc = token_resp.error_description.unwrap_or_default();
anyhow::bail!("GitHub auth error: {} - {}", err, desc);
}
None => {
anyhow::bail!("Unexpected response from GitHub");
}
}
}
}
/// Save a GitHub OAuth token to the standard Copilot config location.
pub fn save_github_token(token: &str, username: &str) -> Result<()> {
let config_dir = legacy_copilot_config_dir();
std::fs::create_dir_all(&config_dir)
.with_context(|| format!("Failed to create {}", config_dir.display()))?;
crate::platform::set_directory_permissions_owner_only(&config_dir)
.with_context(|| format!("Failed to secure {}", config_dir.display()))?;
let hosts_path = config_dir.join("hosts.json");
let mut config: HashMap<String, HashMap<String, String>> =
if let Ok(data) = std::fs::read_to_string(&hosts_path) {
serde_json::from_str(&data).unwrap_or_default()
} else {
HashMap::new()
};
let mut entry = HashMap::new();
entry.insert("user".to_string(), username.to_string());
entry.insert("oauth_token".to_string(), token.to_string());
config.insert("github.com".to_string(), entry);
let json = serde_json::to_string_pretty(&config)?;
crate::storage::write_text_secret(&hosts_path, &json)
.with_context(|| format!("Failed to write {}", hosts_path.display()))?;
// A token written by jcode's own device-login flow should be immediately
// usable in future sessions. Without this, later reads treat the saved
// hosts.json as an untrusted external auth source and appear to "lose"
// the Copilot login after restart/new session.
crate::config::Config::allow_external_auth_source_for_path(
COPILOT_HOSTS_AUTH_SOURCE_ID,
&hosts_path,
)?;
super::AuthStatus::invalidate_cache();
Ok(())
}
/// Copilot account type - determines API base URL and available models
#[derive(Debug, Clone, PartialEq)]
pub enum CopilotAccountType {
Individual,
Business,
Enterprise,
Unknown,
}
impl std::fmt::Display for CopilotAccountType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CopilotAccountType::Individual => write!(f, "individual"),
CopilotAccountType::Business => write!(f, "business"),
CopilotAccountType::Enterprise => write!(f, "enterprise"),
CopilotAccountType::Unknown => write!(f, "unknown"),
}
}
}
/// Information about the user's Copilot subscription
#[derive(Debug, Clone)]
pub struct CopilotSubscriptionInfo {
pub account_type: CopilotAccountType,
pub available_models: Vec<CopilotModelInfo>,
}
/// Model info from the Copilot /models endpoint
#[derive(Debug, Clone, Deserialize)]
pub struct CopilotModelInfo {
pub id: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub vendor: String,
#[serde(default)]
pub version: String,
#[serde(default)]
pub model_picker_enabled: bool,
#[serde(default)]
pub capabilities: Option<CopilotModelCapabilities>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CopilotModelCapabilities {
#[serde(default)]
pub limits: Option<CopilotModelLimits>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CopilotModelLimits {
#[serde(default)]
pub max_context_window_tokens: Option<usize>,
#[serde(default)]
pub max_output_tokens: Option<usize>,
}
#[derive(Debug, Deserialize)]
struct ModelsResponse {
#[serde(default)]
data: Vec<CopilotModelInfo>,
}
/// Fetch available models from the Copilot API.
pub async fn fetch_available_models(
client: &reqwest::Client,
bearer_token: &str,
) -> Result<Vec<CopilotModelInfo>> {
let resp = client
.get(format!("{}/models", COPILOT_API_BASE))
.header("Authorization", format!("Bearer {}", bearer_token))
.header("Editor-Version", EDITOR_VERSION)
.header("Editor-Plugin-Version", EDITOR_PLUGIN_VERSION)
.header("Copilot-Integration-Id", COPILOT_INTEGRATION_ID)
.send()
.await
.context("Failed to fetch Copilot models")?;
if !resp.status().is_success() {
let status = resp.status();
let body = crate::util::http_error_body(resp, "HTTP error").await;
anyhow::bail!("Copilot models fetch failed (HTTP {}): {}", status, body);
}
let models_resp: ModelsResponse = resp
.json()
.await
.context("Failed to parse Copilot models response")?;
Ok(models_resp.data)
}
/// Determine the best default model based on available models.
/// - If claude-opus-4.6 is available -> paid tier -> use claude-opus-4.6
/// - Otherwise -> free/basic tier -> use claude-sonnet-4.6 or claude-sonnet-4
pub fn choose_default_model(available_models: &[CopilotModelInfo]) -> String {
let model_ids: Vec<&str> = available_models.iter().map(|m| m.id.as_str()).collect();
if model_ids.contains(&"claude-opus-4.6") {
"claude-opus-4.6".to_string()
} else if model_ids.contains(&"claude-sonnet-4.6") {
"claude-sonnet-4.6".to_string()
} else {
"claude-sonnet-4".to_string()
}
}
/// Fetch the authenticated GitHub username using an OAuth token.
pub async fn fetch_github_username(client: &reqwest::Client, token: &str) -> Result<String> {
let resp = client
.get("https://api.github.com/user")
.header("Authorization", format!("Bearer {}", token))
.header("User-Agent", EDITOR_VERSION)
.send()
.await
.context("Failed to fetch GitHub user")?;
if !resp.status().is_success() {
anyhow::bail!("Failed to fetch GitHub user (HTTP {})", resp.status());
}
#[derive(Deserialize)]
struct GithubUser {
login: String,
}
let user: GithubUser = resp.json().await.context("Failed to parse GitHub user")?;
Ok(user.login)
}
#[cfg(test)]
#[path = "copilot_auth_tests.rs"]
mod tests;