Package to manage OAuth 2.0 login for OSM in Python.
Project description
osm-login-python
Package to manage OAuth 2.0 login for OSM in Python.
Install with pip
pip install osm-login-python
Import Auth and initialize class with your credentials
from osm_login_python.core import Auth
osm_auth=Auth(
osm_url=YOUR_OSM_URL,
client_id=YOUR_OSM_CLIENT_ID,
client_secret=YOUR_OSM_CLIENT_SECRET,
secret_key=YOUR_OSM_SECRET_KEY,
login_redirect_uri=YOUR_OSM_LOGIN_REDIRECT_URI,
scope=YOUR_OSM_SCOPE_LIST,
)
Usage
Three functions are provided:
-
login() -- Returns the login url for OSM.
- The user must then access this URL and authorize the OAuth application to login.
- The user will be redirected to the configured
login_redirect_uri
after successful login with OSM. - The web page must then call the
callback()
function below, sending the current URL to the function (which includes the OAuth authorization code).
-
callback() -- Returns the encoded and serialized data:
user_data
a JSON of OSM user data.oauth_token
a string OSM OAuth token.- Both are encoded and serialized as an additional safety measure when used in URLs.
-
deserialize_data() -- returns decoded and deserialized data from
callback()
.
[!NOTE] This package is primarily intended to return OSM user data.
It is also possible to obtain the
oauth_token
as described above, for making authenticated requests against the OSM API from within a secure backend service.To use the OAuth token in a frontend please use caution and adhere to best practice security, such as embedding in a secure httpOnly cookie (do not store in localStorage, sessionStorage, or unsecure cookies).
Example
In Django:
import json
from django.conf import settings
from osm_login_python.core import Auth
from django.http import JsonResponse
# initialize osm_auth with our credentials
osm_auth = Auth(
osm_url=YOUR_OSM_URL,
client_id=YOUR_OSM_CLIENT_ID,
client_secret=YOUR_OSM_CLIENT_SECRET,
secret_key=YOUR_OSM_SECRET_KEY,
login_redirect_uri=YOUR_OSM_LOGIN_REDIRECT_URI,
scope=YOUR_OSM_SCOPE,
)
def login(request):
login_url = osm_auth.login()
return JsonResponse(login_url)
def callback(request):
# Generating token through osm_auth library method
token = osm_auth.callback(request.build_absolute_uri())
return JsonResponse(token)
def get_my_data(request, serialized_user_data: str):
user_data = osm_auth.deserialize_data(serialized_user_data)
return JsonResponse(user_data)
-
Django integration example here https://github.com/hotosm/fAIr/tree/master/backend/login
-
FastAPI integration example here https://github.com/hotosm/export-tool-api/tree/develop/API/auth
Version Control
Use commitizen for version control.
Test Coverage
Generate a coverage badge:
pdm install
pdm run coverage run -m pytest
# pdm run coverage report
pdm run coverage coverage-badge -o docs/coverage.svg
Contribute
Contributions are welcome!
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
Built Distribution
File details
Details for the file osm-login-python-2.0.0.tar.gz
.
File metadata
- Download URL: osm-login-python-2.0.0.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.9.3 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01fa35402e1342820c725068f4e41c81dcce064e07058f8a206d98a8b6f04bb9 |
|
MD5 | 77478f089bf1f1e1b4f24f05cf27e5d5 |
|
BLAKE2b-256 | 71321aae4704a91d288157a8dcb27cd47ca3b3b16d714b3d695966dacc896e31 |
File details
Details for the file osm_login_python-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: osm_login_python-2.0.0-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.9.3 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1289410fb1967f584ee52b02d96c85ff2cb104068481fa6530d5f3b138ec7afc |
|
MD5 | 658713e1e641583fd1cf05d8d3570ab2 |
|
BLAKE2b-256 | 4de14de4056c1b766149aaa5707b3e51b00f56d1fa37ba3d5bc797ec6625fb77 |