Public-key signature based authentication and authorization backend
Project description
ECUTH
A library for authentication and authorization with public key signature, using random challenge/response.
The main motivation for writing this package was to be able to use an established HTTP authentication scheme with Metamask to sign a challenge to prove identity. However, the package is (hopefully) designed to work in any generic case.
The server application in /example demonstrates the Metamask use-case, at the same time demonstrating all parts of the package.
Usage
In the examples folder a uwsgi HTTP server is provided. This illustrates how to use the eip712 and hoba filters to perform a pure HTTP authentication using Metamask as a signer, using the EthereumRetriever implementation.
To run the server, you need the "uwsgi python plugin" installed. Once you have, you can run it like this:
uwsgi --wsgi-file examples/server.py --http :5555
(note that the provided config uses port 5555 in the origin. It has no special meaning, just made up on the spot. If you change it, you will have to change the config, too).
The recipe is as follows:
- Server generates and stores the challenge, and sends it to the client in a
WWW-Authenticationheader along withrealmandmax-age. - Client uses the challenge to generate the HOBA signature material.
- Client hashes the HOBA signature material with sha256.
- Client uses the hashed HOBA signature material as the challenge in the EIP712 schema.
- Client serializes the challenge according to the EIP712 schema.
- Client prompts the puny human to sign the serialized data in step 5 above with Metamask.
- Client generates the HOBA authorization header value and sends it to the server. Note that:
- the challenge part of the header is the original server challenge.
- the signature part is a signature on the HOBA-challenge-wrapped-in-EIP712 described in steps 2-6 above.
- Server parses the header, verifies that the challenge hasn't expired.
- Server retrieves the filter pipeline from the stored challenge and executes it on the original challenge. The filters are, in sequence:
- HOBA filter
- SHA256 filter (this filter is defined in the server example source file directly)
- EIP712 filter
- The resulting value of step 9 is the data signed by the client. The ethereum address of the client is recovered using the signature and this data.
- Server requests a file at the base-url with filename matching the 0x-hex of the recovered ethereum address.
- If the file can be retrieved and is readable, it means that the authentication was a success. There should be much rejoicing.
Overview
ACL
The ACL items used in this example constitute a simple key/value store, implemented with the YAMLAcl class. The document must:
- be a valid YML document.
- must have two top-level elements;
levelanditems. itemsmust be a collection of key/value entries on a single-level.- the value of each
itemmust be0(no access),2(write) or4(read). (any key with value 0 can optionally be omitted).
level: <level>
items:
foo: 0
foo.bar: 2
xyzzy.baz: 4
...
Interpretation
The level field defines an hierarchical criteria for changing data:
If
levelof userXis lower or equal tolevelof userY, then userXcannot change or delete data for that user.
The values for the items keys are a simplificaiton of the chmod bitflag used in unix systems;
- If
0x04is set, read access is granted. - If
0x02is set, write access is granted.
GNUPG ACL encryption
The example uses gnupg for encryption of the ACL list.
Provided in this repository is a .gnupg resource directory with the keys used in the test and server, along with the test/data directory containing:
- exported keys
- the ACL source file
- the encrypted and signed ACL source file
- The private key (and keystore file) of the ethereum key used in the example
The latter was generated with this command:
gpg -aer 245508630E91CA06EFA1FBB20B297F3839D18362 --sign -u 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 -o test/data/0xe1AB8145F7E55DC933d51a18c793F901A3A0b276 test/data/data.yml
The test/server gnupg setup will expect the key that signed the ACL document to be fully trusted. The trustdb in .gnupg already includes this trust. Of course I'm not gonna give you my secret key, so you may want to try yourself with your own. When you do, just remember you have to update the trust settings.
... or just write your own decrypter method, dang nabbit :)
Filters
A deterministic transformation pipeline of the challenge can be added by using filter methods. This is useful in cases where the client needs to wrap the data before signing it.
Two filters are provided in the ecuth.filters package:
- EIP712: Metadata wrapper for Ethereum, increasingly standard in the ecosystem, and soon-to-be enforced by the Metamask message signer.
- HOBA: The HTTP Origin-Bound Authentication scheme, which allows authentication with client signing a challenge using public key crypto, exactly what we're exploring here.
These filters each have additional dependencies, defined as extra_requires in the setup.cfg file (i.e. to install them both, use pip install ecuth[eip712,hoba])
EIP712
Described in https://eips.ethereum.org/EIPS/eip-712
The provided filter will expect the challenge to be provided wrapped in the following (pseudo) schema:
const challenge_type = [
{
type: 'bytes',
name: 'challenge',
}
];
const domain_type = [
{
name: "name",
type: "string"
},
{
name: "version",
ype: "string"
},
{
name: "chainId",
type: "uint256",
},
//{ name: "verifyingContract", type: "address" },
//{ name: "salt", type: "bytes32" },
];
const data_to_serialize_and_sign = {
types: {
EIP712Domain: domain_type,
Challenge: challenge_type,
},
primaryType: "Challenge",
domain: <domain_data>,
message: {
challenge: <challenge_value>,
},
}
(verifyingContract and salt are commented out as the contract part not yet implemented)
To be clear; this schema is custom designed for the EIP712 filter and example server (see below).
HOBA
A superficial study of the existing authentication schemes for HTTP has suggested that HOBA is the least "hacky" for the purposes of a simple challenge/response mechanism where the public key is known to the server. The only caveat is that the secp256k1 signature algorithm used with Ethereum (and other cryptocurrencies) is not standardized as an IANA public key signature algo scheme. In fact, at the time of writing, only RSA-SHA256 and RSA-SHA1 are. Since all numbres between 2 and 99 are up for grabs, the arbitrary but not quite unpretentious number 42 has been chosen in the dependency package http-hoba-auth written specifically to support this package. So, collisions may happen. Beware.
That being said; it's anyway advisable to read RFC 7486 before you use this filter.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ecuth-0.4.4.tar.gz.
File metadata
- Download URL: ecuth-0.4.4.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8aca603fbc5b06c47e3c2a7bb2e1d107827650463ecdea2f2ae6078f2da8ab65
|
|
| MD5 |
39b3d423802400ffa8ce79834b66bda4
|
|
| BLAKE2b-256 |
ff18b28a7c3397145c41676b230f1425726953ac192767d808f56ae8d5454ff0
|
File details
Details for the file ecuth-0.4.4-py3-none-any.whl.
File metadata
- Download URL: ecuth-0.4.4-py3-none-any.whl
- Upload date:
- Size: 25.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18ba0de6e2ea56da5924cbce796494f8b767f49c24539e6f0339720975a54569
|
|
| MD5 |
1acd11ea9180107e6b5ed8dac15163cb
|
|
| BLAKE2b-256 |
c5b43a7a3a616e7a37a8ef6a170ea4fb0dc3c4e4458d8c2260c8f95b616b386b
|