leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly 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
497
//! Authentication providers and implementations
//!
//! This module provides authentication providers including OAuth2, SAML,
//! and other authentication mechanisms.

use super::errors::SecurityError;
use super::types::*;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Core authentication provider trait
#[async_trait]
pub trait AuthProvider: Send + Sync {
    /// Authenticate user with credentials
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> Result<AuthenticationResult, SecurityError>;

    /// Validate existing token
    async fn validate_token(&self, token: &str) -> Result<User, SecurityError>;

    /// Refresh access token
    async fn refresh_token(
        &self,
        refresh_token: &str,
    ) -> Result<AuthenticationResult, SecurityError>;

    /// Logout user
    async fn logout(&self, token: &str) -> Result<(), SecurityError>;

    /// Get provider type identifier
    fn provider_type(&self) -> &'static str;
}

/// OAuth 2.0 authentication provider
pub struct OAuth2Provider {
    client_id: String,
    client_secret: String,
    authorization_url: String,
    token_url: String,
    userinfo_url: String,
    redirect_uri: String,
    scope: Vec<String>,
    token_cache: Arc<RwLock<HashMap<String, (User, u64)>>>, // token -> (user, expires_at)
}

impl OAuth2Provider {
    pub fn new(
        client_id: String,
        client_secret: String,
        authorization_url: String,
        token_url: String,
        userinfo_url: String,
        redirect_uri: String,
    ) -> Self {
        Self {
            client_id,
            client_secret,
            authorization_url,
            token_url,
            userinfo_url,
            redirect_uri,
            scope: vec![
                "openid".to_string(),
                "profile".to_string(),
                "email".to_string(),
            ],
            token_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn get_authorization_url(&self, state: &str) -> String {
        format!(
            "{}?client_id={}&response_type=code&redirect_uri={}&scope={}&state={}",
            self.authorization_url,
            self.client_id,
            urlencoding::encode(&self.redirect_uri),
            self.scope.join(" "),
            state
        )
    }

    pub async fn exchange_code_for_token(
        &self,
        code: &str,
    ) -> Result<OAuth2TokenResponse, SecurityError> {
        // Mock implementation - in real scenario, make HTTP request
        Ok(OAuth2TokenResponse {
            access_token: format!("access_token_{}", code),
            token_type: "Bearer".to_string(),
            expires_in: 3600,
            refresh_token: Some(format!("refresh_token_{}", code)),
            scope: Some(self.scope.join(" ")),
        })
    }

    pub async fn get_user_info(&self, _access_token: &str) -> Result<OAuth2UserInfo, SecurityError> {
        // Mock implementation - in real scenario, make HTTP request
        Ok(OAuth2UserInfo {
            sub: "user123".to_string(),
            name: "Test User".to_string(),
            email: "test@example.com".to_string(),
            email_verified: true,
            picture: Some("https://example.com/avatar.jpg".to_string()),
        })
    }

    pub async fn refresh_access_token(
        &self,
        refresh_token: &str,
    ) -> Result<OAuth2TokenResponse, SecurityError> {
        // Mock implementation - in real scenario, make HTTP request
        Ok(OAuth2TokenResponse {
            access_token: format!("new_access_token_{}", refresh_token),
            token_type: "Bearer".to_string(),
            expires_in: 3600,
            refresh_token: Some(format!("new_refresh_token_{}", refresh_token)),
            scope: Some(self.scope.join(" ")),
        })
    }
}

#[async_trait]
impl AuthProvider for OAuth2Provider {
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> Result<AuthenticationResult, SecurityError> {
        match &credentials.credential_type {
            CredentialType::OAuth2 => {
                // For OAuth2, we expect an authorization code
                if let Some(code) = credentials.get_data("code") {
                    let token_response = self.exchange_code_for_token(code).await?;
                    let user_info = self.get_user_info(&token_response.access_token).await?;

                    let user = User::new(
                        user_info.sub,
                        user_info.name.clone(),
                        user_info.email,
                        user_info.name,
                    );

                    let expires_at = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_secs()
                        + token_response.expires_in as u64;

                    // Cache the token
                    {
                        let mut cache = self.token_cache.write().await;
                        cache.insert(
                            token_response.access_token.clone(),
                            (user.clone(), expires_at),
                        );
                    }

                    Ok(AuthenticationResult::success(
                        user,
                        token_response.access_token,
                        expires_at,
                    ))
                } else {
                    Err(SecurityError::authentication_failed(
                        "Missing authorization code",
                    ))
                }
            }
            _ => Err(SecurityError::authentication_failed(
                "Invalid credential type for OAuth2",
            )),
        }
    }

    async fn validate_token(&self, token: &str) -> Result<User, SecurityError> {
        let cache = self.token_cache.read().await;
        if let Some((user, expires_at)) = cache.get(token) {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();

            if *expires_at > now {
                Ok(user.clone())
            } else {
                Err(SecurityError::token_error("Token expired"))
            }
        } else {
            Err(SecurityError::token_error("Invalid token"))
        }
    }

    async fn refresh_token(
        &self,
        refresh_token: &str,
    ) -> Result<AuthenticationResult, SecurityError> {
        let token_response = self.refresh_access_token(refresh_token).await?;
        let user_info = self.get_user_info(&token_response.access_token).await?;

        let user = User::new(
            user_info.sub,
            user_info.name.clone(),
            user_info.email,
            user_info.name,
        );

        let expires_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + token_response.expires_in as u64;

        // Cache the new token
        {
            let mut cache = self.token_cache.write().await;
            cache.insert(
                token_response.access_token.clone(),
                (user.clone(), expires_at),
            );
        }

        Ok(AuthenticationResult::success(
            user,
            token_response.access_token,
            expires_at,
        ))
    }

    async fn logout(&self, token: &str) -> Result<(), SecurityError> {
        let mut cache = self.token_cache.write().await;
        cache.remove(token);
        Ok(())
    }

    fn provider_type(&self) -> &'static str {
        "oauth2"
    }
}

/// OAuth2 token response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuth2TokenResponse {
    pub access_token: String,
    pub token_type: String,
    pub expires_in: u32,
    pub refresh_token: Option<String>,
    pub scope: Option<String>,
}

/// OAuth2 user info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuth2UserInfo {
    pub sub: String,
    pub name: String,
    pub email: String,
    pub email_verified: bool,
    pub picture: Option<String>,
}

/// SAML authentication provider
pub struct SAMLProvider {
    entity_id: String,
    sso_url: String,
    slo_url: String,
    certificate: String,
    private_key: String,
    user_cache: Arc<RwLock<HashMap<String, User>>>, // assertion_id -> user
}

impl SAMLProvider {
    pub fn new(
        entity_id: String,
        sso_url: String,
        slo_url: String,
        certificate: String,
        private_key: String,
    ) -> Self {
        Self {
            entity_id,
            sso_url,
            slo_url,
            certificate,
            private_key,
            user_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn get_sso_url(&self, relay_state: &str) -> String {
        format!(
            "{}?SAMLRequest={}&RelayState={}",
            self.sso_url,
            urlencoding::encode(&self.create_authn_request()),
            urlencoding::encode(relay_state)
        )
    }

    fn create_authn_request(&self) -> String {
        // Mock SAML AuthnRequest - in real scenario, create proper XML
        format!("<samlp:AuthnRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\"_{}\" Version=\"2.0\" IssueInstant=\"{}\" Destination=\"{}\" AssertionConsumerServiceURL=\"{}\"><saml:Issuer xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\">{}</saml:Issuer></samlp:AuthnRequest>",
                uuid::Uuid::new_v4(),
                chrono::Utc::now().to_rfc3339(),
                self.sso_url,
                self.sso_url,
                self.entity_id)
    }

    pub async fn process_saml_response(&self, _saml_response: &str) -> Result<User, SecurityError> {
        // Mock SAML response processing - in real scenario, parse and validate XML
        let user = User::new(
            "saml_user_123".to_string(),
            "saml_user".to_string(),
            "saml@example.com".to_string(),
            "SAML User".to_string(),
        );

        // Cache the user
        {
            let mut cache = self.user_cache.write().await;
            cache.insert("assertion_123".to_string(), user.clone());
        }

        Ok(user)
    }
}

#[async_trait]
impl AuthProvider for SAMLProvider {
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> Result<AuthenticationResult, SecurityError> {
        match &credentials.credential_type {
            CredentialType::SAML => {
                if let Some(saml_response) = credentials.get_data("saml_response") {
                    let user = self.process_saml_response(saml_response).await?;
                    let token = format!("saml_token_{}", uuid::Uuid::new_v4());
                    let expires_at = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_secs()
                        + 3600;

                    Ok(AuthenticationResult::success(user, token, expires_at))
                } else {
                    Err(SecurityError::authentication_failed(
                        "Missing SAML response",
                    ))
                }
            }
            _ => Err(SecurityError::authentication_failed(
                "Invalid credential type for SAML",
            )),
        }
    }

    async fn validate_token(&self, token: &str) -> Result<User, SecurityError> {
        // For SAML, we'd typically validate the token differently
        // This is a simplified implementation
        if token.starts_with("saml_token_") {
            // Mock user for SAML token
            Ok(User::new(
                "saml_user_123".to_string(),
                "saml_user".to_string(),
                "saml@example.com".to_string(),
                "SAML User".to_string(),
            ))
        } else {
            Err(SecurityError::token_error("Invalid SAML token"))
        }
    }

    async fn refresh_token(
        &self,
        _refresh_token: &str,
    ) -> Result<AuthenticationResult, SecurityError> {
        Err(SecurityError::token_error(
            "SAML tokens cannot be refreshed",
        ))
    }

    async fn logout(&self, _token: &str) -> Result<(), SecurityError> {
        // SAML logout would typically involve SLO (Single Logout)
        Ok(())
    }

    fn provider_type(&self) -> &'static str {
        "saml"
    }
}

/// Simple username/password authentication provider
pub struct UsernamePasswordProvider {
    users: Arc<RwLock<HashMap<String, (String, User)>>>, // username -> (password_hash, user)
}

impl UsernamePasswordProvider {
    pub fn new() -> Self {
        Self {
            users: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn add_user(&self, username: String, password: String, user: User) {
        let password_hash = self.hash_password(&password);
        let mut users = self.users.write().await;
        users.insert(username, (password_hash, user));
    }

    fn hash_password(&self, password: &str) -> String {
        // In real implementation, use proper password hashing like bcrypt
        format!("hash_{}", password)
    }

    fn verify_password(&self, password: &str, hash: &str) -> bool {
        // In real implementation, use proper password verification
        hash == &format!("hash_{}", password)
    }
}

#[async_trait]
impl AuthProvider for UsernamePasswordProvider {
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> Result<AuthenticationResult, SecurityError> {
        match &credentials.credential_type {
            CredentialType::UsernamePassword => {
                if let (Some(username), Some(password)) =
                    (&credentials.username, &credentials.password)
                {
                    let users = self.users.read().await;
                    if let Some((password_hash, user)) = users.get(username) {
                        if self.verify_password(password, password_hash) {
                            let token = format!("token_{}", uuid::Uuid::new_v4());
                            let expires_at = std::time::SystemTime::now()
                                .duration_since(std::time::UNIX_EPOCH)
                                .unwrap()
                                .as_secs()
                                + 3600;

                            Ok(AuthenticationResult::success(
                                user.clone(),
                                token,
                                expires_at,
                            ))
                        } else {
                            Err(SecurityError::authentication_failed("Invalid password"))
                        }
                    } else {
                        Err(SecurityError::authentication_failed("User not found"))
                    }
                } else {
                    Err(SecurityError::authentication_failed(
                        "Missing username or password",
                    ))
                }
            }
            _ => Err(SecurityError::authentication_failed(
                "Invalid credential type for username/password",
            )),
        }
    }

    async fn validate_token(&self, token: &str) -> Result<User, SecurityError> {
        // Simple token validation - in real implementation, use JWT or similar
        if token.starts_with("token_") {
            // Mock user for token
            Ok(User::new(
                "user123".to_string(),
                "testuser".to_string(),
                "test@example.com".to_string(),
                "Test User".to_string(),
            ))
        } else {
            Err(SecurityError::token_error("Invalid token"))
        }
    }

    async fn refresh_token(
        &self,
        _refresh_token: &str,
    ) -> Result<AuthenticationResult, SecurityError> {
        Err(SecurityError::token_error("Token refresh not supported"))
    }

    async fn logout(&self, _token: &str) -> Result<(), SecurityError> {
        Ok(())
    }

    fn provider_type(&self) -> &'static str {
        "username_password"
    }
}