litellm-rs 0.6.0

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
//! Token refresh endpoint

use crate::server::routes::ApiResponse;
use crate::server::state::AppState;
use actix_web::{HttpResponse, Result as ActixResult, web};
use async_trait::async_trait;
use tracing::{debug, error, warn};

use super::models::{
    AuthFlowFailure, AuthorizationStage, EncodingStage, PrimaryAuthStage, RefreshTokenRequest,
    RefreshWireRequest, SequencedAuthFlow, TeamSelectionStage, execute_auth_flow,
};

type RefreshFlowDriver<P, T, A, E> = SequencedAuthFlow<P, T, A, E>;

#[async_trait(?Send)]
trait RefreshTokenVerifier {
    async fn verify(
        &mut self,
        refresh_token: &str,
    ) -> crate::utils::error::gateway_error::Result<uuid::Uuid>;
}

struct JwtRefreshTokenVerifier<'a>(&'a crate::auth::jwt::types::JwtHandler);

#[async_trait(?Send)]
impl RefreshTokenVerifier for JwtRefreshTokenVerifier<'_> {
    async fn verify(
        &mut self,
        refresh_token: &str,
    ) -> crate::utils::error::gateway_error::Result<uuid::Uuid> {
        self.0.verify_refresh_token(refresh_token).await
    }
}

#[async_trait(?Send)]
trait RefreshUserLookup {
    async fn find_user(
        &mut self,
        user_id: uuid::Uuid,
    ) -> crate::utils::error::gateway_error::Result<Option<crate::core::models::user::types::User>>;
}

struct DatabaseRefreshUserLookup<'a>(&'a crate::storage::database::Database);

#[async_trait(?Send)]
impl RefreshUserLookup for DatabaseRefreshUserLookup<'_> {
    async fn find_user(
        &mut self,
        user_id: uuid::Uuid,
    ) -> crate::utils::error::gateway_error::Result<Option<crate::core::models::user::types::User>>
    {
        self.0.find_user_by_id(user_id).await
    }
}

struct RefreshPrimaryStage<V, U> {
    verifier: V,
    users: U,
    request: RefreshTokenRequest,
}

#[async_trait(?Send)]
impl<V: RefreshTokenVerifier, U: RefreshUserLookup> PrimaryAuthStage for RefreshPrimaryStage<V, U> {
    type Principal = crate::core::models::user::types::User;

    async fn authenticate(&mut self) -> Result<Self::Principal, AuthFlowFailure> {
        let user_id = self
            .verifier
            .verify(&self.request.refresh_token)
            .await
            .map_err(|error| {
                warn!("Invalid refresh token: {}", error);
                AuthFlowFailure::bad_request("Invalid refresh token")
            })?;
        let user = match self.users.find_user(user_id).await {
            Ok(Some(user)) => user,
            Ok(None) => {
                warn!("Refresh token for non-existent user: {}", user_id);
                return Err(AuthFlowFailure::unauthorized("Invalid token"));
            }
            Err(error) => {
                error!("Database error during token refresh: {}", error);
                return Err(AuthFlowFailure::internal("Database error"));
            }
        };
        if !user.is_active() {
            return Err(AuthFlowFailure::unauthorized("Invalid token"));
        }
        Ok(user)
    }
}

struct RefreshTeamSelectionStage<'a> {
    auth: &'a crate::auth::AuthSystem,
    team_id: Option<uuid::Uuid>,
}

#[async_trait(?Send)]
impl TeamSelectionStage<crate::core::models::user::types::User> for RefreshTeamSelectionStage<'_> {
    type Selection = crate::auth::jwt::types::VerifiedActiveTeam;

    async fn select_team(
        &mut self,
        user: &crate::core::models::user::types::User,
    ) -> Result<Option<Self::Selection>, AuthFlowFailure> {
        let Some(team_id) = self.team_id else {
            return Ok(None);
        };
        match self.auth.validate_active_team(user.id(), team_id).await {
            Ok(Some(verified)) => Ok(Some(verified)),
            Ok(None) => Err(AuthFlowFailure::bad_request("Invalid team selection")),
            Err(error) => {
                error!("Team validation failed during token refresh: {}", error);
                Err(AuthFlowFailure::internal("Internal server error"))
            }
        }
    }
}

struct RefreshAuthorizationStage<'a> {
    auth: &'a crate::auth::AuthSystem,
}

#[async_trait(?Send)]
impl AuthorizationStage<crate::core::models::user::types::User> for RefreshAuthorizationStage<'_> {
    type Authorization = Vec<String>;

    async fn authorize(
        &mut self,
        user: &crate::core::models::user::types::User,
    ) -> Result<Self::Authorization, AuthFlowFailure> {
        self.auth
            .rbac()
            .get_user_permissions(user)
            .await
            .map_err(|error| {
                error!("Failed to load permissions during token refresh: {}", error);
                AuthFlowFailure::internal("Internal server error")
            })
    }
}

struct RefreshEncodingStage<'a> {
    jwt: &'a crate::auth::jwt::types::JwtHandler,
}

#[async_trait(?Send)]
impl
    EncodingStage<
        crate::core::models::user::types::User,
        crate::auth::jwt::types::VerifiedActiveTeam,
        Vec<String>,
    > for RefreshEncodingStage<'_>
{
    type Output = crate::auth::jwt::types::TokenPair;

    async fn encode(
        &mut self,
        user: crate::core::models::user::types::User,
        verified_team: Option<crate::auth::jwt::types::VerifiedActiveTeam>,
        permissions: Vec<String>,
    ) -> Result<Self::Output, AuthFlowFailure> {
        let token_result = match verified_team.as_ref() {
            Some(verified_team) => {
                self.jwt
                    .create_token_pair_for_verified_team(
                        user.id(),
                        user.role.to_string(),
                        permissions,
                        verified_team,
                        None,
                    )
                    .await
            }
            None => {
                self.jwt
                    .create_token_pair(user.id(), user.role.to_string(), permissions, None, None)
                    .await
            }
        };
        token_result.map_err(|error| {
            error!("Failed to generate new tokens: {}", error);
            AuthFlowFailure::internal("Internal server error")
        })
    }
}

/// Refresh token endpoint
pub async fn refresh_token(
    state: web::Data<AppState>,
    request: web::Json<RefreshTokenRequest>,
) -> ActixResult<HttpResponse> {
    refresh_token_internal(state, request.into_inner(), None).await
}

pub(super) async fn refresh_token_with_wire(
    state: web::Data<AppState>,
    request: web::Json<RefreshWireRequest>,
) -> ActixResult<HttpResponse> {
    let request = request.into_inner();
    refresh_token_internal(state, request.public, request.team_id).await
}

async fn refresh_token_internal(
    state: web::Data<AppState>,
    request: RefreshTokenRequest,
    team_id: Option<uuid::Uuid>,
) -> ActixResult<HttpResponse> {
    debug!("Token refresh request");
    let mut flow = RefreshFlowDriver {
        primary: RefreshPrimaryStage {
            verifier: JwtRefreshTokenVerifier(state.auth.jwt()),
            users: DatabaseRefreshUserLookup(state.storage.database.as_ref()),
            request,
        },
        team: RefreshTeamSelectionStage {
            auth: state.auth.as_ref(),
            team_id,
        },
        authorization: RefreshAuthorizationStage {
            auth: state.auth.as_ref(),
        },
        encoding: RefreshEncodingStage {
            jwt: state.auth.jwt(),
        },
    };
    match execute_auth_flow(&mut flow).await {
        Ok(tokens) => Ok(HttpResponse::Ok().json(ApiResponse::success(tokens))),
        Err(failure) => Ok(failure.into_response()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::Cell;
    use std::rc::Rc;

    struct VerifierSpy {
        valid: bool,
        user_id: uuid::Uuid,
        calls: Rc<Cell<usize>>,
    }

    #[async_trait(?Send)]
    impl RefreshTokenVerifier for VerifierSpy {
        async fn verify(
            &mut self,
            _refresh_token: &str,
        ) -> crate::utils::error::gateway_error::Result<uuid::Uuid> {
            self.calls.set(self.calls.get() + 1);
            if self.valid {
                Ok(self.user_id)
            } else {
                Err(crate::utils::error::gateway_error::GatewayError::auth(
                    "invalid refresh",
                ))
            }
        }
    }

    struct UserLookupSpy {
        user: Option<crate::core::models::user::types::User>,
        calls: Rc<Cell<usize>>,
    }

    #[async_trait(?Send)]
    impl RefreshUserLookup for UserLookupSpy {
        async fn find_user(
            &mut self,
            _user_id: uuid::Uuid,
        ) -> crate::utils::error::gateway_error::Result<
            Option<crate::core::models::user::types::User>,
        > {
            self.calls.set(self.calls.get() + 1);
            Ok(self.user.take())
        }
    }

    struct TeamSpy {
        selection: Result<Option<u8>, AuthFlowFailure>,
        calls: Rc<Cell<usize>>,
    }

    #[async_trait(?Send)]
    impl TeamSelectionStage<crate::core::models::user::types::User> for TeamSpy {
        type Selection = u8;

        async fn select_team(
            &mut self,
            _principal: &crate::core::models::user::types::User,
        ) -> Result<Option<Self::Selection>, AuthFlowFailure> {
            self.calls.set(self.calls.get() + 1);
            self.selection
        }
    }

    struct AuthorizationSpy {
        calls: Rc<Cell<usize>>,
    }

    #[async_trait(?Send)]
    impl AuthorizationStage<crate::core::models::user::types::User> for AuthorizationSpy {
        type Authorization = u8;

        async fn authorize(
            &mut self,
            _principal: &crate::core::models::user::types::User,
        ) -> Result<u8, AuthFlowFailure> {
            self.calls.set(self.calls.get() + 1);
            Ok(1)
        }
    }

    struct EncodingSpy {
        calls: Rc<Cell<usize>>,
    }

    #[async_trait(?Send)]
    impl EncodingStage<crate::core::models::user::types::User, u8, u8> for EncodingSpy {
        type Output = u8;

        async fn encode(
            &mut self,
            _principal: crate::core::models::user::types::User,
            _selection: Option<u8>,
            _authorization: u8,
        ) -> Result<Self::Output, AuthFlowFailure> {
            self.calls.set(self.calls.get() + 1);
            Ok(1)
        }
    }

    #[tokio::test]
    async fn gh1130_refresh_driver_primary_failures_never_check_team_or_encode() {
        for (case_name, primary_kind, selection) in [
            ("invalid_refresh_with_team", "invalid", Ok(Some(1))),
            (
                "invalid_refresh_foreign_team",
                "invalid",
                Err(AuthFlowFailure::bad_request("foreign team")),
            ),
            ("missing_user_with_team", "missing", Ok(Some(1))),
            ("missing_user_without_team", "missing", Ok(None)),
            ("inactive_user_with_team", "inactive", Ok(Some(1))),
            ("inactive_user_without_team", "inactive", Ok(None)),
        ] {
            let verifier_calls = Rc::new(Cell::new(0));
            let user_lookup_calls = Rc::new(Cell::new(0));
            let team_calls = Rc::new(Cell::new(0));
            let rbac_calls = Rc::new(Cell::new(0));
            let encode_calls = Rc::new(Cell::new(0));
            let user_id = uuid::Uuid::new_v4();
            let user = if primary_kind == "inactive" {
                let mut user = crate::core::models::user::types::User::new(
                    "inactive-refresh-user".to_string(),
                    "inactive-refresh@example.com".to_string(),
                    "unused-hash".to_string(),
                );
                user.status = crate::core::models::user::types::UserStatus::Inactive;
                Some(user)
            } else {
                None
            };
            let mut flow = RefreshFlowDriver {
                primary: RefreshPrimaryStage {
                    verifier: VerifierSpy {
                        valid: primary_kind != "invalid",
                        user_id,
                        calls: verifier_calls.clone(),
                    },
                    users: UserLookupSpy {
                        user,
                        calls: user_lookup_calls.clone(),
                    },
                    request: RefreshTokenRequest {
                        refresh_token: "refresh".to_string(),
                    },
                },
                team: TeamSpy {
                    selection,
                    calls: team_calls.clone(),
                },
                authorization: AuthorizationSpy {
                    calls: rbac_calls.clone(),
                },
                encoding: EncodingSpy {
                    calls: encode_calls.clone(),
                },
            };
            let failure = match execute_auth_flow(&mut flow).await {
                Ok(_) => panic!("invalid refresh principal must fail"),
                Err(failure) => failure,
            };
            let expected_status = if primary_kind == "invalid" {
                actix_web::http::StatusCode::BAD_REQUEST
            } else {
                actix_web::http::StatusCode::UNAUTHORIZED
            };
            assert_eq!(
                failure.into_response().status(),
                expected_status,
                "{case_name}"
            );
            assert_eq!(verifier_calls.get(), 1, "{case_name}");
            assert_eq!(
                user_lookup_calls.get(),
                usize::from(primary_kind != "invalid"),
                "{case_name}"
            );
            assert_eq!(team_calls.get(), 0, "{case_name}");
            assert_eq!(rbac_calls.get(), 0, "{case_name}");
            assert_eq!(encode_calls.get(), 0, "{case_name}");
        }
    }
}