Skip to content

Commit

Permalink
Implement draft /auth API
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Oct 27, 2021
1 parent d3e23df commit 79b6835
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 54 deletions.
21 changes: 18 additions & 3 deletions book/src/api-vc-auth-header.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ The validator client HTTP server requires that all requests have the following
HTTP header:

- Name: `Authorization`
- Value: `Basic <api-token>`
- Value: `Bearer <api-token>`

Where `<api-token>` is a string that can be obtained from the validator client
host. Here is an example `Authorization` header:

```
Authorization Basic api-token-0x03eace4c98e8f77477bb99efb74f9af10d800bd3318f92c33b719a4644254d4123
Authorization: Bearer api-token-0x03eace4c98e8f77477bb99efb74f9af10d800bd3318f92c33b719a4644254d4123
```

## Obtaining the API token
Expand All @@ -35,12 +35,27 @@ to the file containing the api token.
Sep 28 19:17:52.615 INFO HTTP API started api_token_file: "$HOME/prater/validators/api-token.txt", listen_address: 127.0.0.1:5062
```

The _path_ to the API token may also be fetched from the HTTP API itself (this endpoint is the only
one accessible without the token):

```bash
curl http://localhost:5062/eth/v1/auth
```

Response:

```json
{
"token_path": "/home/karlm/.lighthouse/prater/validators/api-token.txt"
}
```

## Example

Here is an example `curl` command using the API token in the `Authorization` header:

```bash
curl localhost:5062/lighthouse/version -H "Authorization: Basic api-token-0x03eace4c98e8f77477bb99efb74f9af10d800bd3318f92c33b719a4644254d4123"
curl localhost:5062/lighthouse/version -H "Authorization: Bearer api-token-0x03eace4c98e8f77477bb99efb74f9af10d800bd3318f92c33b719a4644254d4123"
```

The server should respond with its version:
Expand Down
15 changes: 15 additions & 0 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use serde::{de::DeserializeOwned, Serialize};
use std::convert::TryFrom;
use std::fmt;
use std::iter::Iterator;
use std::path::PathBuf;
use std::time::Duration;

pub const V1: EndpointVersion = EndpointVersion(1);
Expand Down Expand Up @@ -53,6 +54,12 @@ pub enum Error {
InvalidServerSentEvent(String),
/// The server returned an invalid SSZ response.
InvalidSsz(ssz::DecodeError),
/// An I/O error occurred while loading an API token from disk.
TokenReadError(PathBuf, std::io::Error),
/// The client has been configured without a server pubkey, but requires one for this request.
NoServerPubkey,
/// The client has been configured without an API token, but requires one for this request.
NoToken,
}

impl Error {
Expand All @@ -70,6 +77,8 @@ impl Error {
Error::InvalidJson(_) => None,
Error::InvalidServerSentEvent(_) => None,
Error::InvalidSsz(_) => None,
Error::TokenReadError(..) => None,
Error::NoServerPubkey | Error::NoToken => None,
}
}
}
Expand All @@ -80,6 +89,12 @@ impl fmt::Display for Error {
}
}

impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Error::Reqwest(error)
}
}

/// A struct to define a variety of different timeouts for different validator tasks to ensure
/// proper fallback behaviour.
#[derive(Clone)]
Expand Down
Loading

0 comments on commit 79b6835

Please sign in to comment.