Awesome SSO
A library designed to host common components for a cluster of microservices sharing a single sign on.
Feature
- A common exception class, supporting both status code and custom error code to map to more detailed error message or serve as i18n key.
- A common FastAPI app for interaction with service, like login ,registration and unregistration.
- a connector for minio object store.
- a connector for beanie, a mongo odm compatible with pydantic.
Usage
Installation
pip install awesome-sso
Exceptions
Using fast API as example, we may simply throw exception with a proper status code, and an optional error code. We may also supply arbitrary key value in args dict, to help frontend render better error message.
from awesome_sso.exceptions import NotFound
from fastapi import APIRouter
router = APIRouter()
@router.get('/transactions')
def get(id: str):
try:
obj = find_by_id(id)
except Exception as e:
raise NotFound(message='transaction not found' % id, error_code='A0001', args={id: id})
...
And we may implement a common error handler to convert all these errors to proper response schema
from awesome_sso.exceptions import HTTPException
from fastapi.requests import Request
from fastapi.responses import JSONResponse
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={
'detail': exc.detail,
'error_code': exc.error_code,
}
)
This would result in a response with status code 404, and body
{
"status_code": 404,
"detail": {
"message": "transaction not found",
"id": "some_id"
},
"error_code": "A0001"
}
With this response, frontend can decide to simply render detail, or map it to detailed message. If error_code "A0001" correspond to the following i18 n entry
"error.A0001": {"en-US": "transaction can not be found with supplied {id}: {message}"}
we may format message accordingly with
errorMessage = formatMessage({ id: `error.${error.data.error_code}` }, error.data.detail);
Note that error code is not supplied, is default to status code. So it is always safe to simply use error_code in frontend to decide what to render.
Data Store
Minio
refer to tests/test_minio.py
Mongo
refer to tests/service/test_user.py
from beanie import init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from awesome_sso.service.user.schema import AwesomeUser
def init_mongo():
settings = YOUR_SETTINGS()
models = [AwesomeUser]
cli = AsyncIOMotorClient(settings.mongodb_dsn)
await init_beanie(
database=cli[settings.mongodb_db_name],
document_models=models,
)
for model in models:
await model.get_motor_collection().drop()
await model.get_motor_collection().drop_indexes()
Service
configure service settings
from awesome_sso.service.settings import Settings
settings = Settings()
settings.init_app(
symmetric_key='YOUR_SYMMETRIC_KEY', # to encode and decode service token
public_key='YOUR_PUBLIC_KEY', # to decode the token signed by sso
user_model=USER_MODEL, # user orm needs to inherit AwesomeUser from `awesome_sso.user.schema`
service_name='YOUR_SERVICE_NAME', # for service discovery, to recognize service
sso_domain='YOUR_SSO_DOMAIN', # for service registration and sync user
)
initial service and mount to your application
from awesome_sso.service import Service
from fastapi import FastAPI
app = FastAPI()
service = Service()
service.init_app(YOUR_FASTAPI_APP)
app.mount('/YOUR/PATH', YOUR_FASTAPI_APP)
then open the api doc, you will see the apis in awesome_sso.service.user.route
Development
Installing Poetry
- create your own environment for poetry, and simply run:
pip install poetry - alternatively, you can refer to poetry's official page
- to be able to use
poedirectly,pip install poethepoet
Contributing
- project setup:
poetry install - create your own branch to start developing new feature.
- before creating pr, make sure you pass
poe lintand./run_test.sh.- what happened inside
./run_test.shis that a minio server is setup for you temporarily, and teardown and unit test is finished. - notice that
poe testwould also work if you already have a minio up and running. You need the following env variable:MINIO_ACCESS_KEY,MINIO_SECRET_KEY,MINIO_ADDRESSupon runningpoe test.
- what happened inside
- for a list of available poe command,
poe - after you submit a pr, you should check if pipeline is successful.
Releasing
poetry version [new_version]git commit -m"Bump version"git push origin develop- create new release on github.
- Create release off develop branch, auto generate notes, and review release note.
- Publish release
Metadata
Release files for awesome-sso 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| awesome-sso-0.6.0.tar.gz | 17.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| awesome_sso-0.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.1 kB
Release files / awesome-sso-0.6.0.tar.gz
| Download URL | awesome-sso-0.6.0.tar.gz |
|---|---|
| Size | 17.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
438ff4a0607c73214c626ec7bed82bfc32c46d348c03382ac49446e541c6f775
|
|
BLAKE2b-256 checksum How to use checksums |
9b5f7ef9d2af77f98653df341d4303cfdab4bf8ffdb69252306a95ddcde353eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.8.12 Darwin/20.6.0
|
Release files / awesome_sso-0.6.0-py3-none-any.whl
| Download URL | awesome_sso-0.6.0-py3-none-any.whl |
|---|---|
| Size | 25.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c822588dfdec588f1ebaf69963f769ef8a730e02a44f3fe1d645b34492d85df9
|
|
BLAKE2b-256 checksum How to use checksums |
d7df1675756ab5981b5a084657e964beae00ed2122a0f8b6690ce23f1d88fad2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.8.12 Darwin/20.6.0
|
Security for awesome-sso 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.