- Provider: rimossi openai, Bedrock e GCP integrati e ogni fallback a OpenAI;
restano ollama, lmstudio e i provider personalizzati. Senza provider, exec e
TUI mostrano come configurarlo.
- Chiave per provider: api_key = "keyring", provider_api_key_store =
auto|keyring|file (fallback su file 0600 / ACL Windows); `codex login
--provider <id> --with-api-key`, `logout --provider`; login status e doctor
mostrano dove sta la chiave senza rivelarla.
- Modelli letti da GET {base_url}/models; contesto da config, poi dal server
(/props, meta.n_ctx), poi 32768; apply_patch come tool function; niente tool
ospitati; niente piu' avviso "metadata not found".
- Unione dei soli messaggi system/developer iniziali in un unico system;
quelli successivi restano al loro posto come user (cache del server
preservata: turno N prefisso esatto del turno N+1).
- Modelli ausiliari (memorie, guardian-v2) = modello di sessione se il
preferito gpt-* non e' nel catalogo.
- Ricerca web: crate web-tools, backend SearXNG con web_search e web_fetch
(protezione SSRF); senza backend nessuno strumento web.
- Skill: rimossa openai-docs; skill-installer punta a git.difonzo.net.
- apply_patch accetta gli header @@ -a,b +c,d @@ dei diff unificati.
- Rimossi resti: request_plugin_install, AppsInstructions, --cyber-access-program,
URL updater/realtime predefiniti.
- Fix: importazione sessioni con provider vuoto.
- Istruzioni di sistema invariate. Test: cancellati solo quelli del codice
rimosso, test di sicurezza adattati; restano solo fallimenti d'ambiente.
- Prova reale su imeAI + SearXNG: risposta, modifica file, web_search,
cache riusata, connessioni solo verso .25 e .57.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
997 lines
35 KiB
Rust
997 lines
35 KiB
Rust
//! Authentication step UI and state transitions used by onboarding.
|
|
//!
|
|
//! This module owns the auth-step state machine (ChatGPT login/device-code/API
|
|
//! key), renders the corresponding UI, and handles auth-scoped keyboard input.
|
|
//! It intentionally does not decide onboarding flow completion; the enclosing
|
|
//! onboarding screen coordinates step progression.
|
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
use codex_app_server_client::AppServerRequestHandle;
|
|
use codex_app_server_protocol::AccountUpdatedNotification;
|
|
use codex_app_server_protocol::AuthMode as ApiAuthMode;
|
|
use codex_app_server_protocol::ClientRequest;
|
|
use codex_app_server_protocol::LoginAccountParams;
|
|
use codex_app_server_protocol::LoginAccountResponse;
|
|
use codex_login::AuthConfig;
|
|
use codex_login::read_openai_api_key_from_env;
|
|
use codex_protocol::auth::AuthMode;
|
|
use crossterm::event::KeyCode;
|
|
use crossterm::event::KeyEvent;
|
|
use crossterm::event::KeyEventKind;
|
|
use crossterm::event::KeyModifiers;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::layout::Constraint;
|
|
use ratatui::layout::Layout;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::prelude::Widget;
|
|
use ratatui::style::Color;
|
|
use ratatui::style::Modifier;
|
|
use ratatui::style::Style;
|
|
use ratatui::style::Stylize;
|
|
use ratatui::text::Line;
|
|
use ratatui::widgets::Block;
|
|
use ratatui::widgets::BorderType;
|
|
use ratatui::widgets::Borders;
|
|
use ratatui::widgets::Paragraph;
|
|
use ratatui::widgets::WidgetRef;
|
|
use ratatui::widgets::Wrap;
|
|
|
|
use codex_protocol::config_types::ForcedLoginMethod;
|
|
use std::sync::Arc;
|
|
use std::sync::RwLock;
|
|
use std::time::Instant;
|
|
use uuid::Uuid;
|
|
|
|
use crate::LoginStatus;
|
|
use crate::activation_key_guard::ActivationKeyGuard;
|
|
use crate::key_hint::KeyBinding;
|
|
use crate::key_hint::KeyBindingListExt;
|
|
use crate::onboarding::gateway::GatewayStep;
|
|
use crate::onboarding::gateway::USE_CUSTOM_PROVIDER;
|
|
use crate::onboarding::keys;
|
|
use crate::onboarding::onboarding_screen::KeyboardHandler;
|
|
use crate::onboarding::onboarding_screen::StepStateProvider;
|
|
use crate::tui::FrameRequester;
|
|
|
|
/// Marks buffer cells that have cyan+underlined style as an OSC 8 hyperlink.
|
|
///
|
|
/// Terminal emulators recognise the OSC 8 escape sequence and treat the entire
|
|
/// marked region as a single clickable link, regardless of row wrapping. This
|
|
/// is necessary because ratatui's cell-based rendering emits `MoveTo` at every
|
|
/// row boundary, which breaks normal terminal URL detection for long URLs that
|
|
/// wrap across multiple rows.
|
|
pub(crate) fn mark_url_hyperlink(buf: &mut Buffer, area: Rect, url: &str) {
|
|
crate::terminal_hyperlinks::mark_url_hyperlink(buf, area, url);
|
|
}
|
|
|
|
use super::onboarding_screen::StepState;
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) enum SignInState {
|
|
PickMode,
|
|
ApiKeyEntry(ApiKeyInputState),
|
|
ApiKeyConfigured,
|
|
GatewaySelected(String),
|
|
GatewayOAuth {
|
|
provider_id: String,
|
|
url: Option<String>,
|
|
},
|
|
GatewayOAuthFailed {
|
|
provider_id: String,
|
|
message: String,
|
|
},
|
|
GatewayConfigured,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(crate) enum SignInOption {
|
|
ApiKey,
|
|
Gateway,
|
|
}
|
|
|
|
const API_KEY_DISABLED_MESSAGE: &str = "API key login is disabled.";
|
|
pub(super) fn onboarding_request_id() -> codex_app_server_protocol::RequestId {
|
|
codex_app_server_protocol::RequestId::String(Uuid::new_v4().to_string())
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
pub(crate) struct ApiKeyInputState {
|
|
value: String,
|
|
prepopulated_from_env: bool,
|
|
}
|
|
|
|
impl KeyboardHandler for AuthModeWidget {
|
|
fn handle_key_event(&mut self, key_event: KeyEvent) {
|
|
if self
|
|
.activation_key_guard
|
|
.as_mut()
|
|
.is_some_and(|guard| guard.consume(KeyBinding::from_event(key_event), key_event.kind))
|
|
|| key_event.kind == KeyEventKind::Release
|
|
{
|
|
return;
|
|
}
|
|
if self.handle_gateway_key_event(key_event) {
|
|
return;
|
|
}
|
|
if self.handle_api_key_entry_key_event(&key_event) {
|
|
return;
|
|
}
|
|
|
|
if keys::MOVE_UP.is_pressed(key_event) {
|
|
self.move_highlight(/*delta*/ -1);
|
|
return;
|
|
}
|
|
if keys::MOVE_DOWN.is_pressed(key_event) {
|
|
self.move_highlight(/*delta*/ 1);
|
|
return;
|
|
}
|
|
if key_event.kind != KeyEventKind::Press {
|
|
return;
|
|
}
|
|
if keys::SELECT_FIRST.is_pressed(key_event) {
|
|
self.select_option_by_index(/*index*/ 0, key_event);
|
|
return;
|
|
}
|
|
if keys::SELECT_SECOND.is_pressed(key_event) {
|
|
self.select_option_by_index(/*index*/ 1, key_event);
|
|
return;
|
|
}
|
|
if keys::SELECT_THIRD.is_pressed(key_event) {
|
|
self.select_option_by_index(/*index*/ 2, key_event);
|
|
return;
|
|
}
|
|
if keys::SELECT_FOURTH.is_pressed(key_event) {
|
|
self.select_option_by_index(/*index*/ 3, key_event);
|
|
return;
|
|
}
|
|
if keys::CONFIRM.is_pressed(key_event) {
|
|
let sign_in_state = { (*self.sign_in_state.read().unwrap()).clone() };
|
|
match sign_in_state {
|
|
SignInState::GatewayOAuthFailed { provider_id, .. } => {
|
|
self.start_gateway_oauth(provider_id)
|
|
}
|
|
SignInState::PickMode => {
|
|
self.activate_highlighted_mode(key_event);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn handle_paste(&mut self, pasted: String) {
|
|
if let Some(wizard) = &mut self.gateway {
|
|
if let Some(input) = wizard.active_input_mut() {
|
|
input.paste(&pasted);
|
|
self.request_frame.schedule_frame();
|
|
}
|
|
return;
|
|
}
|
|
let is_api_key_entry = matches!(
|
|
*self.sign_in_state.read().unwrap(),
|
|
SignInState::ApiKeyEntry(_)
|
|
);
|
|
if is_api_key_entry {
|
|
let _ = self.handle_api_key_entry_paste(pasted);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub(crate) struct AuthModeWidget {
|
|
pub request_frame: FrameRequester,
|
|
pub highlighted_mode: SignInOption,
|
|
pub(super) activation_key_guard: Option<ActivationKeyGuard>,
|
|
pub error: Arc<RwLock<Option<String>>>,
|
|
pub sign_in_state: Arc<RwLock<SignInState>>,
|
|
pub login_status: LoginStatus,
|
|
pub app_server_target: crate::AppServerTarget,
|
|
pub app_server_request_handle: AppServerRequestHandle,
|
|
pub auth_config: AuthConfig,
|
|
pub gateway_setup_enabled: bool,
|
|
pub(super) gateway: Option<GatewayStep>,
|
|
}
|
|
|
|
impl AuthModeWidget {
|
|
pub(super) fn set_error(&self, message: Option<String>) {
|
|
*self.error.write().unwrap() = message;
|
|
}
|
|
|
|
fn error_message(&self) -> Option<String> {
|
|
self.error.read().unwrap().clone()
|
|
}
|
|
|
|
/// Returns whether the auth flow is currently accepting text input.
|
|
pub(crate) fn is_text_entry_active(&self) -> bool {
|
|
if let Some(wizard) = &self.gateway {
|
|
return wizard.is_text_entry_active();
|
|
}
|
|
self.sign_in_state.read().is_ok_and(|guard| match &*guard {
|
|
SignInState::ApiKeyEntry(_) => true,
|
|
_ => false,
|
|
})
|
|
}
|
|
|
|
/// Returns whether printable quit shortcuts must be treated as text input.
|
|
///
|
|
/// API-key entry keeps its existing empty-field quit behavior, while
|
|
/// provider setup fields accept printable input from their first character.
|
|
pub(crate) fn should_suppress_printable_quit(&self) -> bool {
|
|
if let Some(wizard) = &self.gateway {
|
|
return wizard.is_text_entry_active();
|
|
}
|
|
self.sign_in_state.read().is_ok_and(|guard| match &*guard {
|
|
SignInState::ApiKeyEntry(state) => !state.value.is_empty(),
|
|
_ => false,
|
|
})
|
|
}
|
|
|
|
fn confirm_binding(&self) -> KeyBinding {
|
|
keys::CONFIRM[0]
|
|
}
|
|
|
|
fn cancel_binding(&self) -> KeyBinding {
|
|
keys::CANCEL[0]
|
|
}
|
|
|
|
fn is_api_login_allowed(&self) -> bool {
|
|
self.auth_config
|
|
.is_login_method_allowed(ForcedLoginMethod::Api)
|
|
}
|
|
|
|
fn displayed_sign_in_options(&self) -> Vec<SignInOption> {
|
|
let mut options = vec![SignInOption::ApiKey];
|
|
if self.gateway_setup_enabled {
|
|
options.push(SignInOption::Gateway);
|
|
}
|
|
options
|
|
}
|
|
|
|
fn move_highlight(&mut self, delta: isize) {
|
|
if !matches!(*self.sign_in_state.read().unwrap(), SignInState::PickMode) {
|
|
return;
|
|
}
|
|
let options = self.displayed_sign_in_options();
|
|
if options.is_empty() {
|
|
return;
|
|
}
|
|
|
|
let current_index = options
|
|
.iter()
|
|
.position(|option| *option == self.highlighted_mode)
|
|
.unwrap_or(0);
|
|
let next_index =
|
|
(current_index as isize + delta).rem_euclid(options.len() as isize) as usize;
|
|
self.highlighted_mode = options[next_index];
|
|
}
|
|
|
|
fn select_option_by_index(&mut self, index: usize, key_event: KeyEvent) {
|
|
if !matches!(*self.sign_in_state.read().unwrap(), SignInState::PickMode) {
|
|
return;
|
|
}
|
|
let options = self.displayed_sign_in_options();
|
|
if let Some(option) = options.get(index).copied() {
|
|
self.highlighted_mode = option;
|
|
self.activate_highlighted_mode(key_event);
|
|
}
|
|
}
|
|
|
|
fn activate_highlighted_mode(&mut self, key_event: KeyEvent) {
|
|
let option = self.highlighted_mode;
|
|
self.guard_activation(key_event);
|
|
self.handle_sign_in_option(option);
|
|
}
|
|
|
|
pub(super) fn guard_activation(&mut self, key_event: KeyEvent) {
|
|
// Enter and printable shortcuts can repeat as plain presses without a release.
|
|
self.activation_key_guard = Some(ActivationKeyGuard::new_at(
|
|
KeyBinding::from_event(key_event),
|
|
Instant::now(),
|
|
/*reported_repeats_only*/ false,
|
|
));
|
|
}
|
|
|
|
fn handle_sign_in_option(&mut self, option: SignInOption) {
|
|
match option {
|
|
SignInOption::ApiKey => {
|
|
if self.is_api_login_allowed() {
|
|
self.start_api_key_entry();
|
|
} else {
|
|
self.disallow_api_login();
|
|
}
|
|
}
|
|
SignInOption::Gateway => {
|
|
if self.gateway_setup_enabled && self.is_api_login_allowed() {
|
|
self.start_gateway_picker();
|
|
} else if !self.is_api_login_allowed() {
|
|
self.disallow_api_login();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn disallow_api_login(&mut self) {
|
|
self.highlighted_mode = SignInOption::ApiKey;
|
|
self.set_error(Some(API_KEY_DISABLED_MESSAGE.to_string()));
|
|
*self.sign_in_state.write().unwrap() = SignInState::PickMode;
|
|
self.request_frame.schedule_frame();
|
|
}
|
|
|
|
fn render_pick_mode(&self, area: Rect, buf: &mut Buffer) {
|
|
let mut lines: Vec<Line> = if self.gateway_setup_enabled {
|
|
vec![" Choose how you want to use aKode.".into(), "".into()]
|
|
} else {
|
|
vec![" Connect an API key to use aKode.".into(), "".into()]
|
|
};
|
|
|
|
let options_start = lines.len();
|
|
let mut compact_options = Vec::new();
|
|
let mut create_mode_item = |idx: usize,
|
|
selected_mode: SignInOption,
|
|
text: &str,
|
|
description: &str|
|
|
-> Vec<Line<'static>> {
|
|
let is_selected = self.highlighted_mode == selected_mode;
|
|
let caret = if is_selected { ">" } else { " " };
|
|
|
|
let line1 = if is_selected {
|
|
Line::from(vec![
|
|
format!("{caret} {index}. ", index = idx + 1).cyan().dim(),
|
|
text.to_string().cyan(),
|
|
])
|
|
} else {
|
|
format!(" {index}. {text}", index = idx + 1).into()
|
|
};
|
|
|
|
let line2 = if is_selected {
|
|
Line::from(format!(" {description}"))
|
|
.fg(Color::Cyan)
|
|
.add_modifier(Modifier::DIM)
|
|
} else {
|
|
Line::from(format!(" {description}"))
|
|
.style(Style::default().add_modifier(Modifier::DIM))
|
|
};
|
|
|
|
compact_options.push(line1.clone());
|
|
vec![line1, line2]
|
|
};
|
|
|
|
for (idx, option) in self.displayed_sign_in_options().into_iter().enumerate() {
|
|
match option {
|
|
SignInOption::ApiKey => {
|
|
lines.extend(create_mode_item(
|
|
idx,
|
|
option,
|
|
"Use an OpenAI API key",
|
|
"Pay for what you use",
|
|
));
|
|
}
|
|
SignInOption::Gateway => {
|
|
lines.extend(create_mode_item(
|
|
idx,
|
|
option,
|
|
USE_CUSTOM_PROVIDER,
|
|
"Choose a saved configuration or create a new one",
|
|
));
|
|
}
|
|
}
|
|
lines.push("".into());
|
|
}
|
|
|
|
let options_end = lines.len();
|
|
if !self.is_api_login_allowed() {
|
|
lines.push(
|
|
" API key login is disabled by your administrator."
|
|
.dim()
|
|
.into(),
|
|
);
|
|
lines.push("".into());
|
|
}
|
|
lines.push(Line::from(vec![
|
|
" Press ".dim(),
|
|
self.confirm_binding().into(),
|
|
" to continue".dim(),
|
|
]));
|
|
if let Some(err) = self.error_message() {
|
|
lines.push("".into());
|
|
lines.push(err.red().into());
|
|
}
|
|
|
|
// Keep the options and confirmation hint visible in short terminals.
|
|
if Paragraph::new(lines.clone())
|
|
.wrap(Wrap { trim: false })
|
|
.line_count(area.width)
|
|
> usize::from(area.height)
|
|
{
|
|
lines.splice(options_start..options_end, compact_options);
|
|
}
|
|
Paragraph::new(lines)
|
|
.wrap(Wrap { trim: false })
|
|
.render(area, buf);
|
|
}
|
|
|
|
fn render_api_key_configured(&self, area: Rect, buf: &mut Buffer) {
|
|
let lines = vec![
|
|
"✓ API key configured".fg(Color::Green).into(),
|
|
"".into(),
|
|
" aKode will use usage-based billing with your API key.".into(),
|
|
];
|
|
|
|
Paragraph::new(lines)
|
|
.wrap(Wrap { trim: false })
|
|
.render(area, buf);
|
|
}
|
|
|
|
fn render_api_key_entry(&self, area: Rect, buf: &mut Buffer, state: &ApiKeyInputState) {
|
|
let [intro_area, input_area, footer_area] = Layout::vertical([
|
|
Constraint::Min(4),
|
|
Constraint::Length(3),
|
|
Constraint::Min(2),
|
|
])
|
|
.areas(area);
|
|
|
|
let mut intro_lines: Vec<Line> = vec![
|
|
Line::from(vec![
|
|
"> ".into(),
|
|
"Use your own OpenAI API key for usage-based billing".bold(),
|
|
]),
|
|
"".into(),
|
|
" Paste or type your API key below.".into(),
|
|
"".into(),
|
|
];
|
|
if state.prepopulated_from_env {
|
|
intro_lines.push(" Detected OPENAI_API_KEY environment variable.".into());
|
|
intro_lines.push(
|
|
" Paste a different key if you prefer to use another account."
|
|
.dim()
|
|
.into(),
|
|
);
|
|
intro_lines.push("".into());
|
|
}
|
|
Paragraph::new(intro_lines)
|
|
.wrap(Wrap { trim: false })
|
|
.render(intro_area, buf);
|
|
|
|
let content_line: Line = if state.value.is_empty() {
|
|
vec!["Paste or type your API key".dim()].into()
|
|
} else {
|
|
Line::from(state.value.clone())
|
|
};
|
|
Paragraph::new(content_line)
|
|
.wrap(Wrap { trim: false })
|
|
.block(
|
|
Block::default()
|
|
.title("API key")
|
|
.borders(Borders::ALL)
|
|
.border_type(BorderType::Rounded)
|
|
.border_style(Style::default().fg(Color::Cyan)),
|
|
)
|
|
.render(input_area, buf);
|
|
|
|
let mut footer_lines: Vec<Line> = vec![
|
|
Line::from(vec![
|
|
" Press ".dim(),
|
|
self.confirm_binding().into(),
|
|
" to save".dim(),
|
|
]),
|
|
Line::from(vec![
|
|
" Press ".dim(),
|
|
self.cancel_binding().into(),
|
|
" to go back".dim(),
|
|
]),
|
|
];
|
|
if let Some(error) = self.error_message() {
|
|
footer_lines.push("".into());
|
|
footer_lines.push(error.red().into());
|
|
}
|
|
Paragraph::new(footer_lines)
|
|
.wrap(Wrap { trim: false })
|
|
.render(footer_area, buf);
|
|
}
|
|
|
|
fn handle_api_key_entry_key_event(&mut self, key_event: &KeyEvent) -> bool {
|
|
let mut should_save: Option<String> = None;
|
|
let mut should_request_frame = false;
|
|
|
|
{
|
|
let mut guard = self.sign_in_state.write().unwrap();
|
|
if let SignInState::ApiKeyEntry(state) = &mut *guard {
|
|
if keys::CANCEL.is_pressed(*key_event) {
|
|
*guard = SignInState::PickMode;
|
|
self.set_error(/*message*/ None);
|
|
should_request_frame = true;
|
|
} else if key_event.kind == KeyEventKind::Press
|
|
&& keys::CONFIRM.is_pressed(*key_event)
|
|
{
|
|
let trimmed = state.value.trim().to_string();
|
|
if trimmed.is_empty() {
|
|
self.set_error(Some("API key cannot be empty".to_string()));
|
|
should_request_frame = true;
|
|
} else {
|
|
should_save = Some(trimmed);
|
|
}
|
|
} else {
|
|
match key_event.code {
|
|
KeyCode::Backspace => {
|
|
if state.prepopulated_from_env {
|
|
state.value.clear();
|
|
state.prepopulated_from_env = false;
|
|
} else {
|
|
state.value.pop();
|
|
}
|
|
self.set_error(/*message*/ None);
|
|
should_request_frame = true;
|
|
}
|
|
KeyCode::Char(c)
|
|
if key_event.kind == KeyEventKind::Press
|
|
&& !key_event.modifiers.contains(KeyModifiers::SUPER)
|
|
&& !key_event.modifiers.contains(KeyModifiers::CONTROL)
|
|
&& !key_event.modifiers.contains(KeyModifiers::ALT) =>
|
|
{
|
|
if state.prepopulated_from_env {
|
|
state.value.clear();
|
|
state.prepopulated_from_env = false;
|
|
}
|
|
state.value.push(c);
|
|
self.set_error(/*message*/ None);
|
|
should_request_frame = true;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
// handled; let guard drop before potential save
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if let Some(api_key) = should_save {
|
|
self.save_api_key(api_key);
|
|
} else if should_request_frame {
|
|
self.request_frame.schedule_frame();
|
|
}
|
|
true
|
|
}
|
|
|
|
fn handle_api_key_entry_paste(&mut self, pasted: String) -> bool {
|
|
let trimmed = pasted.trim();
|
|
if trimmed.is_empty() {
|
|
return false;
|
|
}
|
|
|
|
let mut guard = self.sign_in_state.write().unwrap();
|
|
if let SignInState::ApiKeyEntry(state) = &mut *guard {
|
|
if state.prepopulated_from_env {
|
|
state.value = trimmed.to_string();
|
|
state.prepopulated_from_env = false;
|
|
} else {
|
|
state.value.push_str(trimmed);
|
|
}
|
|
self.set_error(/*message*/ None);
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
drop(guard);
|
|
self.request_frame.schedule_frame();
|
|
true
|
|
}
|
|
|
|
pub(super) fn start_api_key_entry(&mut self) {
|
|
if !self.is_api_login_allowed() {
|
|
self.disallow_api_login();
|
|
return;
|
|
}
|
|
self.set_error(/*message*/ None);
|
|
let prefill_from_env = read_openai_api_key_from_env();
|
|
let mut guard = self.sign_in_state.write().unwrap();
|
|
match &mut *guard {
|
|
SignInState::ApiKeyEntry(state) => {
|
|
if state.value.is_empty() {
|
|
if let Some(prefill) = prefill_from_env {
|
|
state.value = prefill;
|
|
state.prepopulated_from_env = true;
|
|
} else {
|
|
state.prepopulated_from_env = false;
|
|
}
|
|
}
|
|
}
|
|
_ => {
|
|
*guard = SignInState::ApiKeyEntry(ApiKeyInputState {
|
|
value: prefill_from_env.clone().unwrap_or_default(),
|
|
prepopulated_from_env: prefill_from_env.is_some(),
|
|
});
|
|
}
|
|
}
|
|
drop(guard);
|
|
self.request_frame.schedule_frame();
|
|
}
|
|
|
|
fn save_api_key(&mut self, api_key: String) {
|
|
if !self.is_api_login_allowed() {
|
|
self.disallow_api_login();
|
|
return;
|
|
}
|
|
self.set_error(/*message*/ None);
|
|
let request_handle = self.app_server_request_handle.clone();
|
|
let sign_in_state = self.sign_in_state.clone();
|
|
let error = self.error.clone();
|
|
let request_frame = self.request_frame.clone();
|
|
tokio::spawn(async move {
|
|
match request_handle
|
|
.request_typed::<LoginAccountResponse>(ClientRequest::LoginAccount {
|
|
request_id: onboarding_request_id(),
|
|
params: LoginAccountParams::ApiKey {
|
|
api_key: api_key.clone(),
|
|
},
|
|
})
|
|
.await
|
|
{
|
|
Ok(LoginAccountResponse::ApiKey {}) => {
|
|
*error.write().unwrap() = None;
|
|
*sign_in_state.write().unwrap() = SignInState::ApiKeyConfigured;
|
|
}
|
|
Err(err) => {
|
|
*error.write().unwrap() = Some(format!("Failed to save API key: {err}"));
|
|
*sign_in_state.write().unwrap() = SignInState::ApiKeyEntry(ApiKeyInputState {
|
|
value: api_key,
|
|
prepopulated_from_env: false,
|
|
});
|
|
}
|
|
}
|
|
request_frame.schedule_frame();
|
|
});
|
|
self.request_frame.schedule_frame();
|
|
}
|
|
|
|
pub(crate) fn on_account_updated(&mut self, notification: AccountUpdatedNotification) {
|
|
self.login_status = notification
|
|
.auth_mode
|
|
.map(|auth_mode| {
|
|
LoginStatus::AuthMode(match auth_mode {
|
|
ApiAuthMode::ApiKey => AuthMode::ApiKey,
|
|
ApiAuthMode::Headers => AuthMode::Headers,
|
|
})
|
|
})
|
|
.unwrap_or(LoginStatus::NotAuthenticated);
|
|
}
|
|
}
|
|
|
|
impl StepStateProvider for AuthModeWidget {
|
|
fn get_step_state(&self) -> StepState {
|
|
let sign_in_state = self.sign_in_state.read().unwrap();
|
|
match &*sign_in_state {
|
|
SignInState::PickMode
|
|
| SignInState::ApiKeyEntry(_)
|
|
| SignInState::GatewayOAuth { .. }
|
|
| SignInState::GatewayOAuthFailed { .. } => StepState::InProgress,
|
|
SignInState::ApiKeyConfigured
|
|
| SignInState::GatewaySelected(_)
|
|
| SignInState::GatewayConfigured => StepState::Complete,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl WidgetRef for AuthModeWidget {
|
|
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
|
|
if let Some(wizard) = &self.gateway {
|
|
wizard.render(area, buf);
|
|
return;
|
|
}
|
|
let sign_in_state = self.sign_in_state.read().unwrap();
|
|
match &*sign_in_state {
|
|
SignInState::PickMode => {
|
|
self.render_pick_mode(area, buf);
|
|
}
|
|
SignInState::ApiKeyEntry(state) => {
|
|
self.render_api_key_entry(area, buf, state);
|
|
}
|
|
SignInState::ApiKeyConfigured => {
|
|
self.render_api_key_configured(area, buf);
|
|
}
|
|
SignInState::GatewayOAuth { url, .. } => {
|
|
let mut lines = vec![Line::from(
|
|
"Finish signing in to your gateway in the browser.".bold(),
|
|
)];
|
|
if let Some(url) = url {
|
|
lines.push(Line::from(url.clone().cyan().underlined()));
|
|
}
|
|
lines.push(Line::from("Press Ctrl+C to quit.".dim()));
|
|
Paragraph::new(lines)
|
|
.wrap(Wrap { trim: false })
|
|
.render(area, buf);
|
|
if let Some(url) = url {
|
|
mark_url_hyperlink(buf, area, url);
|
|
}
|
|
}
|
|
SignInState::GatewayOAuthFailed { message, .. } => {
|
|
Paragraph::new(vec![
|
|
Line::from(message.clone().red()),
|
|
Line::from("Press Enter to retry or Ctrl+C to quit.".dim()),
|
|
])
|
|
.wrap(Wrap { trim: false })
|
|
.render(area, buf);
|
|
}
|
|
SignInState::GatewaySelected(_) | SignInState::GatewayConfigured => {
|
|
Paragraph::new("✓ Custom provider configured".green())
|
|
.wrap(Wrap { trim: false })
|
|
.render(area, buf);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::legacy_core::config::ConfigBuilder;
|
|
use codex_app_server_client::AppServerRequestHandle;
|
|
use codex_app_server_client::DEFAULT_IN_PROCESS_CHANNEL_CAPACITY;
|
|
use codex_app_server_client::InProcessAppServerClient;
|
|
use codex_app_server_client::InProcessClientStartArgs;
|
|
use codex_arg0::Arg0DispatchPaths;
|
|
use codex_config::LoaderOverrides;
|
|
use codex_features::Feature;
|
|
use pretty_assertions::assert_eq;
|
|
use std::sync::Arc;
|
|
use tempfile::TempDir;
|
|
|
|
pub(super) async fn widget_api_login_forbidden() -> (AuthModeWidget, TempDir) {
|
|
let codex_home = TempDir::new().unwrap();
|
|
let codex_home_path = codex_home.path().to_path_buf();
|
|
// Embedded startup can refresh configuration after first paint.
|
|
let loader_overrides = LoaderOverrides {
|
|
ignore_managed_requirements: true,
|
|
managed_config_path: Some(codex_home_path.join("managed_config.toml")),
|
|
..LoaderOverrides::without_managed_config_for_tests()
|
|
};
|
|
let config = ConfigBuilder::default()
|
|
.loader_overrides(loader_overrides.clone())
|
|
.codex_home(codex_home_path.clone())
|
|
.build()
|
|
.await
|
|
.unwrap();
|
|
let mut auth_config = config.auth_config();
|
|
let client = InProcessAppServerClient::start(InProcessClientStartArgs {
|
|
required_human_approval: true,
|
|
arg0_paths: Arg0DispatchPaths::default(),
|
|
config: Arc::new(config),
|
|
cli_overrides: Vec::new(),
|
|
loader_overrides,
|
|
strict_config: false,
|
|
embedded_network_policy: Default::default(),
|
|
feedback: codex_feedback::CodexFeedback::new(),
|
|
log_db: None,
|
|
state_db: None,
|
|
environment_manager: Arc::new(
|
|
codex_app_server_client::EnvironmentManager::default_for_tests(),
|
|
),
|
|
config_warnings: Vec::new(),
|
|
session_source: serde_json::from_value(serde_json::json!("cli"))
|
|
.expect("cli session source should deserialize"),
|
|
enable_codex_api_key_env: false,
|
|
client_name: "test".to_string(),
|
|
client_version: "test".to_string(),
|
|
experimental_api: true,
|
|
mcp_server_openai_form_elicitation: false,
|
|
opt_out_notification_methods: Vec::new(),
|
|
channel_capacity: DEFAULT_IN_PROCESS_CHANNEL_CAPACITY,
|
|
})
|
|
.await
|
|
.unwrap();
|
|
// A non-API login method forbids API keys; it can no longer select ChatGPT.
|
|
auth_config.forced_login_method = Some(ForcedLoginMethod::Chatgpt);
|
|
let widget = AuthModeWidget {
|
|
request_frame: FrameRequester::test_dummy(),
|
|
highlighted_mode: SignInOption::ApiKey,
|
|
activation_key_guard: None,
|
|
error: Arc::new(RwLock::new(None)),
|
|
sign_in_state: Arc::new(RwLock::new(SignInState::PickMode)),
|
|
login_status: LoginStatus::NotAuthenticated,
|
|
app_server_target: crate::AppServerTarget::Embedded,
|
|
app_server_request_handle: AppServerRequestHandle::InProcess(client.request_handle()),
|
|
auth_config,
|
|
gateway_setup_enabled: false,
|
|
gateway: None,
|
|
};
|
|
(widget, codex_home)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn api_key_entry_snapshots() {
|
|
let (mut widget, _tmp) = widget_api_login_forbidden().await;
|
|
widget.auth_config.forced_login_method = None;
|
|
*widget.sign_in_state.write().unwrap() =
|
|
SignInState::ApiKeyEntry(ApiKeyInputState::default());
|
|
|
|
for (name, width) in [("api_key_entry", 80), ("api_key_entry_narrow", 40)] {
|
|
let height = 14;
|
|
let area = Rect::new(/*x*/ 0, /*y*/ 0, width, height);
|
|
let mut terminal = crate::custom_terminal::Terminal::with_options(
|
|
crate::test_backend::VT100Backend::new(width, height),
|
|
)
|
|
.expect("terminal");
|
|
terminal.set_viewport_area(area);
|
|
terminal
|
|
.draw(|frame| widget.render_ref(area, frame.buffer_mut()))
|
|
.expect("draw");
|
|
|
|
insta::assert_snapshot!(name, terminal.backend().to_string());
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn api_key_flow_disabled_when_api_login_forbidden() {
|
|
let (mut widget, _tmp) = widget_api_login_forbidden().await;
|
|
|
|
widget.start_api_key_entry();
|
|
|
|
assert_eq!(
|
|
widget.error_message().as_deref(),
|
|
Some(API_KEY_DISABLED_MESSAGE)
|
|
);
|
|
assert!(matches!(
|
|
&*widget.sign_in_state.read().unwrap(),
|
|
SignInState::PickMode
|
|
));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gateway_option_requires_feature_and_api_login_permission() {
|
|
let (mut widget, codex_home) = widget_api_login_forbidden().await;
|
|
widget.auth_config.forced_login_method = None;
|
|
assert_eq!(
|
|
widget.displayed_sign_in_options(),
|
|
vec![SignInOption::ApiKey]
|
|
);
|
|
|
|
std::fs::write(
|
|
codex_home.path().join("config.toml"),
|
|
"[features]\ngateway_setup_wizard = true\n",
|
|
)
|
|
.unwrap();
|
|
let config = ConfigBuilder::default()
|
|
.codex_home(codex_home.path().to_path_buf())
|
|
.build()
|
|
.await
|
|
.unwrap();
|
|
widget.gateway_setup_enabled = crate::should_show_provider_setup_wizard(
|
|
widget.login_status,
|
|
/*requires_openai_auth*/ true,
|
|
&config,
|
|
&widget.app_server_target,
|
|
Feature::GatewaySetupWizard,
|
|
);
|
|
assert_eq!(
|
|
widget.displayed_sign_in_options(),
|
|
vec![SignInOption::ApiKey, SignInOption::Gateway]
|
|
);
|
|
widget.move_highlight(/*delta*/ 1);
|
|
|
|
let area = Rect::new(0, 0, 76, 19);
|
|
let mut buffer = Buffer::empty(area);
|
|
widget.render_pick_mode(area, &mut buffer);
|
|
let mut rows = (area.top()..area.bottom())
|
|
.map(|row| {
|
|
(area.left()..area.right())
|
|
.map(|column| buffer[(column, row)].symbol())
|
|
.collect::<String>()
|
|
.trim_end()
|
|
.to_string()
|
|
})
|
|
.collect::<Vec<_>>();
|
|
while rows.last().is_some_and(String::is_empty) {
|
|
rows.pop();
|
|
}
|
|
insta::assert_snapshot!(rows.join("\n"), @r###"
|
|
Choose how you want to use aKode.
|
|
|
|
1. Use an OpenAI API key
|
|
Pay for what you use
|
|
|
|
> 2. Use a custom provider or gateway
|
|
Choose a saved configuration or create a new one
|
|
|
|
Press enter to continue
|
|
"###);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn saving_api_key_is_blocked_when_api_login_forbidden() {
|
|
let (mut widget, _tmp) = widget_api_login_forbidden().await;
|
|
|
|
widget.save_api_key("sk-test".to_string());
|
|
|
|
assert_eq!(
|
|
widget.error_message().as_deref(),
|
|
Some(API_KEY_DISABLED_MESSAGE)
|
|
);
|
|
assert!(matches!(
|
|
&*widget.sign_in_state.read().unwrap(),
|
|
SignInState::PickMode
|
|
));
|
|
assert_eq!(widget.login_status, LoginStatus::NotAuthenticated);
|
|
}
|
|
|
|
/// Collects all buffer cell symbols that contain the OSC 8 open sequence
|
|
/// for the given URL. Returns the concatenated "inner" characters.
|
|
fn collect_osc8_chars(buf: &Buffer, area: Rect, url: &str) -> String {
|
|
let open = format!("\x1B]8;;{url}\x07");
|
|
let close = "\x1B]8;;\x07";
|
|
let mut chars = String::new();
|
|
for y in area.top()..area.bottom() {
|
|
for x in area.left()..area.right() {
|
|
let sym = buf[(x, y)].symbol();
|
|
if let Some(rest) = sym.strip_prefix(open.as_str())
|
|
&& let Some(ch) = rest.strip_suffix(close)
|
|
{
|
|
chars.push_str(ch);
|
|
}
|
|
}
|
|
}
|
|
chars
|
|
}
|
|
|
|
#[test]
|
|
fn mark_url_hyperlink_wraps_cyan_underlined_cells() {
|
|
let url = "https://example.com";
|
|
let area = Rect::new(0, 0, 20, 1);
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
// Manually write some cyan+underlined characters to simulate a rendered URL.
|
|
for (i, ch) in "example".chars().enumerate() {
|
|
let cell = &mut buf[(i as u16, 0)];
|
|
cell.set_symbol(&ch.to_string());
|
|
cell.fg = Color::Cyan;
|
|
cell.modifier = Modifier::UNDERLINED;
|
|
}
|
|
// Leave a plain cell that should NOT be marked.
|
|
buf[(7, 0)].set_symbol("X");
|
|
|
|
mark_url_hyperlink(&mut buf, area, url);
|
|
|
|
// Each cyan+underlined cell should now carry the OSC 8 wrapper.
|
|
let found = collect_osc8_chars(&buf, area, url);
|
|
assert_eq!(found, "example");
|
|
|
|
// The plain "X" cell should be untouched.
|
|
assert_eq!(buf[(7, 0)].symbol(), "X");
|
|
}
|
|
|
|
#[test]
|
|
fn mark_url_hyperlink_sanitizes_control_chars() {
|
|
let area = Rect::new(0, 0, 10, 1);
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
// One cyan+underlined cell to mark.
|
|
let cell = &mut buf[(0, 0)];
|
|
cell.set_symbol("a");
|
|
cell.fg = Color::Cyan;
|
|
cell.modifier = Modifier::UNDERLINED;
|
|
|
|
// URL contains ESC and BEL that could break the OSC 8 sequence.
|
|
let malicious_url = "https://evil.com/\x1B]8;;\x07injected";
|
|
mark_url_hyperlink(&mut buf, area, malicious_url);
|
|
|
|
let sym = buf[(0, 0)].symbol().to_string();
|
|
// The sanitized URL retains `]` (printable) but strips ESC and BEL.
|
|
let sanitized = "https://evil.com/]8;;injected";
|
|
assert!(
|
|
sym.contains(sanitized),
|
|
"symbol should contain sanitized URL, got: {sym:?}"
|
|
);
|
|
// The injected close-sequence must not survive: \x1B and \x07 are gone.
|
|
assert!(
|
|
!sym.contains("\x1B]8;;\x07injected"),
|
|
"symbol must not contain raw control chars from URL"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod activation_key_tests;
|
|
|
|
#[cfg(test)]
|
|
#[path = "gateway_tests.rs"]
|
|
mod gateway_tests;
|