Skip to main content

A powerful, simple, and async security library for Sanic.

Project description

Contributors Forks Stargazers Issues Conda Downloads Code style: black


Sanic Security

A powerful, simple, and async security library for Sanic.
Documentation · Report Bug · Request Feature

Table of Contents

About The Project

Sanic Security is an authentication, authorization, and verification library designed for use with Sanic. This library contains a variety of features including:

  • Simple login, registration, and authentication
  • Text and email two-step verification
  • Two-factor authentication
  • Captcha
  • JWT
  • Wildcard and role based authorization
  • Blueprint

This repository has been starred by Sanic's core maintainer:

aphopkins

Getting Started

In order to get started, please install pip.

Prerequisites

  • pip
sudo apt-get install python3-pip

Installation

  • Install pip packages
pip3 install sanic-security

Usage

Once Sanic Security is configured and good to go, implementation is easy.

Initial Setup

First you have to create a configuration file called security.ini in the working directory. Below is an example of its contents:

[SECURITY]
secret=05jF8cSMAdjlXcXeS2ZJUHg7Tbyu
captcha_font=captcha.ttf
cache_path = ./resources/security-cache

[BLUEPRINT]
register_route=api/auth/register
login_route=api/auth/login
verify_route=api/auth/verify
logout_route=api/auth/logout
captcha_request_route=api/capt/request
captcha_img_route=api/capt/img

[TORTOISE]
username=admin
password=8UVbijLUGYfUtItAi
endpoint=example.cweAenuBY6b.us-north-1.rds.amazonaws.com
schema=exampleschema
models=sanic_security.models, example.models
engine=mysql
generate=true

[TWILIO]
from=12058469963
token=1bcioi878ygO8fi766Fb34750e82a5ab
sid=AC6156Jg67OOYe75c26dgtoTICifIe51cbf

[SMTP]
host=smtp.gmail.com
port=465
from=test@gmail.com
username=test@gmail.com
password=wfrfouwiurhwlnj
tls=true
start_tls=false

You may remove each section in the configuration you aren't using. For example, if you're not utilizing Twillio you can delete the TWILLIO section.

Once you've configured Sanic Security, you can initialize Sanic with the example below:

initialize_security_orm(app)
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)

All request bodies must be sent as form-data. The tables in the below examples represent example request form data.

Authentication

  • Registration

Phone can be null or empty.

Key Value
username test
email test@test.com
phone 19811354186
password testpass
captcha Aj8HgD
@app.post("api/auth/register")
@requires_captcha()
async def on_register(request, captcha_session):
    account = await register(request)
    two_step_session = await request_two_step_verification(request, account)
    await two_step_session.text_code() # Text verification code.
    await two_step_session.email_code() # Or email verification code.
    response = json("Registration successful!", two_step_session.account.json())
    two_step_session.encode(response)
    return response
  • Verify Account
Key Value
code G8ha9nVae
@app.post("api/auth/verify")
async def on_verify(request):
    two_step_session = await verify_account(request)
    return json("You have verified your account and may login!", two_step_session.account.json())
  • Login
Key Value
email test@test.com
password testpass
@app.post("api/auth/login")
async def on_login(request):
    authentication_session = await login(request)
    response = json("Login successful!", authentication_session.account.json())
    authentication_session.encode(response)
    return response
  • Login (With two-factor authentication)
Key Value
email test@test.com
password testpass
@app.post("api/auth/login")
async def on_two_factor_login(request):
    authentication_session = await login(request, two_factor=True)
    two_step_session = await request_two_step_verification(request, authentication_session.account)
    await two_step_session.text_code() # Text verification code.
    await two_step_session.email_code() # Or email verification code.
    response = json("Login successful! A second factor is now required to be authenticated.", authentication_session.account.json())
    authentication_session.encode(response)
    two_step_session.encode(response)
    return response
  • Second Factor
Key Value
code G8ha9nVae
@app.post("api/auth/login/second-factor")
@requires_two_step_verification()
async def on_second_factor(request, two_step_verification):
  authentication_session = await on_second_factor(request)
  response = json("Second factor attempt successful! You may now be authenticated!",
                  authentication_session.account.json())
  return response
  • Logout
@app.post("api/auth/logout")
@requires_authentication()
async def on_logout(request, authentication_session):
    await logout(authentication_session)
    response = json("Logout successful!", authentication_session.account.json())
    return response
  • Requires Authentication
@app.get("api/auth/authenticate")
@requires_authentication()
async def on_authenticated(request, authentication_session):
    return json(f"Hello {authentication_session.account.username}! You have been authenticated.", 
                authentication_session.account.json())

Captcha

You must download a .ttf font for captcha challenges and define the file's path in security.ini.

1001 Free Fonts

Recommended Font

Captcha challenge example:

Captcha example

  • Request Captcha
@app.post("api/captcha/request")
async def on_request_captcha(request):
    captcha_session = await request_captcha(request)
    response = json("Captcha request successful!", captcha_session.json())
    captcha_session.encode(response)
    return response
  • Captcha Image
@app.get("api/captcha/img")
async def on_captcha_img(request):
    captcha_session = await CaptchaSession.decode(request)
    return await captcha_session.get_image()
  • Requires Captcha
Key Value
captcha Aj8HgD
@app.post("api/captcha/attempt")
@requires_captcha()
async def on_captcha_attempt(request, captcha_session):
    return json("Captcha attempt successful!", captcha_session.json())

Two-step Verification

  • Request Two-step Verification
Key Value
email test@test.com
captcha Aj8HgD
@app.post("api/verification/request")
@requires_captcha()
async def on_request_verification(request, captcha_session):
    two_step_session = await request_two_step_verification(request)
    await two_step_session.text_code() # Text verification code.
    await two_step_session.email_code() # Or email verification code.
    response = json("Verification request successful!", two_step_session.account.json())
    two_step_session.encode(response)
    return response
  • Resend Two-step Verification Code
@app.post("api/verification/resend")
async def on_resend_verification(request):
    two_step_session = await TwoStepSession.decode(request)
    await two_step_session.text_code() # Text verification code.
    await two_step_session.email_code() # Or email verification code.
    return json("Verification code resend successful!", two_step_session.account.json())
  • Requires Two-step Verification
Key Value
code G8ha9nVa
@app.post("api/verification/attempt")
@requires_two_step_verification()
async def on_verified(request, two_step_session):
    response = json("Two-step verification attempt successful!", two_step_session.account.json())
    return response

Authorization

Sanic Security comes with two protocols for authorization: role based and wildcard based permissions.

Role-based access control (RBAC) is a policy-neutral access-control mechanism defined around roles and privileges.

Wildcard permissions support the concept of multiple levels or parts. For example, you could grant a user the permission printer:query. The colon in this example is a special character used to delimit the next part in the permission string. In this example, the first part is the domain that is being operated on (printer), and the second part is the action (query) being performed. This concept was inspired by Apache Shiro's implementation of wildcard based permissions.

Examples of wildcard permissions are:

admin:add,update,delete
admin:add
admin:*
employee:add,delete
employee:delete
employee:*
  • Require Permissions
@app.post("api/auth/perms")
@require_permissions("admin:update", "employee:add")
async def on_require_perms(request, authentication_session):
    return text("Account permitted.")
  • Require Roles
@app.post("api/auth/roles")
@require_roles("Admin", "Moderator")
async def on_require_roles(request, authentication_session):
    return text("Account permitted.")

Blueprint

The Sanic Security blueprint contains endpoints that allow you to employ fundamental authentication and verification into your application with a single line of code.

  • Implementation
app.blueprint(security)
  • Endpoints

Endpoints are configured via security.ini file.

Method Endpoint Info
POST api/auth/register A captcha is required. Register an account with an email, username, and password. Once the account is created successfully, a two-step session is requested and the code is emailed.
POST api/auth/login Login with an email and password. A two-step session is requested when the account is not verified and the code is emailed.
POST api/auth/verify Verify account with a two-step session code found in email.
POST api/auth/logout Logout of logged in account.
POST api/capt/request Requests new captcha session.
GET api/capt/img Retrieves captcha image from existing captcha session.

Testing

Make sure the test Sanic instance (test/server.py) is running on your machine as both postman, and the unit tests operate as a test client.

Then run the unit tests (test/tests.py) or test with postman via clicking the button below.

Run in Postman

Tortoise

Sanic Security uses Tortoise ORM for database operations.

Tortoise ORM is an easy-to-use asyncio ORM (Object Relational Mapper).

  • Define your models like so:
from tortoise.models import Model
from tortoise import fields

class Tournament(Model):
    id = fields.IntField(pk=True)
    name = fields.TextField()
  • Use it like so:
# Create instance by save
tournament = Tournament(name='New Tournament')
await tournament.save()

# Or by .create()
await Tournament.create(name='Another Tournament')

# Now search for a record
tour = await Tournament.filter(name__contains='Another').first()
print(tour.name)

Roadmap

Keep up with Sanic Security's Trello board for a list of proposed features, known issues, and in progress development.

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the GNU General Public License v3.0. See LICENSE for more information.

Versioning

0.0.0.0

Major.Minor.Revision.Patch

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic-security-0.10.9.6.tar.gz (36.4 kB view details)

Uploaded Source

File details

Details for the file sanic-security-0.10.9.6.tar.gz.

File metadata

  • Download URL: sanic-security-0.10.9.6.tar.gz
  • Upload date:
  • Size: 36.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for sanic-security-0.10.9.6.tar.gz
Algorithm Hash digest
SHA256 538efe63c65b8f07b31c83970f297f832cddc8a0ae5529a68d6017faae5e54a5
MD5 c7f99e9076da6c21bbded0673c56677d
BLAKE2b-256 8bbf13e11059d9435074ee70ed18d83bd68d069a5c452f0123a79650b97159b1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page