Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions packages/core/guard/server/src/routing/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,36 @@ pub async fn route_request(

// Check auth (if enabled)
if let Some(auth) = &ctx.config().auth {
let token = headers
.get(X_RIVET_TOKEN)
.and_then(|x| x.to_str().ok())
// Fallback to checking websocket protocol if rivet token is not set
.or_else(|| {
if is_websocket {
headers
.get(SEC_WEBSOCKET_PROTOCOL)
.and_then(|protocols| protocols.to_str().ok())
.and_then(|protocols| {
protocols
.split(',')
.map(|p| p.trim())
.find_map(|p| p.strip_prefix(WS_PROTOCOL_TOKEN))
})
} else {
None
}
})
.ok_or_else(|| {
crate::errors::MissingHeader {
header: X_RIVET_TOKEN.to_string(),
}
.build()
})?;
// Extract token
let token = if is_websocket {
headers
.get(SEC_WEBSOCKET_PROTOCOL)
.and_then(|protocols| protocols.to_str().ok())
.and_then(|protocols| {
protocols
.split(',')
.map(|p| p.trim())
.find_map(|p| p.strip_prefix(WS_PROTOCOL_TOKEN))
})
.ok_or_else(|| {
crate::errors::MissingHeader {
header: SEC_WEBSOCKET_PROTOCOL.to_string(),
}
.build()
})?
} else {
headers
.get(X_RIVET_TOKEN)
.and_then(|x| x.to_str().ok())
.ok_or_else(|| {
crate::errors::MissingHeader {
header: X_RIVET_TOKEN.to_string(),
}
.build()
})?
};

// Validate token
if token != auth.admin_token {
return Err(rivet_api_builder::ApiForbidden.build());
}
Expand Down
Loading