Skip to main content

fprovider/adapters/openai/
auth.rs

1//! OpenAI-specific credential helpers and auth resolution policy.
2
3use crate::{ProviderError, ProviderId, SecureCredentialManager};
4
5use super::types::OpenAiAuth;
6
7impl SecureCredentialManager {
8    /// Stores an OpenAI API key for provider-authenticated requests.
9    ///
10    /// OpenAI keys are expected to start with `sk-`.
11    pub fn set_openai_api_key(&self, api_key: impl Into<String>) -> Result<(), ProviderError> {
12        let api_key = api_key.into();
13        if !api_key.starts_with("sk-") {
14            return Err(ProviderError::authentication(
15                "OpenAI API key must start with 'sk-'",
16            ));
17        }
18
19        self.set_api_key(ProviderId::OpenAi, api_key)
20    }
21}
22
23/// Resolves OpenAI authentication from API key credentials only.
24pub(crate) fn resolve_openai_auth(
25    credentials: &SecureCredentialManager,
26) -> Result<OpenAiAuth, ProviderError> {
27    if let Some(api_key) = credentials.api_key(ProviderId::OpenAi)? {
28        return Ok(OpenAiAuth::ApiKey(api_key));
29    }
30
31    Err(ProviderError::authentication(
32        "no OpenAI API key configured",
33    ))
34}