Skip to main content

nv_redfish_bmc_http/
credentials.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use std::fmt;
17
18/// Credentials used to access the BMC.
19///
20/// Security notes:
21/// - `Debug`/`Display` redact secrets by design.
22/// - Prefer short-lived instances and avoid logging credentials.
23#[derive(Clone)]
24pub enum BmcCredentials {
25    /// Use HTTP Basic authentication with username and password.
26    UsernamePassword {
27        /// Username to access BMC.
28        username: String,
29        password: Option<String>,
30    },
31    /// Use Redfish session token authentication.
32    Token {
33        token: String,
34    },
35}
36
37impl BmcCredentials {
38    /// Create username/password credentials.
39    #[must_use]
40    pub const fn username_password(username: String, password: Option<String>) -> Self {
41        Self::UsernamePassword { username, password }
42    }
43
44    /// Create token credentials.
45    #[must_use]
46    pub const fn token(token: String) -> Self {
47        Self::Token { token }
48    }
49
50    /// Create new username/password credentials.
51    #[must_use]
52    pub const fn new(username: String, password: String) -> Self {
53        Self::username_password(username, Some(password))
54    }
55}
56
57impl fmt::Debug for BmcCredentials {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            Self::UsernamePassword { username, .. } => f
61                .debug_struct("BmcCredentials::UsernamePassword")
62                .field("username", username)
63                .field("password", &"[REDACTED]")
64                .finish(),
65            Self::Token { .. } => f
66                .debug_struct("BmcCredentials::Token")
67                .field("token", &"[REDACTED]")
68                .finish(),
69        }
70    }
71}
72
73impl fmt::Display for BmcCredentials {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            Self::UsernamePassword { username, .. } => {
77                write!(
78                    f,
79                    "BmcCredentials::UsernamePassword(username: {username}, password: [REDACTED])"
80                )
81            }
82            Self::Token { .. } => write!(f, "BmcCredentials::Token(token: [REDACTED])"),
83        }
84    }
85}