5
0
Fork 0
backend-login/README.md

160 lines
3.7 KiB
Markdown
Raw Permalink Normal View History

2019-02-10 14:52:22 +08:00
# backend-login
2019-02-24 12:01:02 +08:00
Beep backend handling login. Call `/init` and then `/verify` in sequence. `/login` is legacy to provide an easy source of tokens for testing, and will be removed someday™.
2019-02-10 18:43:02 +08:00
2019-02-18 23:43:24 +08:00
## Environment variables
Supply environment variables by either exporting them or editing ```.env```.
| ENV | Description | Default |
| ---- | ----------- | ------- |
| LISTEN | Host and port number to listen on | :8080 |
| SECRET | JWT secret | secret |
2019-02-24 12:01:02 +08:00
## API
| Contents |
| -------- |
| Init Auth |
| Verify Code |
| Create Token (temporary) |
| Register User |
---
2019-02-24 12:01:02 +08:00
### Init Auth
```
POST /init
```
Kick off SMS verification process.
#### Body
| Name | Type | Description |
| ---- | ---- | ----------- |
| phone_number | String | Verifying phone number in format `<country code><8 digits>`. |
#### Success (200 OK)
A nonce, to be used for `/verify` to add additional entropy.
#### Errors
| Code | Description |
| ---- | ----------- |
| 400 | Error parsing body/phone_number is not a valid phone number |
| 500 | Error generating nonce/Making request to Twilio SMS |
---
### Verify Code
```
POST /verify
```
Second half of the verification process, verifying the code and returning a JWT. If the user does not exist in the database, a blank one is created.
#### Body
| Name | Type | Description |
| ---- | ---- | ----------- |
| code | String | Verification code received by SMS. |
| nonce | String | Nonce returned by `/init`. |
| clientid | String | ID unique to device, e.g. MAC Address |
#### Success (200 OK)
JWT token.
```json
{
"userid": "<userid>",
"clientid": "<clientid>"
}
```
#### Errors
| Code | Description |
| ---- | ----------- |
| 400 | Error parsing body |
| 404 | Code with nonce supplied was not found |
| 500 | Error retrieving record from Redis/querying postgres/creating user ID/generating token |
---
### Create Token (temporary)
2019-02-10 18:43:02 +08:00
```
POST /login
```
2019-02-24 12:01:02 +08:00
Just a simple little endpoint to get a valid token without having to jump through the (expensive) hoops of SMS Authentication.
#### Body
2019-02-10 18:43:02 +08:00
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
2019-02-24 12:01:02 +08:00
| userid | String | User's ID. | ✓ |
| clientid | String | Device's ID. Must be unique to the device. I suggest something based on MAC address. | ✓ |
2019-02-10 18:43:02 +08:00
2019-02-24 12:01:02 +08:00
#### Success (200 OK)
2019-02-10 18:43:02 +08:00
JWT token.
2019-02-24 12:01:02 +08:00
#### Errors
2019-02-10 18:43:02 +08:00
| Code | Description |
| ---- | ----------- |
| 400 | Required fields in body were not supplied |
| 500 | Error creating the JWT |
---
### Register User
```
2019-06-16 13:39:54 +08:00
POST /register/:code/:nonce
```
2019-06-16 13:39:54 +08:00
Register a new user. Proxies `core`'s CreateUser endpoint, adding in a dummy token. Admittedly not the most secure implementation ever, but sue me it's 3AM now. Requires a code and nonce supplied from querying the `/init` endpoint.
#### Params
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| code | String | OTP code SMS-ed to the user. Initiated with the `/init` endpoint. | ✓ |
| nonce | String | Nonce returned by the `/init` endpoint response. | ✓ |
#### Body
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| first_name | String | First name of the added user. | ✓ |
| last_name | String | Last name of the added user. | ✓ |
| phone_number | String | Phone number of the added user. Shouldn't be needed but makes life easier. | X |
#### Success Response (200 OK)
Created user object.
```json
{
"id": "<id>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
}
```
#### Errors
| Code | Description |
| ---- | ----------- |
| 400 | Error parsing submitted body, or fields first_name or last_name have a length of 0 |
2019-06-16 13:39:54 +08:00
| 401 | Supplied OTP is invalid |
| 500 | Error occurred inserting entry into database/proxying |