Authentication and authorization
Quick answer
Authentication establishes who is making a request. Authorization decides whether that identity may perform the requested action on the requested resource.
Enforce permissions for every resource
Use established authentication middleware that validates token signatures, expiry, issuer and intended audience according to your protocol. Never treat a claimed user ID from a request as proof of identity. After authentication, enforce server-side resource ownership and operation-specific rules; deny by default. A missing or invalid identity generally results in 401 (with an appropriate authentication challenge), while an authenticated but forbidden request generally receives 403. The examples below model authorization after identity verification; they are not authentication implementations.
struct VerifiedIdentity { id: u64 }fn can_edit(actor: &VerifiedIdentity, owner: u64) -> bool { actor.id == owner }fn main() { let actor = VerifiedIdentity { id: 2 }; // synthetic, preverified identity println!("{}", can_edit(&actor, 2)); println!("{}", can_edit(&actor, 3));}Expected stdout (not executed):
truefalseIn a real service only a trusted verification boundary constructs this identity.
fn status(authenticated: bool, permitted: bool) -> u16 { if !authenticated { 401 } else if !permitted { 403 } else { 200 }}fn main() { for (a,p) in [(false,true),(true,false),(true,true)] { println!("{}", status(a,p)); } }Expected stdout (not executed):
401403200Permission alone is insufficient without authentication.
Common mistake
Do not use a client-supplied role or owner ID as a trusted authorization fact.
Try it yourself
Add a read-only role that may view but not edit an owned item.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Authentication and authorization — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A signed token is valid but its user lacks access to a record. What response category fits?
Authentication and authorization — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust struct VerifiedIdentity { id: u64 } fn can_edit(actor: &VerifiedIdentity, owner: u64) -> bool { actor.id == owner } fn main() { let actor = VerifiedIdentity { id: 2 }; // synthetic, preverified identity println!("{}", can_edit(&actor, 2)); println!("{}", can_edit(&actor, 3)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Authentication and authorization — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn status(authenticated: bool, permitted: bool) -> u16 { if !authenticated { 401 } else if !permitted { 403 } else { 200 } } fn main() { for (a,p) in [(false,true),(true,false),(true,true)] { println!("{}", status(a,p)); } } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.