Skip to content

Commit c892ece

Browse files
authored
feat: Update XrpcClient, add AuthorizationToken (#248)
1 parent 4896bb7 commit c892ece

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

atrium-api/src/agent/inner.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
use super::{Session, SessionStore};
22
use crate::did_doc::DidDocument;
3-
use crate::types::string::Did;
4-
use crate::types::TryFromUnknown;
5-
use atrium_xrpc::error::{Error, Result, XrpcErrorKind};
6-
use atrium_xrpc::{HttpClient, OutputDataOrBytes, XrpcClient, XrpcRequest};
3+
use crate::types::{string::Did, TryFromUnknown};
4+
use atrium_xrpc::{
5+
error::{Error, Result, XrpcErrorKind},
6+
types::AuthorizationToken,
7+
HttpClient, OutputDataOrBytes, XrpcClient, XrpcRequest,
8+
};
79
use http::{Method, Request, Response};
810
use serde::{de::DeserializeOwned, Serialize};
9-
use std::fmt::Debug;
10-
use std::sync::{Arc, RwLock};
11+
use std::{
12+
fmt::Debug,
13+
sync::{Arc, RwLock},
14+
};
1115
use tokio::sync::{Mutex, Notify};
1216

1317
struct WrapperClient<S, T> {
@@ -72,13 +76,13 @@ where
7276
fn base_uri(&self) -> String {
7377
self.store.get_endpoint()
7478
}
75-
async fn authentication_token(&self, is_refresh: bool) -> Option<String> {
79+
async fn authorization_token(&self, is_refresh: bool) -> Option<AuthorizationToken> {
7680
self.store.get_session().await.map(|session| {
77-
if is_refresh {
81+
AuthorizationToken::Bearer(if is_refresh {
7882
session.data.refresh_jwt
7983
} else {
8084
session.data.access_jwt
81-
}
85+
})
8286
})
8387
}
8488
async fn atproto_proxy_header(&self) -> Option<String> {

atrium-xrpc/src/traits.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use crate::error::Error;
2-
use crate::error::{XrpcError, XrpcErrorKind};
3-
use crate::types::{Header, NSID_REFRESH_SESSION};
1+
use crate::error::{Error, XrpcError, XrpcErrorKind};
2+
use crate::types::{AuthorizationToken, Header, NSID_REFRESH_SESSION};
43
use crate::{InputDataOrBytes, OutputDataOrBytes, XrpcRequest};
54
use http::{Method, Request, Response};
65
use serde::{de::DeserializeOwned, Serialize};
7-
use std::fmt::Debug;
8-
use std::future::Future;
6+
use std::{fmt::Debug, future::Future};
97

108
/// An abstract HTTP client.
119
#[cfg_attr(not(target_arch = "wasm32"), trait_variant::make(Send))]
@@ -32,9 +30,12 @@ type XrpcResult<O, E> = core::result::Result<OutputDataOrBytes<O>, self::Error<E
3230
pub trait XrpcClient: HttpClient {
3331
/// The base URI of the XRPC server.
3432
fn base_uri(&self) -> String;
35-
/// Get the authentication token to use `Authorization` header.
33+
/// Get the authorization token to use `Authorization` header.
3634
#[allow(unused_variables)]
37-
fn authentication_token(&self, is_refresh: bool) -> impl Future<Output = Option<String>> {
35+
fn authorization_token(
36+
&self,
37+
is_refresh: bool,
38+
) -> impl Future<Output = Option<AuthorizationToken>> {
3839
async { None }
3940
}
4041
/// Get the `atproto-proxy` header.
@@ -102,12 +103,10 @@ where
102103
builder = builder.header(Header::ContentType, encoding);
103104
}
104105
if let Some(token) = client
105-
.authentication_token(
106-
request.method == Method::POST && request.nsid == NSID_REFRESH_SESSION,
107-
)
106+
.authorization_token(request.method == Method::POST && request.nsid == NSID_REFRESH_SESSION)
108107
.await
109108
{
110-
builder = builder.header(Header::Authorization, format!("Bearer {}", token));
109+
builder = builder.header(Header::Authorization, token);
111110
}
112111
if let Some(proxy) = client.atproto_proxy_header().await {
113112
builder = builder.header(Header::AtprotoProxy, proxy);

atrium-xrpc/src/types.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
use http::header::{AUTHORIZATION, CONTENT_TYPE};
2-
use http::{HeaderName, Method};
1+
use http::header::{HeaderName, HeaderValue, InvalidHeaderValue, AUTHORIZATION, CONTENT_TYPE};
2+
use http::Method;
33
use serde::{de::DeserializeOwned, Serialize};
44

55
pub(crate) const NSID_REFRESH_SESSION: &str = "com.atproto.server.refreshSession";
66

7+
pub enum AuthorizationToken {
8+
Bearer(String),
9+
Dpop(String),
10+
}
11+
12+
impl TryFrom<AuthorizationToken> for HeaderValue {
13+
type Error = InvalidHeaderValue;
14+
15+
fn try_from(token: AuthorizationToken) -> Result<Self, Self::Error> {
16+
HeaderValue::from_str(&match token {
17+
AuthorizationToken::Bearer(t) => format!("Bearer {t}"),
18+
AuthorizationToken::Dpop(t) => format!("DPoP {t}"),
19+
})
20+
}
21+
}
22+
723
/// HTTP headers which can be used in XPRC requests.
824
pub enum Header {
925
ContentType,

0 commit comments

Comments
 (0)