Skip to main content

subspace_core/credentials/
credential.rs

1//! One credential: its value, how it is sent and where it came from.
2
3use crate::Secret;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Scheme {
7    Bearer,
8}
9
10/// Where a credential came from.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum Source {
13    /// The environment variable.
14    Environment(&'static str),
15    /// The config file key, such as `github.token`.
16    ConfigFile(&'static str),
17    /// An account signed in through Subspace.
18    SignedIn {
19        login: String,
20    },
21    GhCli {
22        login: String,
23    },
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct Credential {
28    pub value: Secret,
29    pub scheme: Scheme,
30    pub source: Source,
31}
32
33impl Credential {
34    /// A credential with its scheme and where it came from.
35    pub fn new(value: Secret, scheme: Scheme, source: Source) -> Self {
36        Self {
37            value,
38            scheme,
39            source,
40        }
41    }
42
43    /// The `Authorization` header value.
44    pub fn authorization(&self) -> Secret {
45        match self.scheme {
46            Scheme::Bearer => Secret::new(format!("Bearer {}", self.value.expose())),
47        }
48    }
49}