Skip to main content

scion_sdk_reqwest_connect_rpc/token_source/
static_token.rs

1// Copyright 2025 Anapaya Systems
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Static token source implementation.
16//!
17//! Allows using a `String` or `&'static str` as a token source.
18
19use async_trait::async_trait;
20use tokio::sync::watch;
21
22use crate::token_source::{TokenSource, TokenSourceError};
23
24/// A static token source that always returns the same token.
25/// Can be created from a `String` or `&'static str`.
26pub struct StaticTokenSource {
27    token: String,
28    watch_tx: watch::Sender<Option<Result<String, TokenSourceError>>>,
29}
30
31impl From<String> for StaticTokenSource {
32    fn from(token: String) -> Self {
33        let (watch_tx, _watch_rx) = watch::channel(Some(Ok(token.clone())));
34        StaticTokenSource { token, watch_tx }
35    }
36}
37
38impl From<&'static str> for StaticTokenSource {
39    fn from(token: &'static str) -> Self {
40        let (watch_tx, _watch_rx) = watch::channel(Some(Ok(token.to_string())));
41        StaticTokenSource {
42            token: token.to_string(),
43            watch_tx,
44        }
45    }
46}
47
48#[async_trait]
49impl TokenSource for StaticTokenSource {
50    async fn get_token(&self) -> Result<String, TokenSourceError> {
51        Ok(self.token.clone())
52    }
53
54    fn watch(&self) -> watch::Receiver<Option<Result<String, TokenSourceError>>> {
55        self.watch_tx.subscribe()
56    }
57}