Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/overhaul #1

Merged
merged 36 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4deec73
Replaced virtualenv with Pipenv
MasterKale Sep 24, 2018
7d21eb9
Moved source code into src/
MasterKale Sep 24, 2018
07d3305
Removed test script
MasterKale Sep 24, 2018
f1ede78
Switched to using `os` for environment vars
MasterKale Sep 24, 2018
58d1b18
Updated README
MasterKale Sep 24, 2018
335515a
Enable Flake8 linting support
MasterKale Sep 25, 2018
d40ecd3
Add type annotations to helpers.py
MasterKale Sep 25, 2018
052f987
Add unit tests for get_hash and get_json helpers
MasterKale Sep 25, 2018
3bc5eae
Add initial server tests cases
MasterKale Oct 10, 2018
36443d9
Add .editorconfig
MasterKale Oct 10, 2018
e24d73b
Add start script in Pipfile
MasterKale Oct 10, 2018
86fcfbf
Update server to use Redis URL for connection info
MasterKale Oct 10, 2018
6598d86
Add flake8-quotes
Oct 18, 2018
43609be
Fix lint issues in unit tests
Oct 18, 2018
5eefde2
Add tests for get_username
Oct 18, 2018
47095af
Lint fixes for server tests
Oct 18, 2018
4330b59
Add mockredispy for testing
Oct 18, 2018
cdc81e8
Patch Redis with mock instance when testing
Oct 18, 2018
a5957e7
Add test coverage reporting
MasterKale Nov 1, 2018
79e236d
Finish GenerateHashResource testing
Nov 5, 2018
3762f17
Enforce string type for hash response
Nov 5, 2018
dda66d7
Pull in mockredispy to patch manually
Nov 5, 2018
d8acbf9
Remove cast to string due to bad mock library
Nov 5, 2018
69de0a4
Add ProfileMock for profile validation
Nov 5, 2018
2316f57
Add test cases for hash validation
Nov 5, 2018
db4e201
Move coverage library to dev dependencies
Nov 5, 2018
f18cd40
Upgrade to Falcon v1.4.1
Nov 5, 2018
eed5fcc
Remove redundant middleware
Nov 5, 2018
2eeb1a9
Update unit tests to use new Falcon testing
Nov 5, 2018
0575647
Restore JSON request middleware
Nov 6, 2018
3716745
Switch to HTTP 200 instead of OK
Nov 6, 2018
7a7bea8
Exclude mockredis from lint check
Nov 6, 2018
a6e558c
Finalize unit tests
Nov 6, 2018
4e63e46
Add production start command
Nov 6, 2018
dc8ed31
Refresh README
Nov 6, 2018
c698769
Switch to double-quotes
Nov 6, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[run]
omit=
*/site-packages/*
*/distutils/*
*/src/tests/*
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{py,md}]
indent_size = 4
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-line-length=99
inline-quotes="
exclude=
src/tests/mockredis
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ bin/
include/
lib/
pip-selfcheck.json
local_settings.py
local_settings.py
.DS_Store
.coverage
htmlcov
.env
29 changes: 29 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[scripts]
test = "python -m unittest discover -s src/tests"
test-coverage = "coverage run -m unittest discover -s src/tests"
test-report = "coverage report"
test-report-html = "coverage html"
lint = "flake8"
start = "gunicorn src.server:app --reload"
start-prod = "gunicorn src.server:app"

[packages]
falcon = "*"
gunicorn = "*"
python-mimeparse = "*"
redis = "*"
requests = "*"
six = "*"

[dev-packages]
flake8 = "*"
flake8-quotes = "*"
coverage = "*"

[requires]
python_version = "3.6"
173 changes: 173 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 39 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@

GoonAuth2 is a REST API service that can be used to authorize membership in the Something Is Awful internet forum.

## Requirements

- Pipenv
- Python3 (v3.6+)
- Redis (v5.0.0+)

## Installation

The service is powered by **Python 3** (v3.4.3) and **Redis** (v2.8.4). Python dependencies include **Falcon**, **redis-py**, **requests**, and **gunicorn**. You can install all of these dependencies via the included `requirements.txt`:
Install dependencies with **Pipenv** via the included **Pipfile**:

```sh
$> pipenv install
```

A few environment variables can be set within a **.env** file (placed in the root of this project) to customize functionality:

- `REDIS_URL`
- **String** in the following format: `redis://[username]:[password]@[hostname]:6379`
- **Default:** "" (will attempt to connect to localhost:6379 without a username or password)
- `HASH_LIFESPAN_MINS`
- **Number** of minutes a hash is good for
- **Default:** 5

virtualenv .
pip install -r requirements.txt
The only things stored in the database are short-lived `key:value` pairs that automatically expire in `HASH_LIFESPAN_MINS * 60` seconds.

There are a couple of values you'll need to update before the server will work. First, update `REDIS_HOST`, `REDIS_PORT`, and `REDIS_DB_NUM` to point to whatever Redis server you want to use. The only things stored in the database are short-lived `key:value` pairs that automatically expire in `HASH_LIFESPAN_MINS * 60` seconds.
The following values will also need to be set so that the server can access SA profiles:

You'll also want to set values for the following strings: `COOKIE_SESSIONID`, `COOKIE_SESSIONHASH`, `COOKIE_BBUSERID`, and `COOKIE_BBPASSWORD`. I opted to create an accompanying `local_settings.py` file and define them within that, but feel free to specify them as you wish.
- `COOKIE_SESSIONID`
- `COOKIE_SESSIONHASH`
- `COOKIE_BBUSERID`
- `COOKIE_BBPASSWORD`.

These four values need to be taken from an existing logged-in user's cookies:

![Something Is Awful Cookies](./docs/somethingisawful_cookies.png)

Once everything is in place, you can start the server using `gunicorn`:

gunicorn server:app
```sh
$> pipenv run start-prod
```

## Usage

Expand All @@ -29,9 +52,11 @@ POST to `/v1/generate_hash/` with a JSON-encoded payload containing a `username`

The returned payload will contain a `hash` key with a random 32-character alphanumeric value:

{
"hash": "hMPAtkx6xIEtVfqqP0X9bvEG8lU4Yypb"
}
```json
{
"hash": "hMPAtkx6xIEtVfqqP0X9bvEG8lU4Yypb"
}
```

The hash will expire after **5 minutes** but can easily be re-generated after expiration by re-submitting the above request.

Expand All @@ -49,6 +74,8 @@ Once the hash is in-place, POST a request to `/v1/validate_user/` with a JSON-en

The returned payload will contain a `validated` key with a `boolean` value of whether or not the hash was detected :

{
"validated": true
}
```json
{
"validated": true
}
```
45 changes: 0 additions & 45 deletions helpers.py

This file was deleted.

6 changes: 0 additions & 6 deletions requirements.txt

This file was deleted.

Empty file added src/__init__.py
Empty file.
Loading