Skip to content

Commit

Permalink
Add fake auth server
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed Jun 12, 2024
1 parent ee8ac76 commit f02750a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ NEGOTIATOR_LOG_LEVEL=DEBUG
SECRET_KEY=2vaR+Kp/SbVUQwZ73Uu7ul3OCCI=
DATABASE_URL=postgresql://host.docker.internal:5432/negotiator_development?user=negotiator&password=negotiator
OPENAI_API_KEY=
CLIENT_ID=
CLIENT_SECRET=
HOST_URL=http://localhost:8081
OAUTH_URL=
USER_INFO_URL=
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export NEGOTIATOR_LOG_LEVEL=DEBUG
export SECRET_KEY=2vaR+Kp/SbVUQwZ73Uu7ul3OCCI=
export DATABASE_URL='postgresql://localhost:5432/negotiator_development?user=negotiator&password=negotiator'
export OPENAI_API_KEY=fill_me_in
export CLIENT_ID=fill_me_in
export CLIENT_SECRET=fill_me_in
export CLIENT_ID=some_client_id
export CLIENT_SECRET=some_client_secret
export HOST_URL=http://localhost:8081
export OAUTH_URL=http://localhost:8999
export USER_INFO_URL=http://localhost:8999/userinfo
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ web-components/test:
negotiator/run:
source venv/bin/activate && python -m negotiator;

.PHONY: fake-auth/run
fake-auth/run:
source venv/bin/activate && python fake_auth_server.py;

.PHONY: migrate
migrate:
source venv/bin/activate && alembic upgrade head
Expand Down
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ This approach allows for the simplicity of server side rendered app with the dyn
npm run build:watch --prefix web-components
```

1. Run the fake oauth server in a separate terminal
```shell
python fake_auth_server.py
```

1. Run the app in a separate terminal
```shell
source .env
poetry run python -m negotiator
python -m negotiator
```

### Running tests
Expand All @@ -53,13 +58,9 @@ poetry run python -m unittest
npm run test --prefix web-components
```

### Running without poetry
### Running with Docker

1. Install dependencies and run via gunicorn
```shell
poetry export --without-hashes --format=requirements.txt > requirements.txt
```

```shell
pip install -r requirements.txt
```
Expand All @@ -69,10 +70,6 @@ npm run test --prefix web-components
```

1. Pack and run via docker
```shell
poetry export --without-hashes --format=requirements.txt > requirements.txt
```

```shell
pack build negotiator --builder=gcr.io/buildpacks/builder:v1
```
Expand Down
42 changes: 42 additions & 0 deletions fake_auth_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from urllib.parse import urlencode

from flask import Flask, request, Response, redirect, jsonify

app = Flask(__name__)


@app.get("/auth")
def auth():
state = request.args['state']
client_id = request.args['client_id']
redirect_uri = request.args['redirect_uri']

if client_id != 'some_client_id':
return Response("Unauthorized", status=401)

query_string = urlencode({'state': state, 'code': 'some_code'})

return redirect(f'{redirect_uri}?{query_string}')


@app.post("/token")
def access_token():
if (request.form['client_id'] != 'some_client_id'
or request.form['client_secret'] != 'some_client_secret'
or request.form['code'] != 'some_code'):
return Response("Unauthorized", status=401)

return jsonify({"access_token": "some_access_token"})


@app.get("/userinfo")
def userinfo():
auth_header = request.headers["Authorization"]

if auth_header != "Bearer some_access_token":
return Response("Unauthorized", status=401)

return jsonify({"email": "[email protected]"})


app.run(debug=True, host="0.0.0.0", port=8999)

0 comments on commit f02750a

Please sign in to comment.