clickup_v2 0.1.1

A comprehensive Rust client library and CLI for ClickUp API v2 with OAuth2 authentication, task management, and custom fields support
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
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
use std::collections::HashMap;
use std::time::Duration;
use oauth2::{
    AuthorizationCode, ClientId, ClientSecret, CsrfToken, RedirectUrl, Scope,
    AuthUrl, TokenUrl, basic::BasicClient,
    reqwest::async_http_client, TokenResponse,
};
use crate::config::EnvManager;
use crate::auth::callback::CallbackServer;
use crate::auth::token::TokenManager;
use crate::client::api::ClickUpClient;
use crate::error::{AuthError, AuthResult};

/// Gerenciador do fluxo OAuth2 do ClickUp
#[derive(Debug)]
pub struct OAuthFlow {
    env_manager: EnvManager,
    token_manager: TokenManager,
}

impl OAuthFlow {
    /// Cria uma nova instância do fluxo OAuth
    pub fn new() -> AuthResult<Self> {
        let env_manager = EnvManager::load()?;
        env_manager.validate()?;
        
        let token_manager = TokenManager::new();
        
        Ok(Self {
            env_manager,
            token_manager,
        })
    }

    /// Executa o fluxo de autenticação completo
    pub async fn authenticate(&self) -> AuthResult<String> {
        log::info!("🔑 Iniciando processo de autenticação OAuth2...");
        log::info!("📍 {}", self.env_manager.environment_info());

        // 1. Verifica se já existe um token válido
        if let Some(token) = self.token_manager.get_token() {
            log::info!("🔍 Token encontrado, validando...");
            
            if self.validate_token(&token).await? {
                log::info!("✅ Token válido! Autenticação concluída.");
                return Ok(token);
            } else {
                log::warn!("❌ Token inválido, iniciando novo fluxo OAuth2...");
                self.token_manager.remove_token()?;
            }
        } else {
            log::info!("🆕 Nenhum token encontrado, iniciando fluxo OAuth2...");
        }

        // 2. Inicia o fluxo OAuth2
        let token = self.execute_oauth_flow().await?;
        
        // 3. Valida o token obtido
        if self.validate_token(&token).await? {
            // 4. Salva o token
            self.token_manager.save_token(&token)?;
            log::info!("✅ Autenticação OAuth2 concluída com sucesso!");
            Ok(token)
        } else {
            Err(AuthError::oauth_error("Token obtido é inválido"))
        }
    }

    /// Executa o fluxo OAuth2 completo
    async fn execute_oauth_flow(&self) -> AuthResult<String> {
        match self.env_manager.environment {
            crate::config::env::Environment::Development => {
                self.execute_local_oauth_flow().await
            },
            crate::config::env::Environment::Production => {
                self.execute_production_oauth_flow().await
            }
        }
    }

    /// Executa o fluxo OAuth2 para ambiente de desenvolvimento (local)
    async fn execute_local_oauth_flow(&self) -> AuthResult<String> {
        log::info!("🏠 Executando fluxo OAuth2 para desenvolvimento local...");

        // 1. Configura o cliente OAuth2
        let (auth_url, token_url) = EnvManager::get_oauth_urls();
        let client = self.create_oauth_client(&auth_url, &token_url)?;

        // 2. Gera URL de autorização
        let (auth_url, csrf_token) = client
            .authorize_url(CsrfToken::new_random)
            .add_scope(Scope::new("read".to_string()))
            .add_scope(Scope::new("write".to_string()))
            .url();

        log::info!("🌐 URL de autorização gerada: {}", auth_url);

        // 3. Inicia o servidor callback local
        let state = csrf_token.secret().clone();
        let callback_server = CallbackServer::new(self.env_manager.callback_port, state.clone());

        // 4. Inicia o servidor e aguarda o resultado
        let server_handle = tokio::spawn(async move {
            callback_server.start_and_wait().await
        });

        // 5. Abre o navegador
        if let Err(e) = webbrowser::open(auth_url.as_str()) {
            log::warn!("⚠️ Não foi possível abrir o navegador automaticamente: {}", e);
            log::info!("🔗 Abra manualmente o link: {}", auth_url);
        } else {
            log::info!("🌐 Navegador aberto automaticamente");
        }

        log::info!("⏳ Aguardando autorização do usuário...");

        // 6. Aguarda o callback
        let callback_result = tokio::time::timeout(
            Duration::from_secs(300), // 5 minutos
            server_handle
        )
        .await
        .map_err(|_| AuthError::Timeout)?
        .map_err(|e| AuthError::generic(format!("Erro na thread do servidor: {}", e)))?
        .map_err(|e| AuthError::generic(format!("Erro no callback: {}", e)))?;

        // 7. O CSRF token já foi validado pelo CallbackServer
        let code = callback_result.code;

        log::info!("✅ Código de autorização recebido");

        // 9. Troca código por token
        self.exchange_code_for_token(client, code).await
    }

    /// Executa o fluxo OAuth2 para ambiente de produção
    async fn execute_production_oauth_flow(&self) -> AuthResult<String> {
        log::info!("☁️ Executando fluxo OAuth2 para produção...");

        // Em produção, assumimos que o token será fornecido via variável de ambiente
        // ou através de um processo externo (como secrets manager)
        
        // 1. Verifica se há um token na variável de ambiente
        if let Some(token) = std::env::var("CLICKUP_ACCESS_TOKEN").ok().filter(|t| !t.is_empty()) {
            log::info!("🔑 Token encontrado nas variáveis de ambiente");
            return Ok(token);
        }

        // 2. Para produção, você pode implementar diferentes estratégias:
        // - Integração com Google Secret Manager
        // - Endpoint para receber token via API
        // - Interface web para autorização
        
        // Por enquanto, retorna erro solicitando configuração manual
        Err(AuthError::config_error(
            "Em ambiente de produção, configure CLICKUP_ACCESS_TOKEN nas variáveis de ambiente. \
            Para obter o token, execute o fluxo OAuth2 em ambiente de desenvolvimento primeiro."
        ))
    }

    /// Cria o cliente OAuth2
    fn create_oauth_client(&self, auth_url: &str, token_url: &str) -> AuthResult<BasicClient> {
        let client_id = ClientId::new(self.env_manager.client_id.clone());
        let client_secret = ClientSecret::new(self.env_manager.client_secret.clone());
        let auth_url = AuthUrl::new(auth_url.to_string())
            .map_err(|e| AuthError::oauth_error(&format!("URL de autorização inválida: {}", e)))?;
        let token_url = TokenUrl::new(token_url.to_string())
            .map_err(|e| AuthError::oauth_error(&format!("URL de token inválida: {}", e)))?;

        let redirect_url = RedirectUrl::new(self.env_manager.get_callback_url())
            .map_err(|e| AuthError::oauth_error(&format!("URL de redirecionamento inválida: {}", e)))?;

        Ok(BasicClient::new(
            client_id,
            Some(client_secret),
            auth_url,
            Some(token_url),
        ).set_redirect_uri(redirect_url))
    }

    /// Troca o código de autorização por um token de acesso
    async fn exchange_code_for_token(&self, client: BasicClient, code: String) -> AuthResult<String> {
        log::info!("🔄 Trocando código de autorização por token...");

        let auth_code = AuthorizationCode::new(code);

        let token_response = client
            .exchange_code(auth_code)
            .request_async(async_http_client)
            .await
            .map_err(|e| AuthError::oauth_error(&format!("Falha ao trocar código por token: {}", e)))?;

        let access_token = token_response.access_token().secret().clone();
        
        log::info!("✅ Token de acesso obtido com sucesso");
        Ok(access_token)
    }

    /// Valida se o token de acesso é válido fazendo uma requisição de teste
    async fn validate_token(&self, token: &str) -> AuthResult<bool> {
        log::info!("🔍 Validando token de acesso...");

        let client = ClickUpClient::new(token.to_string(), self.env_manager.api_base_url.clone());
        
        match client.get_authorized_user().await {
            Ok(_) => {
                log::info!("✅ Token validado com sucesso");
                Ok(true)
            },
            Err(e) => {
                log::warn!("❌ Token inválido: {}", e);
                Ok(false)
            }
        }
    }

    /// Força uma nova autenticação removendo o token atual
    pub async fn force_reauth(&self) -> AuthResult<String> {
        log::info!("🔄 Forçando nova autenticação...");
        
        // Remove o token atual
        self.token_manager.remove_token()?;
        
        // Executa nova autenticação
        self.authenticate().await
    }

    /// Verifica se o usuário está autenticado
    pub async fn is_authenticated(&self) -> bool {
        if let Some(token) = self.token_manager.get_token() {
            self.validate_token(&token).await.unwrap_or(false)
        } else {
            false
        }
    }

    /// Obtém informações do usuário autenticado
    pub async fn get_user_info(&self) -> AuthResult<serde_json::Value> {
        let token = self.authenticate().await?;
        let client = ClickUpClient::new(token, self.env_manager.api_base_url.clone());
        client.get_authorized_user().await
    }

    /// Obtém as equipes autorizadas
    pub async fn get_authorized_teams(&self) -> AuthResult<serde_json::Value> {
        let token = self.authenticate().await?;
        let client = ClickUpClient::new(token, self.env_manager.api_base_url.clone());
        client.get_authorized_teams().await
    }

    /// Revoga o token de acesso
    pub async fn revoke_token(&self) -> AuthResult<()> {
        if let Some(_token) = self.token_manager.get_token() {
            log::info!("🗑️ Revogando token de acesso...");

            // O ClickUp não possui endpoint público de revogação
            // Apenas removemos o token localmente
            self.token_manager.remove_token()?;

            log::info!("✅ Token removido localmente");
        }

        Ok(())
    }

    /// Obtém o token atual sem validação
    pub fn get_current_token(&self) -> Option<String> {
        self.token_manager.get_token()
    }

    /// Define um token manualmente (útil para testes)
    pub fn set_token(&self, token: &str) -> AuthResult<()> {
        self.token_manager.save_token(token)
    }

    /// Retorna informações sobre a configuração atual
    pub fn get_config_info(&self) -> HashMap<String, String> {
        let mut info = HashMap::new();
        
        info.insert("environment".to_string(), self.env_manager.environment_info());
        info.insert("api_base_url".to_string(), self.env_manager.api_base_url.clone());
        info.insert("callback_url".to_string(), self.env_manager.get_callback_url());
        info.insert("has_token".to_string(), self.token_manager.get_token().is_some().to_string());
        
        let details = self.env_manager.get_environment_details();
        info.insert("environment_details".to_string(), details);
        
        info
    }
}

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

    #[tokio::test]
    async fn test_oauth_flow_creation() {
        temp_env::with_vars(
            vec![
                ("CLICKUP_CLIENT_ID", Some("test_client_id")),
                ("CLICKUP_CLIENT_SECRET", Some("test_client_secret")),
            ],
            || {
                let result = OAuthFlow::new();
                assert!(result.is_ok());

                let oauth = result.unwrap();
                assert_eq!(oauth.env_manager.client_id, "test_client_id");
                assert_eq!(oauth.env_manager.client_secret, "test_client_secret");
            },
        );
    }

    #[tokio::test]
    async fn test_oauth_flow_creation_missing_env() {
        temp_env::with_vars_unset(vec!["CLICKUP_CLIENT_ID", "CLICKUP_CLIENT_SECRET"], || {
            let result = OAuthFlow::new();
            assert!(result.is_err());
        });
    }

    #[test]
    fn test_config_info() {
        temp_env::with_vars(
            vec![
                ("CLICKUP_CLIENT_ID", Some("test_client_id")),
                ("CLICKUP_CLIENT_SECRET", Some("test_client_secret")),
            ],
            || {
                let oauth = OAuthFlow::new().unwrap();
                let info = oauth.get_config_info();

                assert!(info.contains_key("environment"));
                assert!(info.contains_key("has_token"));
                assert!(info.contains_key("api_base_url"));
                assert!(info.contains_key("callback_url"));
                assert_eq!(info.get("has_token").unwrap(), "false");
            },
        );
    }

    #[tokio::test]
    async fn test_is_authenticated_without_token() {
        // Set env vars directly for async test
        std::env::set_var("CLICKUP_CLIENT_ID", "test_client_id");
        std::env::set_var("CLICKUP_CLIENT_SECRET", "test_client_secret");
        std::env::remove_var("CLICKUP_ACCESS_TOKEN");

        let oauth = OAuthFlow::new().unwrap();
        assert!(!oauth.is_authenticated().await);

        // Cleanup
        std::env::remove_var("CLICKUP_CLIENT_ID");
        std::env::remove_var("CLICKUP_CLIENT_SECRET");
    }

    #[test]
    fn test_get_current_token_none() {
        temp_env::with_vars(
            vec![
                ("CLICKUP_CLIENT_ID", Some("test_client_id")),
                ("CLICKUP_CLIENT_SECRET", Some("test_client_secret")),
                ("CLICKUP_ACCESS_TOKEN", None),
            ],
            || {
                let oauth = OAuthFlow::new().unwrap();
                assert!(oauth.get_current_token().is_none());
            },
        );
    }

    #[test]
    fn test_set_and_get_token() {
        temp_env::with_vars(
            vec![
                ("CLICKUP_CLIENT_ID", Some("test_client_id")),
                ("CLICKUP_CLIENT_SECRET", Some("test_client_secret")),
            ],
            || {
                let oauth = OAuthFlow::new().unwrap();

                // Define um token
                let test_token = "test_access_token_12345";
                let result = oauth.set_token(test_token);
                assert!(result.is_ok());

                // Verifica se o token foi salvo
                let current_token = oauth.get_current_token();
                assert!(current_token.is_some());
                assert_eq!(current_token.unwrap(), test_token);
            },
        );
    }

    #[tokio::test]
    async fn test_revoke_token() {
        // Set env vars directly for async test
        std::env::set_var("CLICKUP_CLIENT_ID", "test_client_id");
        std::env::set_var("CLICKUP_CLIENT_SECRET", "test_client_secret");

        let oauth = OAuthFlow::new().unwrap();

        // Define um token
        oauth.set_token("test_token_to_revoke").unwrap();
        assert!(oauth.get_current_token().is_some());

        // Revoga o token
        let result = oauth.revoke_token().await;
        assert!(result.is_ok());

        // Verifica se o token foi removido
        assert!(oauth.get_current_token().is_none());

        // Cleanup
        std::env::remove_var("CLICKUP_CLIENT_ID");
        std::env::remove_var("CLICKUP_CLIENT_SECRET");
        std::env::remove_var("CLICKUP_ACCESS_TOKEN");
    }

    #[test]
    fn test_create_oauth_client() {
        temp_env::with_vars(
            vec![
                ("CLICKUP_CLIENT_ID", Some("test_client_id")),
                ("CLICKUP_CLIENT_SECRET", Some("test_client_secret")),
            ],
            || {
                let oauth = OAuthFlow::new().unwrap();
                let (auth_url, token_url) = EnvManager::get_oauth_urls();

                let client = oauth.create_oauth_client(&auth_url, &token_url);
                assert!(client.is_ok());
            },
        );
    }

    #[test]
    fn test_create_oauth_client_invalid_urls() {
        temp_env::with_vars(
            vec![
                ("CLICKUP_CLIENT_ID", Some("test_client_id")),
                ("CLICKUP_CLIENT_SECRET", Some("test_client_secret")),
            ],
            || {
                let oauth = OAuthFlow::new().unwrap();

                // Testa com URL inválida
                let client = oauth.create_oauth_client("not-a-url", "also-not-a-url");
                assert!(client.is_err());
            },
        );
    }

    #[tokio::test]
    async fn test_production_oauth_flow_with_env_token() {
        // Set env vars directly for async test
        std::env::set_var("CLICKUP_CLIENT_ID", "test_client_id");
        std::env::set_var("CLICKUP_CLIENT_SECRET", "test_client_secret");
        std::env::set_var("PRODUCTION", "true");
        std::env::set_var("CLICKUP_ACCESS_TOKEN", "production_token_123");

        let oauth = OAuthFlow::new().unwrap();
        let result = oauth.execute_production_oauth_flow().await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "production_token_123");

        // Cleanup
        std::env::remove_var("CLICKUP_CLIENT_ID");
        std::env::remove_var("CLICKUP_CLIENT_SECRET");
        std::env::remove_var("PRODUCTION");
        std::env::remove_var("CLICKUP_ACCESS_TOKEN");
    }

    #[tokio::test]
    async fn test_production_oauth_flow_without_token() {
        // Set env vars directly for async test
        std::env::set_var("CLICKUP_CLIENT_ID", "test_client_id");
        std::env::set_var("CLICKUP_CLIENT_SECRET", "test_client_secret");
        std::env::set_var("PRODUCTION", "true");
        std::env::remove_var("CLICKUP_ACCESS_TOKEN");

        let oauth = OAuthFlow::new().unwrap();
        let result = oauth.execute_production_oauth_flow().await;

        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.to_string().contains("configure CLICKUP_ACCESS_TOKEN"));

        // Cleanup
        std::env::remove_var("CLICKUP_CLIENT_ID");
        std::env::remove_var("CLICKUP_CLIENT_SECRET");
        std::env::remove_var("PRODUCTION");
    }
}