octomind 0.23.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
Documentation
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
// Copyright 2026 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! OAuth 2.1 + PKCE configuration for MCP servers
//!
//! This module provides strict OAuth configuration for MCP server authentication.
//! All fields are required and validated at config load time.

use serde::{Deserialize, Serialize};
use url::Url;

/// OAuth 2.1 + PKCE configuration for MCP servers.
///
/// This configuration is used for OAuth 2.1 authentication with PKCE (Proof Key for Code Exchange),
/// which is the recommended authentication method for MCP servers per the MCP specification.
///
/// ## Required Fields
/// - `client_id`: The OAuth client ID issued by the authorization server
/// - `client_secret`: The OAuth client secret (for confidential clients)
/// - `authorization_url`: The authorization endpoint URL
/// - `token_url`: The token endpoint URL
/// - `callback_url`: The authorization callback URL registered with the OAuth provider
/// - `scopes`: List of OAuth scopes to request
///
/// ## Example Configuration
///
/// ```toml
/// [mcp.servers.github.oauth]
/// client_id = "your-github-oauth-app-client-id"
/// client_secret = "your-github-oauth-app-client-secret"
/// authorization_url = "https://github.com/login/oauth/authorize"
/// token_url = "https://github.com/login/oauth/access_token"
/// callback_url = "http://localhost:34567/oauth/callback"
/// scopes = ["repo", "read:org", "workflow"]
/// ```
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct OAuthConfig {
	/// The OAuth client ID.
	/// This is issued by the authorization server when you register your application.
	///
	/// Required: Yes
	/// Validation: Must not be empty
	pub client_id: String,

	/// The OAuth client secret.
	/// Used for confidential clients to authenticate with the token endpoint.
	///
	/// Required: Yes (for confidential client OAuth flows)
	/// Validation: Must not be empty
	pub client_secret: String,

	/// The OAuth authorization endpoint URL.
	/// Users will be redirected to this URL to authorize access.
	///
	/// Required: Yes
	/// Validation: Must be a valid HTTPS URL (or http://localhost for development)
	pub authorization_url: String,

	/// The OAuth token endpoint URL.
	/// Used to exchange authorization codes for tokens and to refresh tokens.
	///
	/// Required: Yes
	/// Validation: Must be a valid HTTPS URL (or http://localhost for development)
	pub token_url: String,

	/// The OAuth authorization callback URL.
	/// This must match exactly with the callback URL registered with your OAuth provider.
	/// For local development, use http://localhost with the port your callback server uses.
	///
	/// Required: Yes
	/// Validation: Must be a valid URL (http://localhost or https:// for production)
	pub callback_url: String,

	/// List of OAuth scopes to request from the user.
	/// Scopes determine what access the application requests.
	///
	/// Required: Yes
	/// Validation: Must not be empty
	#[serde(default)]
	pub scopes: Vec<String>,

	/// Optional: State parameter for CSRF protection.
	/// If not provided, a random state will be generated during OAuth flow.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub state: Option<String>,

	/// Optional: Token refresh buffer in seconds before expiry.
	/// Tokens will be refreshed when they are within this time of expiring.
	/// Default: 300 seconds (5 minutes)
	#[serde(default = "default_refresh_buffer")]
	pub refresh_buffer_seconds: u64,
}

/// Default refresh buffer: 5 minutes before token expiry
fn default_refresh_buffer() -> u64 {
	300
}

impl OAuthConfig {
	/// Creates a new OAuthConfig with all required fields.
	///
	/// # Arguments
	///
	/// * `client_id` - The OAuth client ID
	/// * `client_secret` - The OAuth client secret
	/// * `authorization_url` - The authorization endpoint
	/// * `token_url` - The token endpoint
	/// * `callback_url` - The callback URL registered with OAuth provider
	/// * `scopes` - List of requested scopes
	///
	/// # Returns
	///
	/// A new OAuthConfig with default values for optional fields.
	pub fn new(
		client_id: String,
		client_secret: String,
		authorization_url: String,
		token_url: String,
		callback_url: String,
		scopes: Vec<String>,
	) -> Self {
		Self {
			client_id,
			client_secret,
			authorization_url,
			token_url,
			callback_url,
			scopes,
			state: None,
			refresh_buffer_seconds: default_refresh_buffer(),
		}
	}

	/// Validates the OAuth configuration.
	///
	/// Returns `Err` with a descriptive error message if validation fails.
	///
	/// # Validation Rules
	///
	/// - `client_id` must not be empty
	/// - `client_secret` can be empty for public clients (PKCE flow)
	/// - `authorization_url` must be a valid URL
	/// - `token_url` must be a valid URL
	/// - `redirect_uri` must be a valid URL
	/// - `scopes` can be empty (some providers don't require scopes)
	///
	/// # Returns
	///
	/// `Ok(())` if valid, or `Err(String)` with error message.
	pub fn validate(&self) -> Result<(), String> {
		// Validate client_id
		if self.client_id.trim().is_empty() {
			return Err("OAuth client_id cannot be empty".to_string());
		}

		// client_secret can be empty for public clients using PKCE
		// No validation needed for client_secret

		// Validate authorization_url
		let auth_url = Url::parse(&self.authorization_url).map_err(|e| {
			format!(
				"OAuth authorization_url is invalid: {}. Must be a valid URL (e.g., https://example.com/oauth/authorize)",
				e
			)
		})?;

		if auth_url.scheme() != "https"
			&& auth_url.host_str() != Some("localhost")
			&& auth_url.host_str() != Some("127.0.0.1")
		{
			return Err(
				"OAuth authorization_url must use HTTPS or http://localhost/http://127.0.0.1 for development"
					.to_string(),
			);
		}

		// Validate token_url
		let token_url = Url::parse(&self.token_url).map_err(|e| {
			format!(
				"OAuth token_url is invalid: {}. Must be a valid URL (e.g., https://example.com/oauth/token)",
				e
			)
		})?;

		if token_url.scheme() != "https"
			&& token_url.host_str() != Some("localhost")
			&& token_url.host_str() != Some("127.0.0.1")
		{
			return Err(
				"OAuth token_url must use HTTPS or http://localhost/http://127.0.0.1 for development"
					.to_string(),
			);
		}

		// Validate callback_url
		let callback_url = Url::parse(&self.callback_url).map_err(|e| {
			format!(
				"OAuth callback_url is invalid: {}. Must be a valid URL (e.g., http://localhost:34567/oauth/callback)",
				e
			)
		})?;

		if callback_url.scheme() != "http" && callback_url.scheme() != "https" {
			return Err("OAuth callback_url must use HTTP or HTTPS".to_string());
		}

		// Validate scopes (can be empty for some providers)
		// Just validate that scopes don't contain empty strings
		for scope in &self.scopes {
			if scope.trim().is_empty() {
				return Err("OAuth scopes cannot contain empty strings".to_string());
			}
		}

		// Validate refresh_buffer_seconds
		if self.refresh_buffer_seconds < 60 {
			return Err("OAuth refresh_buffer_seconds must be at least 60 seconds".to_string());
		}

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_oauth_config_validation_empty_client_id() {
		let config = OAuthConfig::new(
			String::new(), // empty client_id
			"secret".to_string(),
			"https://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);

		assert!(config.validate().is_err());
		assert_eq!(
			config.validate().unwrap_err(),
			"OAuth client_id cannot be empty"
		);
	}

	#[test]
	fn test_oauth_config_validation_empty_client_secret() {
		// Empty client_secret is valid for public OAuth clients (PKCE flow)
		let config = OAuthConfig::new(
			"client_id".to_string(),
			String::new(), // empty client_secret - valid for public clients
			"https://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);

		// Public clients with PKCE don't require a client_secret
		assert!(config.validate().is_ok());
	}

	#[test]
	fn test_oauth_config_validation_empty_scopes() {
		// Empty scopes are valid - some OAuth providers don't require specific scopes
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"https://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec![], // empty scopes - valid for some providers
		);

		// Empty scopes are now valid (just can't contain empty strings)
		assert!(config.validate().is_ok());
	}

	#[test]
	fn test_oauth_config_validation_invalid_authorization_url() {
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"not-a-valid-url".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);

		assert!(config.validate().is_err());
		assert!(config
			.validate()
			.unwrap_err()
			.contains("authorization_url is invalid"));
	}

	#[test]
	fn test_oauth_config_validation_http_authorization_url_not_localhost() {
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"http://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);

		assert!(config.validate().is_err());
		assert!(config
			.validate()
			.unwrap_err()
			.contains("authorization_url must use HTTPS"));
	}

	#[test]
	fn test_oauth_config_validation_valid_config() {
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"https://github.com/login/oauth/authorize".to_string(),
			"https://github.com/login/oauth/access_token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string(), "read:org".to_string()],
		);

		assert!(config.validate().is_ok());
	}

	#[test]
	fn test_oauth_config_validation_localhost_allowed() {
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"http://localhost:8080/oauth/authorize".to_string(),
			"http://localhost:8080/oauth/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);

		assert!(config.validate().is_ok());
	}

	#[test]
	fn test_oauth_config_refresh_buffer_default() {
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"https://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);

		assert_eq!(config.refresh_buffer_seconds, 300);
	}

	#[test]
	fn test_oauth_config_refresh_buffer_minimum() {
		let mut config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"https://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"http://localhost:34567/oauth/callback".to_string(),
			vec!["repo".to_string()],
		);
		config.refresh_buffer_seconds = 30; // less than minimum

		assert!(config.validate().is_err());
		assert!(config
			.validate()
			.unwrap_err()
			.contains("refresh_buffer_seconds must be at least 60"));
	}

	#[test]
	fn test_oauth_config_validation_invalid_callback_url() {
		let config = OAuthConfig::new(
			"client_id".to_string(),
			"secret".to_string(),
			"https://example.com/authorize".to_string(),
			"https://example.com/token".to_string(),
			"not-a-valid-url".to_string(),
			vec!["repo".to_string()],
		);

		assert!(config.validate().is_err());
		assert!(config
			.validate()
			.unwrap_err()
			.contains("callback_url is invalid"));
	}
}