|
1 |
| -# A Serverless MFA API with support for WebAuthn |
| 1 | +# A Serverless MFA API with support for TOTP and WebAuthn |
2 | 2 |
|
3 |
| -This project provides a semi-generic backend API for supporting WebAuthn credential registration and authentication. |
4 |
| -It is intended to be run in a manner as to be shared between multiple consuming applications. It uses an API key |
5 |
| -and secret to authenticate requests, and further uses that secret as the encryption key. Loss of the API secret |
6 |
| -would mean loss of all WebAuthn credentials stored. |
| 3 | +This project provides a semi-generic backend API for supporting Time-based One Time Passcode (TOTP) and WebAuthn |
| 4 | +Passkey registration and authentication. It is intended to be run in a manner as to be shared between multiple consuming |
| 5 | +applications. It uses an API key and secret to authenticate requests, and further uses that secret as the encryption |
| 6 | +key. Loss of the API secret would mean loss of all credentials stored. |
7 | 7 |
|
8 | 8 | This application can be run in two ways:
|
9 | 9 | 1. As a standalone server using the builtin webserver available in the `server/` folder
|
10 |
| -2. As a AWS Lambda function using the `lambda/` implementation. This implementation can also use |
| 10 | +2. As an AWS Lambda function using the `lambda/` implementation. This implementation can also use |
11 | 11 | [AWS CDK](https://aws.amazon.com/cdk/) to help automate build/deployment. It should also be
|
12 | 12 | noted that the `lambda` format depends on some resources already existing in AWS. There is a `lambda/terraform/`
|
13 | 13 | folder with the Terraform configurations needed to provision them.
|
14 | 14 |
|
15 |
| -## The API |
| 15 | +# API definition |
| 16 | + |
| 17 | +The full definition of the API is found in the openapi.yaml file. A brief summary follows. |
| 18 | + |
| 19 | +## The APIKey API |
| 20 | + |
| 21 | +### Create APIKey |
| 22 | + |
| 23 | +`POST /api-key` |
| 24 | + |
| 25 | +### Activate APIKey |
| 26 | + |
| 27 | +`POST /api-key/activate` |
| 28 | + |
| 29 | +### Rotate APIKey (experimental) |
| 30 | + |
| 31 | +This endpoint has not yet been proven in production use. Proceed at your own risk. |
| 32 | + |
| 33 | +`POST /api-key/rotate` |
| 34 | + |
| 35 | +## The TOTP API |
| 36 | + |
| 37 | +### Required Headers |
| 38 | +1. `x-mfa-apikey` - The API Key |
| 39 | +2. `x-mfa-apisecret` - The API Key Secret |
| 40 | + |
| 41 | +### Create TOTP Passcode |
| 42 | + |
| 43 | +`POST /totp` |
| 44 | + |
| 45 | +### Delete TOTP Passcode |
| 46 | + |
| 47 | +`DELETE /totp/{uuid}` |
| 48 | + |
| 49 | +### Validate TOTP Passcode |
| 50 | + |
| 51 | +`POST /totp/{uuid}/validate` |
| 52 | + |
| 53 | +## The Webauthn API |
16 | 54 | Yes, as you'll see below this API makes heavy use of custom headers for things that seem like they could go into
|
17 | 55 | the request body. We chose to use headers though so that what is sent in the body can be handed off directly
|
18 | 56 | to the WebAuthn library and fit the structures it was expecting without causing any conflicts, etc.
|
@@ -52,3 +90,60 @@ to do with WebAuthn, but is the primary key for finding the right records in Dyn
|
52 | 90 |
|
53 | 91 | ### Delete one of the user's Webauthn credentials
|
54 | 92 | `DELETE /webauthn/credential`
|
| 93 | + |
| 94 | +# Development |
| 95 | + |
| 96 | +## Unit tests |
| 97 | + |
| 98 | +To run unit tests, simply run "make test". It will spin up a Docker Compose environment and run the tests using |
| 99 | +Docker containers for the API and for DynamoDB. |
| 100 | + |
| 101 | +## Manual testing |
| 102 | + |
| 103 | +Unit tests can be run individually, either on the command line or through your IDE. It is also possible to |
| 104 | +test the server and Lambda implementations locally. |
| 105 | + |
| 106 | +### Server |
| 107 | + |
| 108 | +#### HTTP |
| 109 | + |
| 110 | +If HTTPS is not needed, simply start the `app` container and exercise the API using localhost and the Docker port |
| 111 | +defined in docker-compose.yml (currently 8161). |
| 112 | + |
| 113 | +#### HTTPS |
| 114 | + |
| 115 | +To use a "demo UI" that can interact with the API using HTTPS, use Traefik proxy, which is defined in the Docker |
| 116 | +Compose environment. Traefik is a proxy that creates a Let's Encrypt certificate and routes traffic to the local |
| 117 | +container via a registered DNS record. To configure this, define the following variables in `local.env`: |
| 118 | + |
| 119 | +- DNS_PROVIDER=cloudflare |
| 120 | +- CLOUDFLARE_DNS_API_TOKEN=<insert a valid Cloudflare token that has DNS write permission on the domain defined below> |
| 121 | +- LETS_ENCRYPT_EMAIL=<insert your actual email address here> |
| 122 | +- LETS_ENCRYPT_CA=production |
| 123 | +- TLD=<your DNS domain> |
| 124 | +- SANS=mfa-ui.<your domain>,mfa-app.<your domain> |
| 125 | +- BACKEND1_URL=http://ui:80 |
| 126 | +- FRONTEND1_DOMAIN=mfa-ui.<your domain> |
| 127 | +- BACKEND2_URL=http://app:8080 |
| 128 | +- FRONTEND2_DOMAIN=mfa-app.<your domain> |
| 129 | + |
| 130 | +Create DNS A records (without Cloudflare proxy enabled) for the values defined in `FRONTEND1_DOMAIN` and |
| 131 | +`FRONTEND2_DOMAIN` pointing to 127.0.0.1 and wait for DNS propagation. Once all of the above configuration is in place, |
| 132 | +run `make demo`. The first time will take several minutes for all the initialization. You can watch Docker logs on the |
| 133 | +proxy container to keep tabs on the progress. |
| 134 | + |
| 135 | +### Lambda |
| 136 | + |
| 137 | +To exercise the API as it would be used in AWS Lambda, run this command: `air -c .air-cdk.toml`. This will run a |
| 138 | +file watcher that will rebuild the app code and the CDK stack, then run `sam local start-api` using the generated |
| 139 | +Cloudformation template. This will listen on port 8160. Any code changes will trigger a rebuild and SAM will restart |
| 140 | +using the new code. |
| 141 | + |
| 142 | +Implementation notes: |
| 143 | + |
| 144 | +- SAM uses Docker internally, which would make it complicated to run with Docker Compose. |
| 145 | +- You will need to install CDK and SAM on your computer for this to work. |
| 146 | +- It can use the DynamoDB container in Docker Compose, which can be started using `make dbinit`. |
| 147 | +- The `make dbinit` command creates an APIKey (key: `EC7C2E16-5028-432F-8AF2-A79A64CF3BC1` |
| 148 | +secret: `1ED18444-7238-410B-A536-D6C15A3C`) |
| 149 | +- Some unit tests will delete the APIKey created by `make dbinit`. |
0 commit comments