feat: add lesson export and import functionality

- Implemented `LessonExport` and `LessonImport` actions in the action module.
- Added corresponding command parsing for lesson export and import.
- Created functions to handle lesson export and import in the memory module.
- Updated state management to reflect changes after lesson operations.
- Introduced deferred operations for handling asynchronous tasks in the event loop.
- Enhanced the tool execution context to include graduated checks for file operations.
- Added OAuth support with PKCE for secure authorization flows.
- Implemented a loopback server for handling OAuth redirects.
- Refactored various modules to improve code organization and maintainability.
This commit is contained in:
asepharyana
2026-07-11 18:23:01 +07:00
parent cc03bd79b6
commit c1ad206a00
49 changed files with 1088 additions and 12 deletions
+1
View File
@@ -1 +1,2 @@
pub mod openrouter;
pub mod oauth;
+72
View File
@@ -0,0 +1,72 @@
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
pub struct LoopbackServer {
listener: TcpListener,
port: u16,
}
impl LoopbackServer {
pub fn bind() -> std::io::Result<Self> {
let listener = TcpListener::bind("127.0.0.1:0")?;
let port = listener.local_addr()?.port();
Ok(LoopbackServer { listener, port })
}
pub fn port(&self) -> u16 {
self.port
}
pub fn redirect_uri(&self) -> String {
format!("http://127.0.0.1:{}/callback", self.port)
}
pub fn wait_for_code(&self, timeout_ms: u64) -> std::io::Result<String> {
let (mut stream, _) = self.listener.accept()?;
stream.set_read_timeout(Some(std::time::Duration::from_millis(timeout_ms)))?;
Self::read_callback(&mut stream)
}
fn read_callback(stream: &mut TcpStream) -> std::io::Result<String> {
let mut buf = [0u8; 4096];
let n = stream.read(&mut buf)?;
let request = String::from_utf8_lossy(&buf[..n]);
let code = Self::extract_code(&request);
let response = if code.is_some() {
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nAuthorization complete. You may close this tab."
} else {
"HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\nMissing authorization code."
};
let _ = stream.write_all(response.as_bytes());
let _ = stream.flush();
code.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData, "code not found in callback"))
}
fn extract_code(request: &str) -> Option<String> {
let line = request.lines().next()?;
let path = line.split(' ').nth(1)?;
let query = path.split('?').nth(1)?;
for pair in query.split('&') {
let mut parts = pair.splitn(2, '=');
if parts.next()? == "code" {
return parts.next().map(urlencoding);
}
}
None
}
}
fn urlencoding(s: &str) -> String {
let mut result = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '%' {
let hi = chars.next().and_then(|c| c.to_digit(16)).unwrap_or(0);
let lo = chars.next().and_then(|c| c.to_digit(16)).unwrap_or(0);
result.push(char::from((hi * 16 + lo) as u8));
} else {
result.push(c);
}
}
result
}
+153
View File
@@ -0,0 +1,153 @@
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuthToken {
pub access_token: String,
pub refresh_token: Option<String>,
pub expires_at: u64,
pub token_type: String,
}
impl OAuthToken {
pub fn is_expired(&self) -> bool {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
now >= self.expires_at
}
pub fn remaining_secs(&self) -> i64 {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
self.expires_at as i64 - now as i64
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuthConfig {
pub auth_url: String,
pub token_url: String,
pub client_id: String,
pub client_secret: Option<String>,
pub scopes: Vec<String>,
}
impl Default for OAuthConfig {
fn default() -> Self {
OAuthConfig {
auth_url: String::new(),
token_url: String::new(),
client_id: String::new(),
client_secret: None,
scopes: vec!["openid".to_string(), "profile".to_string(), "email".to_string()],
}
}
}
pub struct OAuthManager {
pub config: OAuthConfig,
pub token: Option<OAuthToken>,
client: reqwest::blocking::Client,
}
impl OAuthManager {
pub fn new(config: OAuthConfig) -> Self {
OAuthManager {
config,
token: None,
client: reqwest::blocking::Client::new(),
}
}
pub fn exchange_code(&mut self, code: &str, redirect_uri: &str, code_verifier: &str) -> Result<(), String> {
let mut params = std::collections::HashMap::new();
params.insert("grant_type", "authorization_code");
params.insert("code", code);
params.insert("redirect_uri", redirect_uri);
params.insert("client_id", &self.config.client_id);
params.insert("code_verifier", code_verifier);
let resp = self.client
.post(&self.config.token_url)
.form(&params)
.send()
.map_err(|e| format!("token request failed: {}", e))?;
let status = resp.status();
let body: serde_json::Value = resp.json().map_err(|e| format!("parse failed: {}", e))?;
if !status.is_success() {
return Err(format!("token endpoint returned {}: {}", status, body));
}
let access_token = body["access_token"].as_str().ok_or("missing access_token")?.to_string();
let expires_in = body["expires_in"].as_u64().unwrap_or(3600);
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
self.token = Some(OAuthToken {
access_token,
refresh_token: body["refresh_token"].as_str().map(|s| s.to_string()),
expires_at: now + expires_in,
token_type: body["token_type"].as_str().unwrap_or("Bearer").to_string(),
});
Ok(())
}
pub fn refresh_token(&mut self) -> Result<(), String> {
let refresh_token = self.token.as_ref()
.and_then(|t| t.refresh_token.clone())
.ok_or("no refresh token available")?;
let mut params = std::collections::HashMap::new();
params.insert("grant_type", "refresh_token");
params.insert("refresh_token", &refresh_token);
params.insert("client_id", &self.config.client_id);
let resp = self.client
.post(&self.config.token_url)
.form(&params)
.send()
.map_err(|e| format!("refresh failed: {}", e))?;
let status = resp.status();
let body: serde_json::Value = resp.json().map_err(|e| format!("parse failed: {}", e))?;
if !status.is_success() {
return Err(format!("refresh endpoint returned {}: {}", status, body));
}
let access_token = body["access_token"].as_str().ok_or("missing access_token")?.to_string();
let expires_in = body["expires_in"].as_u64().unwrap_or(3600);
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
self.token = Some(OAuthToken {
access_token,
refresh_token: body["refresh_token"].as_str().map(|s| s.to_string()).or(self.token.as_ref().and_then(|t| t.refresh_token.clone())),
expires_at: now + expires_in,
token_type: body["token_type"].as_str().unwrap_or("Bearer").to_string(),
});
Ok(())
}
pub fn ensure_token(&mut self) -> Result<(), String> {
if let Some(ref token) = self.token {
if token.remaining_secs() < 60 {
return self.refresh_token();
}
}
Ok(())
}
pub fn build_auth_url(&self, redirect_uri: &str, state: &str, code_challenge: &str) -> String {
let mut url = url::Url::parse(&self.config.auth_url).unwrap_or_else(|_| url::Url::parse("https://example.com").unwrap());
url.query_pairs_mut()
.append_pair("response_type", "code")
.append_pair("client_id", &self.config.client_id)
.append_pair("redirect_uri", redirect_uri)
.append_pair("scope", &self.config.scopes.join(" "))
.append_pair("state", state)
.append_pair("code_challenge_method", "S256")
.append_pair("code_challenge", code_challenge);
url.to_string()
}
}
+10
View File
@@ -0,0 +1,10 @@
#[expect(dead_code)]
pub mod pkce;
#[expect(dead_code)]
pub mod loopback;
#[expect(dead_code)]
pub mod manager;
pub use manager::{OAuthManager, OAuthConfig};
pub use pkce::CodeVerifier;
pub use loopback::LoopbackServer;
+41
View File
@@ -0,0 +1,41 @@
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use sha2::{Sha256, Digest};
const VERIFIER_LENGTH: usize = 64;
pub struct CodeVerifier(String);
impl CodeVerifier {
pub fn new() -> Self {
let bytes: Vec<u8> = (0..VERIFIER_LENGTH).map(|_| rand_byte()).collect();
CodeVerifier(URL_SAFE_NO_PAD.encode(&bytes))
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn challenge(&self) -> CodeChallenge {
let mut hasher = Sha256::new();
hasher.update(self.0.as_bytes());
let digest = hasher.finalize();
CodeChallenge(URL_SAFE_NO_PAD.encode(digest))
}
}
fn rand_byte() -> u8 {
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.subsec_nanos();
(nanos & 0xFF) as u8
}
pub struct CodeChallenge(String);
impl CodeChallenge {
pub fn as_str(&self) -> &str {
&self.0
}
}