Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up #735: auth.rs refactors #871

Merged
merged 3 commits into from
May 13, 2024
Merged

Conversation

bfops
Copy link
Collaborator

@bfops bfops commented Feb 20, 2024

Description of Changes

Splitting apart PRs, i.e. shamelessly stealing Noa's auth.rs changes from #735.

API and ABI breaking changes

No. This is expected to not change any user-facing behavior.

Expected complexity level and risk

2?

@bfops bfops requested a review from coolreader18 February 20, 2024 22:47
@bfops bfops changed the title auth.rs changes from #735 Split up #735 - auth.rs changes Feb 20, 2024
@bfops bfops changed the title Split up #735 - auth.rs changes Split up #735: auth.rs refactor Feb 20, 2024
@bfops bfops changed the title Split up #735: auth.rs refactor Split up #735: auth.rs refactors Feb 20, 2024
@bfops bfops marked this pull request as ready for review February 20, 2024 22:56
#[derive(Clone, Deserialize)]
pub struct SpacetimeCreds {
token: String,
}
Copy link
Collaborator Author

@bfops bfops Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am told this is not (de)serialized anywhere besides via the corresponding trait [1]

pub struct SpacetimeAuth {
pub creds: SpacetimeCreds,
pub identity: Identity,
}

pub struct SpacetimeAuthHeader {
pub auth: Option<SpacetimeAuth>,
impl SpacetimeAuth {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review note: this block was just moved from below, unchanged, so it's next to the struct definition.

let Some(creds) = SpacetimeCreds::from_request_parts(parts)? else {
return Ok(Self { auth: None });
};
let claims = creds.decode_token(state.public_key())?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub enum AuthorizationRejection {
Jwt(JwtError),
Header(headers::Error),
Required,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this used to be AuthorizationRejectionReason below, but was moved up and refactored

Jwt(JwtErrorKind),
Header(TypedHeaderRejection),
MalformedTokenQueryString,
CantDecodeAuthorizationToken,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 64 to 70
Ok(None) => Ok(()),
Err(e) => Err(e),
};
if let Ok(Query(creds)) = Query::<Self>::try_from_uri(&parts.uri) {
return Ok(Some(creds));
}
res.map(|()| None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(None) => Ok(()),
Err(e) => Err(e),
};
if let Ok(Query(creds)) = Query::<Self>::try_from_uri(&parts.uri) {
return Ok(Some(creds));
}
res.map(|()| None)
r @ Ok(None) => r,
Err(e) => Err(e),
};
if let Ok(q @ Query(_)) = Query::<Self>::try_from_uri(&parts.uri) {
return Ok(q);
}
res

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, thank you for drafting this!

Comment on lines 53 to +61
pub fn decode_token(&self, public_key: &DecodingKey) -> Result<SpacetimeIdentityClaims, JwtError> {
decode_token(public_key, self.token()).map(|x| x.claims)
}
pub fn encode_token(private_key: &EncodingKey, identity: Identity) -> Result<Self, JwtError> {
let token = encode_token(private_key, identity)?;
let headers::Authorization(basic) = headers::Authorization::basic(TOKEN_USERNAME, &token);
Ok(Self(basic))
Ok(Self { token })
}

fn from_request_parts(parts: &request::Parts) -> Result<Option<Self>, headers::Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs for these?

Comment on lines +85 to +91
pub async fn alloc(ctx: &(impl NodeDelegate + ControlStateDelegate + ?Sized)) -> axum::response::Result<Self> {
let identity = ctx.create_identity().await.map_err(log_and_500)?;
let creds = SpacetimeCreds::encode_token(ctx.private_key(), identity).map_err(log_and_500)?;
Ok(Self { creds, identity })
}

pub fn into_headers(self) -> (TypedHeader<SpacetimeIdentity>, TypedHeader<SpacetimeIdentityToken>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs?

@bfops bfops added the release-any To be landed in any release window label Feb 26, 2024
@bfops bfops marked this pull request as draft April 8, 2024 18:13
@coolreader18 coolreader18 marked this pull request as ready for review May 13, 2024 17:31
@coolreader18
Copy link
Collaborator

This got left behind - I'm good with these changes and I'll rebase #735 on top of them

@coolreader18 coolreader18 added this pull request to the merge queue May 13, 2024
@coolreader18
Copy link
Collaborator

(and add docs)

Merged via the queue into master with commit 4cdb660 May 13, 2024
7 checks passed
@bfops bfops deleted the zeke/noa-router-shuffle branch June 12, 2024 21:12
bfops added a commit that referenced this pull request Jun 12, 2024
bfops added a commit that referenced this pull request Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-any To be landed in any release window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants