mirror of https://github.com/rwf2/Rocket.git
Rename 'APIKey' in example docs to 'ApiKey'.
This commit is contained in:
parent
0bbfa5e21a
commit
70bc97c322
|
@ -162,8 +162,8 @@ impl<S, E> IntoOutcome<S, (Status, E), ()> for Result<S, E> {
|
||||||
/// requests be sent along with a valid API key in a header field. You want to
|
/// requests be sent along with a valid API key in a header field. You want to
|
||||||
/// ensure that the handlers corresponding to these requests don't get called
|
/// ensure that the handlers corresponding to these requests don't get called
|
||||||
/// unless there is an API key in the request and the key is valid. The
|
/// unless there is an API key in the request and the key is valid. The
|
||||||
/// following example implements this using an `APIKey` type and a `FromRequest`
|
/// following example implements this using an `ApiKey` type and a `FromRequest`
|
||||||
/// implementation for that type. The `APIKey` type is then used in the
|
/// implementation for that type. The `ApiKey` type is then used in the
|
||||||
/// `senstive` handler.
|
/// `senstive` handler.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
@ -175,17 +175,17 @@ impl<S, E> IntoOutcome<S, (Status, E), ()> for Result<S, E> {
|
||||||
/// use rocket::http::Status;
|
/// use rocket::http::Status;
|
||||||
/// use rocket::request::{self, Request, FromRequest};
|
/// use rocket::request::{self, Request, FromRequest};
|
||||||
///
|
///
|
||||||
/// struct APIKey(String);
|
/// struct ApiKey(String);
|
||||||
///
|
///
|
||||||
/// /// Returns true if `key` is a valid API key string.
|
/// /// Returns true if `key` is a valid API key string.
|
||||||
/// fn is_valid(key: &str) -> bool {
|
/// fn is_valid(key: &str) -> bool {
|
||||||
/// key == "valid_api_key"
|
/// key == "valid_api_key"
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl<'a, 'r> FromRequest<'a, 'r> for APIKey {
|
/// impl<'a, 'r> FromRequest<'a, 'r> for ApiKey {
|
||||||
/// type Error = ();
|
/// type Error = ();
|
||||||
///
|
///
|
||||||
/// fn from_request(request: &'a Request<'r>) -> request::Outcome<APIKey, ()> {
|
/// fn from_request(request: &'a Request<'r>) -> request::Outcome<ApiKey, ()> {
|
||||||
/// let keys: Vec<_> = request.headers().get("x-api-key").collect();
|
/// let keys: Vec<_> = request.headers().get("x-api-key").collect();
|
||||||
/// if keys.len() != 1 {
|
/// if keys.len() != 1 {
|
||||||
/// return Outcome::Failure((Status::BadRequest, ()));
|
/// return Outcome::Failure((Status::BadRequest, ()));
|
||||||
|
@ -196,12 +196,12 @@ impl<S, E> IntoOutcome<S, (Status, E), ()> for Result<S, E> {
|
||||||
/// return Outcome::Forward(());
|
/// return Outcome::Forward(());
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// return Outcome::Success(APIKey(key.to_string()));
|
/// return Outcome::Success(ApiKey(key.to_string()));
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// #[get("/sensitive")]
|
/// #[get("/sensitive")]
|
||||||
/// fn sensitive(key: APIKey) -> &'static str {
|
/// fn sensitive(key: ApiKey) -> &'static str {
|
||||||
/// # let _key = key;
|
/// # let _key = key;
|
||||||
/// "Sensitive data."
|
/// "Sensitive data."
|
||||||
/// }
|
/// }
|
||||||
|
|
|
@ -89,15 +89,15 @@ request handler signature.
|
||||||
Request guards _protect_ the handler from running unless some set of conditions
|
Request guards _protect_ the handler from running unless some set of conditions
|
||||||
are met by the incoming request metadata. For instance, if you are writing an
|
are met by the incoming request metadata. For instance, if you are writing an
|
||||||
API that requires sensitive calls to be accompanied by an API key in the request
|
API that requires sensitive calls to be accompanied by an API key in the request
|
||||||
header, Rocket can protect those calls via a custom `APIKey` request guard:
|
header, Rocket can protect those calls via a custom `ApiKey` request guard:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[get("/sensitive")]
|
#[get("/sensitive")]
|
||||||
fn sensitive(key: APIKey) -> &'static str { ... }
|
fn sensitive(key: ApiKey) -> &'static str { ... }
|
||||||
```
|
```
|
||||||
|
|
||||||
`APIKey` protects the `sensitive` handler from running incorrectly. In order for
|
`ApiKey` protects the `sensitive` handler from running incorrectly. In order for
|
||||||
Rocket to call the `sensitive` handler, the `APIKey` type needs to be derived
|
Rocket to call the `sensitive` handler, the `ApiKey` type needs to be derived
|
||||||
through a
|
through a
|
||||||
[FromRequest](https://api.rocket.rs/rocket/request/trait.FromRequest.html)
|
[FromRequest](https://api.rocket.rs/rocket/request/trait.FromRequest.html)
|
||||||
implementation, which in this case, validates the API key header. Request guards
|
implementation, which in this case, validates the API key header. Request guards
|
||||||
|
|
Loading…
Reference in New Issue