Files
optimclaw/tools-src/telegram/src/auth.rs
T
a35db4d32d feat: Add Google Suite & Telegram WASM tools (#9)
* Add Google Calendar and Gmail WASM tools, and /add-tool skill

Scaffold two new WASM tools that share a single Google OAuth token:
- google-calendar: list/get/create/update/delete calendar events
- gmail: list/search/get/send/draft/reply/trash emails

Both tools use the sandboxed WIT interface with strict HTTP allowlists,
credential injection, and rate limiting. OAuth config requests only
the minimum scopes needed (calendar.events, gmail.modify, gmail.compose).

Also adds the /add-tool skill for scaffolding future WASM or built-in
tools with all boilerplate wired up.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Document WASM vs MCP server decision guide in CLAUDE.md

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Add Google Drive WASM tool with full file and sharing management

Supports 12 actions: list/get/download/upload/update files, create
folders, delete/trash, share/list/remove permissions, and list shared
drives. Works with both personal and organizational drives via the
corpora parameter. Uses shared google_oauth_token for auth.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Add Google Sheets, Docs, and Slides WASM tools

Three new Google Workspace tools sharing google_oauth_token:
- Sheets: create spreadsheets, read/write/append values, manage sheets, format cells
- Docs: create/read/edit documents, text formatting, paragraphs, tables, lists
- Slides: create/edit presentations, shapes, images, text formatting, thumbnails, templates

Also adds tools-src/TOOLS.md tracking implementation status.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Add Telegram WASM tool with direct MTProto over HTTPS

Replace TDLight Docker dependency with pure-Rust grammers crates
for direct encrypted MTProto communication to Telegram's web
transport endpoints. No middleware, no Docker needed.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Gitignore Cargo.lock files in WASM tools

Library crates should not commit lock files. Consolidate per-tool
.gitignore into a single one at wasm-tools/ level.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Flatten tools-src/wasm-tools/ into tools-src/

All tools are WASM, the extra nesting added no value. Moves all tool
crates up one level, updates WIT paths and documentation references.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix Slack tool: add OAuth auth, URL encoding, pin wit-bindgen

- Add OAuth 2.0 auth section to Slack capabilities with proper scopes
  and manual fallback instructions
- URL-encode query parameters in GET requests to prevent injection
- Remove dead SlackApiError struct
- Pin wit-bindgen to =0.36 across all WASM tools for Rust 1.86 compat
- Update add-tool template with pinned version

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-02-09 06:16:27 +00:00

54 lines
2.1 KiB
Rust

use grammers_mtproto::authentication;
use grammers_tl_types::{self as tl, Deserializable};
use crate::session::Session;
use crate::transport;
/// Perform the full DH auth key exchange with a Telegram DC.
///
/// This drives the Sans-IO `grammers_mtproto::authentication` module over
/// HTTP transport. Four round trips:
///
/// 1. step1 -> ReqPqMulti -> server returns ResPq
/// 2. step2 -> ReqDhParams -> server returns ServerDhParams
/// 3. step3 -> SetClientDhParams -> server returns DhGen answer
/// 4. create_key -> produces auth_key, salt, time_offset
pub fn generate_auth_key(session: &mut Session) -> Result<(), String> {
let dc_id = session.dc_id;
// Step 1: generate nonce, send ReqPqMulti
let (request, step1_data) =
authentication::step1().map_err(|e| format!("auth step1 failed: {e}"))?;
let response_bytes = transport::post_plain(dc_id, &request)?;
let res_pq = tl::enums::ResPq::from_bytes(&response_bytes)
.map_err(|e| format!("failed to parse ResPq: {e}"))?;
// Step 2: factorize PQ, RSA encrypt, send ReqDhParams
let (request, step2_data) =
authentication::step2(step1_data, res_pq).map_err(|e| format!("auth step2 failed: {e}"))?;
let response_bytes = transport::post_plain(dc_id, &request)?;
let server_dh = tl::enums::ServerDhParams::from_bytes(&response_bytes)
.map_err(|e| format!("failed to parse ServerDhParams: {e}"))?;
// Step 3: compute DH g_b, send SetClientDhParams
let (request, step3_data) = authentication::step3(step2_data, server_dh)
.map_err(|e| format!("auth step3 failed: {e}"))?;
let response_bytes = transport::post_plain(dc_id, &request)?;
let dh_answer = tl::enums::SetClientDhParamsAnswer::from_bytes(&response_bytes)
.map_err(|e| format!("failed to parse DhGenAnswer: {e}"))?;
// Final: derive auth key from shared secret
let finished = authentication::create_key(step3_data, dh_answer)
.map_err(|e| format!("auth create_key failed: {e}"))?;
session.set_auth_key(&finished.auth_key);
session.first_salt = finished.first_salt;
session.time_offset = finished.time_offset;
session.initialized = true;
Ok(())
}