This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initial work refactoring noosphere-gateway to be generic over the
spheres it manages.
- Loading branch information
Showing
29 changed files
with
767 additions
and
471 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//! A collection of typed [headers::Header] implementations | ||
//! used in gateway APIs. | ||
#[cfg(doc)] | ||
use headers; | ||
|
||
mod ucan; | ||
|
||
pub use self::ucan::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::data::Jwt; | ||
use anyhow::anyhow; | ||
use cid::Cid; | ||
use headers::{self, Header, HeaderName, HeaderValue}; | ||
use once_cell::sync::Lazy; | ||
use ucan::{chain::ProofChain, crypto::did::DidParser, store::UcanJwtStore}; | ||
|
||
static UCAN_NAME: Lazy<HeaderName> = Lazy::new(|| HeaderName::from_static("ucan")); | ||
|
||
/// A typed header for the `ucan` header, a tuple of [Cid] and [Jwt]. | ||
/// Can represent a request with multiple `ucan` headers. | ||
/// | ||
/// The values are **not** validated during parsing beyond legitimate | ||
/// looking [Cid]s and [Jwt]s here. Use [Ucan::as_proof_chain] to | ||
/// validate and construct a [ProofChain]. | ||
pub struct Ucan(Vec<(Cid, Jwt)>); | ||
|
||
impl Header for Ucan { | ||
fn name() -> &'static HeaderName { | ||
&UCAN_NAME | ||
} | ||
|
||
fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error> | ||
where | ||
I: Iterator<Item = &'i HeaderValue>, | ||
{ | ||
let mut ucans = vec![]; | ||
for header in values.by_ref() { | ||
let value = header.to_str().map_err(|_| headers::Error::invalid())?; | ||
let mut parts: Vec<&str> = value.split_ascii_whitespace().take(2).collect(); | ||
|
||
let jwt: Jwt = parts | ||
.pop() | ||
.ok_or_else(headers::Error::invalid)? | ||
.to_string() | ||
.into(); | ||
let cid: Cid = parts | ||
.pop() | ||
.ok_or_else(headers::Error::invalid)? | ||
.try_into() | ||
.map_err(|_| headers::Error::invalid())?; | ||
|
||
ucans.push((cid, jwt)); | ||
} | ||
|
||
Ok(Ucan(ucans)) | ||
} | ||
|
||
fn encode<E>(&self, values: &mut E) | ||
where | ||
E: Extend<HeaderValue>, | ||
{ | ||
for (cid, jwt) in self.0.iter() { | ||
let value = HeaderValue::from_str(&format!("{} {}", cid, jwt)).unwrap(); | ||
values.extend(std::iter::once(value)); | ||
} | ||
} | ||
} | ||
|
||
impl Ucan { | ||
/// Construct a [ProofChain] from the collected `ucan` header | ||
/// values. This is where validation of the data occurs. | ||
pub async fn as_proof_chain( | ||
&self, | ||
bearer: headers::authorization::Bearer, | ||
mut db: impl UcanJwtStore, | ||
did_parser: &mut DidParser, | ||
) -> anyhow::Result<ProofChain> { | ||
for (cid, jwt) in self.0.iter() { | ||
// TODO(#261): We need a worker process that purges garbage UCANs | ||
let actual_cid = db.write_token(jwt.into()).await?; | ||
if actual_cid != *cid { | ||
return Err(anyhow!("Cid and Jwt do not match.")); | ||
} | ||
} | ||
|
||
let proof_chain = | ||
ProofChain::try_from_token_string(bearer.token(), None, did_parser, &db).await?; | ||
|
||
proof_chain.ucan().validate(None, did_parser).await?; | ||
|
||
Ok(proof_chain) | ||
} | ||
} | ||
|
||
impl From<Ucan> for Vec<(Cid, Jwt)> { | ||
fn from(value: Ucan) -> Self { | ||
value.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.