# osm-login-python
Package to manage OAuth 2.0 login for OSM in Python.
📖 [Documentation](https://hotosm.github.io/osm-login-python/)
[](https://results.pre-commit.ci/latest/github/hotosm/osm-login-python/main)

## Install with [pip](https://pypi.org/project/osm-login-python/)
```bash
pip install osm-login-python
```
## Import Auth and initialize class with your credentials
```python
from osm_login_python.core import Auth
```
```python
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:
1. 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).
2. 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.
3. 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:
```python
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](https://pypi.org/project/commitizen/) for version control.
### Test Coverage
Generate a coverage badge:
```bash
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!
Raw data
{
"_id": null,
"home_page": null,
"name": "osm-login-python",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "osm, openstreetmap, oauth2, login, hot",
"author": null,
"author_email": "Kshitij Raj Sharma <skshitizraj@gmail.com>,Sam Woodcock <sam.woodcock@protonmail.com>",
"download_url": "https://files.pythonhosted.org/packages/71/32/1aae4704a91d288157a8dcb27cd47ca3b3b16d714b3d695966dacc896e31/osm-login-python-2.0.0.tar.gz",
"platform": null,
"description": "# osm-login-python\n\nPackage to manage OAuth 2.0 login for OSM in Python.\n\n\ud83d\udcd6 [Documentation](https://hotosm.github.io/osm-login-python/)\n\n[](https://results.pre-commit.ci/latest/github/hotosm/osm-login-python/main)\n\n\n\n## Install with [pip](https://pypi.org/project/osm-login-python/)\n\n```bash\npip install osm-login-python\n```\n\n## Import Auth and initialize class with your credentials\n\n```python\nfrom osm_login_python.core import Auth\n```\n\n```python\nosm_auth=Auth(\n osm_url=YOUR_OSM_URL,\n client_id=YOUR_OSM_CLIENT_ID,\n client_secret=YOUR_OSM_CLIENT_SECRET,\n secret_key=YOUR_OSM_SECRET_KEY,\n login_redirect_uri=YOUR_OSM_LOGIN_REDIRECT_URI,\n scope=YOUR_OSM_SCOPE_LIST,\n)\n```\n\n## Usage\n\nThree functions are provided:\n\n1. login() -- Returns the login url for OSM.\n\n - The user must then access this URL and authorize the OAuth application\n to login.\n - The user will be redirected to the configured `login_redirect_uri` after\n successful login with OSM.\n - The web page must then call the `callback()` function below, sending the\n current URL to the function (which includes the OAuth authorization code).\n\n2. callback() -- Returns the encoded and serialized data:\n\n - `user_data` a JSON of OSM user data.\n - `oauth_token` a string OSM OAuth token.\n - Both are encoded and serialized as an additional safety measure when used\n in URLs.\n\n3. deserialize_data() -- returns decoded and deserialized data from `callback()`.\n\n> [!NOTE]\n> This package is primarily intended to return OSM user data.\n>\n> It is also possible to obtain the `oauth_token` as described above, for making\n> authenticated requests against the OSM API from within a secure **backend**\n> service.\n>\n> To use the OAuth token in a **frontend** please use caution and adhere\n> to best practice security, such as embedding in a secure httpOnly cookie\n> (do not store in localStorage, sessionStorage, or unsecure cookies).\n\n## Example\n\nIn Django:\n\n```python\nimport json\nfrom django.conf import settings\nfrom osm_login_python.core import Auth\nfrom django.http import JsonResponse\n\n# initialize osm_auth with our credentials\nosm_auth = Auth(\n osm_url=YOUR_OSM_URL,\n client_id=YOUR_OSM_CLIENT_ID,\n client_secret=YOUR_OSM_CLIENT_SECRET,\n secret_key=YOUR_OSM_SECRET_KEY,\n login_redirect_uri=YOUR_OSM_LOGIN_REDIRECT_URI,\n scope=YOUR_OSM_SCOPE,\n)\n\ndef login(request):\n login_url = osm_auth.login()\n return JsonResponse(login_url)\n\ndef callback(request):\n # Generating token through osm_auth library method\n token = osm_auth.callback(request.build_absolute_uri())\n return JsonResponse(token)\n\ndef get_my_data(request, serialized_user_data: str):\n user_data = osm_auth.deserialize_data(serialized_user_data)\n return JsonResponse(user_data)\n```\n\n- Django integration example here\n <https://github.com/hotosm/fAIr/tree/master/backend/login>\n\n- FastAPI integration example here\n <https://github.com/hotosm/export-tool-api/tree/develop/API/auth>\n\n### Version Control\n\nUse [commitizen](https://pypi.org/project/commitizen/) for version control.\n\n### Test Coverage\n\nGenerate a coverage badge:\n\n```bash\npdm install\npdm run coverage run -m pytest\n# pdm run coverage report\npdm run coverage coverage-badge -o docs/coverage.svg\n```\n\n### Contribute\n\nContributions are welcome!\n\n",
"bugtrack_url": null,
"license": "GPL-3.0-only",
"summary": "Package to manage OAuth 2.0 login for OSM in Python.",
"version": "2.0.0",
"project_urls": {
"documentation": "https://hotosm.github.io/osm-login-python",
"homepage": "https://hotosm.github.io/osm-login-python",
"repository": "https://github.com/hotosm/osm-login-python"
},
"split_keywords": [
"osm",
" openstreetmap",
" oauth2",
" login",
" hot"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4de14de4056c1b766149aaa5707b3e51b00f56d1fa37ba3d5bc797ec6625fb77",
"md5": "658713e1e641583fd1cf05d8d3570ab2",
"sha256": "1289410fb1967f584ee52b02d96c85ff2cb104068481fa6530d5f3b138ec7afc"
},
"downloads": -1,
"filename": "osm_login_python-2.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "658713e1e641583fd1cf05d8d3570ab2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 17136,
"upload_time": "2024-08-09T10:35:42",
"upload_time_iso_8601": "2024-08-09T10:35:42.173330Z",
"url": "https://files.pythonhosted.org/packages/4d/e1/4de4056c1b766149aaa5707b3e51b00f56d1fa37ba3d5bc797ec6625fb77/osm_login_python-2.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "71321aae4704a91d288157a8dcb27cd47ca3b3b16d714b3d695966dacc896e31",
"md5": "77478f089bf1f1e1b4f24f05cf27e5d5",
"sha256": "01fa35402e1342820c725068f4e41c81dcce064e07058f8a206d98a8b6f04bb9"
},
"downloads": -1,
"filename": "osm-login-python-2.0.0.tar.gz",
"has_sig": false,
"md5_digest": "77478f089bf1f1e1b4f24f05cf27e5d5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 17339,
"upload_time": "2024-08-09T10:35:43",
"upload_time_iso_8601": "2024-08-09T10:35:43.764828Z",
"url": "https://files.pythonhosted.org/packages/71/32/1aae4704a91d288157a8dcb27cd47ca3b3b16d714b3d695966dacc896e31/osm-login-python-2.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-09 10:35:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hotosm",
"github_project": "osm-login-python",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "osm-login-python"
}