Skip to content

Commit

Permalink
CORS support (#5)
Browse files Browse the repository at this point in the history
Support for CORS has been added in main module.
  - all origin is allowed (*)
  - all methods are allowed (*)
  - LOGIN route now only POST requests

Signed-off-by: Veljko Tekelerović <[email protected]>
  • Loading branch information
vexy authored Mar 29, 2020
1 parent 817db4f commit 5f7def9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
This repository represents the source code template for micro webserver that provides authentication gate for your protected resources.

It is written in `Python` using `Flask` framework and relies on `JWT` authentication mechanism.
Some of the provided strategies are to basic/simple for **serious**, production level webserver. Use this template as starting point for more complex projects and requirements.
Some of the provided strategies are to basic/simple for **serious**, production level webserver.
Use this template as starting point for more complex projects and requirements.

### JWT based
`JSON Web Tokens` - or [JWT](https://jwt.io/) in short - is the foundation authentication principle used in this template.
Expand All @@ -18,9 +19,12 @@ Template is designed to support modular structure. Main application modules are
### Different authentication strategies
Presented here is basic HTTP AUTHENTICATION through Authentication field. Note there are **way secure** authentication mechanisms, such as `OAuth`.

#### CORS setup
For the sake of simplicity, CORS has been enabled completely. Server will accept all origins no matter where the request comes from. Check and/or modify `@app.after_request` directive to further customise desired behaviour (lines [20-24](https://github.com/vexy/flask-auth-template/blob/master/main-module.py#L20-L24) in `main-module.py`).

### Installation
Before you begin:
```
```bash
git clone
cd flask-auth-template
```
Expand Down
2 changes: 1 addition & 1 deletion install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# pip packages
echo "Installing needed modules via pip..."
echo -e "\e[5mInstalling project dependencies\e[0m"
pip3 install -r requirements.txt
7 changes: 7 additions & 0 deletions main-module.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
app.register_blueprint(authRoute)
app.register_blueprint(protectedRoute)

# make sure this is turned off
@app.after_request
def attachCORSHeader(response):
response.headers.set('Access-Control-Allow-Headers', '*')
response.headers.set('Access-Control-Allow-Origin', '*')
return response

# Publicly accessible routes
# ------------------------------
@app.route('/')
Expand Down
2 changes: 1 addition & 1 deletion modules/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# public blueprint exposure
authRoute = Blueprint('auth', __name__)

@authRoute.route('/login')
@authRoute.route('/login', methods=['POST'])
def login():
# get authorization field from HTTP request, early exit if it's not present
auth = request.authorization
Expand Down

0 comments on commit 5f7def9

Please sign in to comment.