Skip to main content

reqsign_google/provide_credential/
file.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use log::debug;
19
20use reqsign_core::{Context, ProvideCredential, Result};
21
22use crate::credential::Credential;
23
24use super::parse::parse_credential_bytes;
25
26/// FileCredentialProvider loads Google credentials from an explicit credential file path.
27#[derive(Debug, Clone)]
28pub struct FileCredentialProvider {
29    path: String,
30    scope: Option<String>,
31}
32
33impl FileCredentialProvider {
34    /// Create a new FileCredentialProvider from a credential file path.
35    pub fn new(path: impl Into<String>) -> Self {
36        Self {
37            path: path.into(),
38            scope: None,
39        }
40    }
41
42    /// Set the OAuth2 scope.
43    pub fn with_scope(mut self, scope: impl Into<String>) -> Self {
44        self.scope = Some(scope.into());
45        self
46    }
47}
48
49impl ProvideCredential for FileCredentialProvider {
50    type Credential = Credential;
51
52    async fn provide_credential(&self, ctx: &Context) -> Result<Option<Self::Credential>> {
53        debug!("loading credential from file path: {}", self.path);
54
55        let content = ctx.file_read(&self.path).await?;
56        parse_credential_bytes(ctx, &content, self.scope.clone()).await
57    }
58}