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

Minify & Refactor Code 🚀 #37

Merged
merged 13 commits into from
Nov 30, 2021
32 changes: 27 additions & 5 deletions .github/workflows/package.yaml → .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
name: Package EasyAuth
name: Build EasyAuth

on:
push:
branches:
- main
branches: ["main"]
pull_request:
branches: ["main"]

jobs:

linter:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Lint
run: pre-commit run --all-files

# Label of the container job
test-easyauth:
# Containers must run in Linux based operating systems
Expand All @@ -12,7 +35,7 @@ jobs:
#container: joshjamison/python38:latest
strategy:
matrix:
python-version: [3.7,3.8,3.9]
python-version: [3.7,3.8,3.9,3.10.0]
steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
Expand All @@ -25,7 +48,6 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install -r requirements-dev.txt
- name: Setup Test Configuration
run: |
Expand Down
43 changes: 0 additions & 43 deletions .github/workflows/main.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/pypi-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand Down
39 changes: 39 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-merge-conflict
- id: check-added-large-files
- id: check-ast
- id: check-symlinks
- id: trailing-whitespace
- id: check-json
- id: debug-statements
- id: pretty-format-json
args: ["--autofix"]
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-print]
files: '\.py$'
exclude: docs/
args:
- --select=F403,F406,T003
- repo: https://github.com/humitos/mirrors-autoflake
rev: v1.3
hooks:
- id: autoflake
files: '\.py$'
exclude: '^\..*'
args: ["--in-place"]
- repo: https://github.com/psf/black
rev: 21.7b0
hooks:
- id: black
args: ["--target-version", "py38"]
38 changes: 23 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
![](./images/logo_t.png)
<br>
#
Create a centralized Authentication and Authorization token server. Easily secure FastAPI endpoints based on Users, Groups, Roles or Permissions to minimize database access requirements of Auth.

---

Create a centralized Authentication and Authorization token server. Easily secure FastAPI endpoints based on Users, Groups, Roles or Permissions to minimize database access requirements of Auth.

[![Documentation Status](https://readthedocs.org/projects/easyauth/badge/?version=latest)](https://easyauth.readthedocs.io/en/latest/?badge=latest) [![PyPI version](https://badge.fury.io/py/easy-auth.svg)](https://pypi.org/project/easy-auth/)

<h2>Documentation</h1>
<h2>Documentation</h1>

[https://easyauth.readthedocs.io/en/latest/](https://easyauth.readthedocs.io/en/latest/)

## Key Features

- Centralized Auth - Single location for Users & Permissions to share across apps
- Granular Endpoint Security - Verify user identity, and define explicitly who and what each user or groups of users may access
- Admin GUI - easy management of users, permissions, tokens, oauth and more!
- Advanced JWT - Token Based Client authorization with built in invalidation capabilities
- Google Oauth - Easy to configure google login
- Integrated Login & Cookie Management - Users are not just authenticated and authorized, they are re-directed on token expiration to login pages via cookie system and sent back to last location afterwards
- Integrated Login & Cookie Management - Users are not just authenticated and authorized, they are re-directed on token expiration to login pages via cookie system and sent back to last location afterwards

## Quick Start

```bash

$ virtualenv -p <python3.X> easy-auth-env
$ source easy-auth-env/bin/activate

(easy-auth) $ pip install easy-auth[server]
(easy-auth) $ pip install easy-auth[server]

(easy-auth) $ pip install easy-auth[client] # without db
(easy-auth) $ pip install easy-auth[client] # without db

```
## Basic Server

## Basic Server

Configure require env variables via a .json

```Bash
$ cat > server_env.json <<EOF
{
Expand All @@ -56,7 +62,7 @@ server = FastAPI()
@server.on_event('startup')
async def startup():
server.auth = await EasyAuthServer.create(
server,
server,
'/auth/token',
auth_secret='abcd1234',
admin_title='EasyAuth - Company',
Expand All @@ -65,14 +71,15 @@ async def startup():
)

```

Start Sever

```bash
$ uvicorn --host 0.0.0.0 --port 8330 test_server:server
uvicorn --host 0.0.0.0 --port 8330 test_server:server
```

## Basic Client


```python
#test_client.py
from fastapi import FastAPI
Expand Down Expand Up @@ -100,13 +107,13 @@ async def startup():
@server.auth.get('/', users=['jane'])
async def root():
return f"I am root"

# grants access to members of 'users' or 'admins' group.
@server.auth.get('/groups', groups=['users', 'admins'])
async def groups():
return f"I am groups"
# grants access to all members of 'users' group

# grants access to all members of 'users' group
# or a groups with role of 'basic' or advanced
@server.auth.get('/roles', roles=['basic', 'advanced'], groups=['users'])
async def roles():
Expand All @@ -117,20 +124,21 @@ async def startup():
async def action():
return f"I am actions"
```

![](docs/images/login.png)

## Server

## Server
<h3>See 0.0.0.0:8330/docs </h3>

![](docs/images/api/api.png)

### GUI

![](docs/images/admin_gui.png)

## Client

![](images/client.png)

![](images/OAuth.png)

24 changes: 13 additions & 11 deletions docker/server/server.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import os

from fastapi import FastAPI

from easyauth.server import EasyAuthServer

server = FastAPI()

AUTH_SECRET = os.environ.get('AUTH_SECRET')
ADMIN_TITLE = os.environ.get('ADMIN_TITLE')
ADMIN_PREFIX = os.environ.get('ADMIN_PREFIX')
AUTH_SECRET = os.environ.get("AUTH_SECRET")
ADMIN_TITLE = os.environ.get("ADMIN_TITLE")
ADMIN_PREFIX = os.environ.get("ADMIN_PREFIX")

if not AUTH_SECRET:
AUTH_SECRET = 'abcd1234'
AUTH_SECRET = "abcd1234"

if not ADMIN_TITLE:
ADMIN_TITLE = 'EasyAuth - Example'
ADMIN_TITLE = "EasyAuth - Example"

if not ADMIN_PREFIX:
ADMIN_PREFIX = '/admin'
ADMIN_PREFIX = "/admin"


@server.on_event('startup')
@server.on_event("startup")
async def startup():
server.auth = await EasyAuthServer.create(
server,
'/auth/token',
server,
"/auth/token",
auth_secret=AUTH_SECRET,
admin_title=ADMIN_TITLE,
admin_prefix=ADMIN_PREFIX
)
admin_prefix=ADMIN_PREFIX,
)
16 changes: 8 additions & 8 deletions docs/client_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ async def startup():
@server.auth.get('/', users=['jane'])
async def root():
return f"I am root"

# grants access to members of 'users' or 'admins' group.
@server.auth.get('/groups', groups=['users', 'admins'])
async def groups():
return f"I am groups"
# grants access to all members of 'users' group

# grants access to all members of 'users' group
# or a groups with role of 'basic' or advanced
@server.auth.get('/roles', roles=['basic', 'advanced'], groups=['users'])
async def roles():
Expand All @@ -50,10 +50,10 @@ async def startup():
{'groups': ['administrators']}

### APIRouter
FastAPI provides a [APIRouter](https://fastapi.tiangolo.com/tutorial/bigger-applications/?h=apirouter#apirouter) object for defining path prefixes, pre-defined dependencies, see fastapi docs for more details. EasyAuthClient can extend the main FastAPI router using the .create_api_router() method or EasyAuthAPIRouter.create().
FastAPI provides a [APIRouter](https://fastapi.tiangolo.com/tutorial/bigger-applications/?h=apirouter#apirouter) object for defining path prefixes, pre-defined dependencies, see fastapi docs for more details. EasyAuthClient can extend the main FastAPI router using the .create_api_router() method or EasyAuthAPIRouter.create().

!!! Important - "EasyAuthAPIRouter Considerations "
EasyAuthAPIRouter should be created after an `EasyAuthClient` or `EasyAuthServer` is created to ensure that the router are correctly included and visible in OpenAPI schema.
EasyAuthAPIRouter should be created after an `EasyAuthClient` or `EasyAuthServer` is created to ensure that the router are correctly included and visible in OpenAPI schema.

```python
from fastapi import FastAPI, Request, Depends
Expand Down Expand Up @@ -116,13 +116,13 @@ async def finance_data():
![](images/easyauthclient-apirouter.png)

### Permissions
EasyAuth allows endpoints to be as exclusive or as inclusive as needed. Authorization is granted if user meets at least 1 condition.
EasyAuth allows endpoints to be as exclusive or as inclusive as needed. Authorization is granted if user meets at least 1 condition.
```python
@server.auth.get(
'/roles',
'/roles',
roles=['basic'], # OR
groups=['users'], # OR
actions=['CREATE_BASIC']
actions=['CREATE_BASIC']
)
```

Expand Down
2 changes: 1 addition & 1 deletion docs/contribute.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Idea / Suggestion / Issue
- Submit an Issue
- Create a Pull request
- Create a Pull request

[https://github.com/codemation/easyauth](https://github.com/codemation/easyauth)
2 changes: 1 addition & 1 deletion docs/databases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Database Usage
!!! TIP
!!! TIP
The default 'batteries mostly included' implementation utilizes a sqlite database managed by aiopyql.

!!! INFO "Supported Databases"
Expand Down
6 changes: 4 additions & 2 deletions docs/import_export.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
##

### Importing - Restore
Pre-existing / Backup configuration can be added via Import Auth Config, or existing entries will be updated.

Pre-existing / Backup configuration can be added via Import Auth Config, or existing entries will be updated.

!!! TIP "Auth Dependencies"
Dependencies are added first, and missing dependencies will be indicated
Expand All @@ -12,7 +13,8 @@ Pre-existing / Backup configuration can be added via Import Auth Config, or exis
![](./images/import-export.png)

### Exporting - Backup
Auth Configuration can be exported to JSON to quickly review all configuration, save as a backup, restore in a new location / database.

Auth Configuration can be exported to JSON to quickly review all configuration, save as a backup, restore in a new location / database.

!!! TIP "Exporting Configuration"
Export exisiting configuration to a JSON file containing all configuers users, groups, roles & permissions.
Expand Down
Loading