# drf-oauth2
OAuth2 implementation for Django Rest Framework.
## Installation
To install the package, use pip:
```bash
pip install drf-oauth2
```
## Requirements
- Python >= 3.7
- Django >= 4.0
- djangorestframework >= 3.12
## Setup
Add `oauth2` to your `INSTALLED_APPS` in your Django settings:
```python
INSTALLED_APPS = [
# Your apps
'oauth2',
]
```
Include the `oauth2` URLs in your project's `urls.py`:
```python
from django.urls import path, include
urlpatterns = [
# Your URLs
path('auth/', include('oauth2.urls')),
]
```
## Configuration
Configure your OAuth2 providers in your Django settings:
```python
import os
SOCIAL_AUTH_CONFIG = {
"google": {
"client_id": os.getenv("GOOGLE_CLIENT_ID", ""),
"client_secret": os.getenv("GOOGLE_CLIENT_SECRET", ""),
"redirect_uri": os.getenv("GOOGLE_REDIRECT_URI", ""),
},
"github": {
"client_id": os.getenv("GITHUB_CLIENT_ID", ""),
"client_secret": os.getenv("GITHUB_CLIENT_SECRET", ""),
"redirect_uri": os.getenv("GITHUB_REDIRECT_URI", ""),
},
"facebook": {
"client_id": os.getenv("FACEBOOK_CLIENT_ID", ""),
"client_secret": os.getenv("FACEBOOK_CLIENT_SECRET", ""),
"redirect_uri": os.getenv("FACEBOOK_REDIRECT_URI", ""),
"api_version": os.getenv("FACEBOOK_API_VERSION", "v21.0"),
},
}
```
## Usage
### Endpoints
- `POST /auth/social/register/` - Authenticate with a social provider.
- `GET /auth/social/list/` - List available OAuth2 providers.
### Example Request
#### Authenticate with a social provider
```http
GET /auth/social/register/
Content-Type: application/json
Request body:
{
"provider": "google"
}
Response body:
{
"success": true,
"message": "Redirecting to provider",
"data": {
"url": "https://github.com/login/oauth/authorize?client_id=CLIENTID&redirect_uri=http://localhost:3001/auth/github/redirect/&scope=user:email"
}
}
```
```http
POST /auth/social/register/
Content-Type: application/json
Request body:
{
"provider": "google",
"code": "authorization-code"
}
Response body:
{
"success": true,
"message": "Authentication successful",
"data": {
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTczODI0MTc0MCwiaWF0IjoxNzM4MTU1MzQwLCJqdGkiOiJiNzU3NWQzNzM1MTg0NDIxYmUzZWVjNmUxZmQwZGJkZiIsInVzZXJfaWQiOjJ9.ZaOaoYgXdaoyyPNWHnywkd97kVA6NwGHTLL2BnIrhQA",
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzM4MTU1NjQwLCJpYXQiOjE3MzgxNTUzNDAsImp0aSI6IjUzNzBhODQ4MDk4MzRmMzA5ZjdiZmE0ODgzNzFiZGI1IiwidXNlcl9pZCI6Mn0.4CimC7CL25EFeZyiWVTgqSE-KxTsnnjl_CXSCmPDITc",
"user": 2,
"expired_at": "2025-02-28T12:55:40.058390Z"
}
}
```
#### List available providers
```http
GET /auth/social/list/
Response body:
{
"success": true,
"message": "Available providers",
"data": [
"github"
]
}
```
## Development
To install development dependencies, use:
```bash
pip install -e .[dev]
```
Raw data
{
"_id": null,
"home_page": "https://github.com/JahongirHakimjonov/drf-oauth2",
"name": "drf-oauth2",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "django rest framework oauth2 authentication",
"author": "Jahongir Hakimjonov",
"author_email": "jahongirhakimjonov@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d6/16/eca1c8634d66bc0fa13bc5a508c869b3b2f2a6a6fe83c4c3ea5274be84c9/drf_oauth2-0.3.tar.gz",
"platform": null,
"description": "# drf-oauth2\r\n\r\nOAuth2 implementation for Django Rest Framework.\r\n\r\n## Installation\r\n\r\nTo install the package, use pip:\r\n\r\n```bash\r\npip install drf-oauth2\r\n```\r\n\r\n## Requirements\r\n\r\n- Python >= 3.7\r\n- Django >= 4.0\r\n- djangorestframework >= 3.12\r\n\r\n## Setup\r\n\r\nAdd `oauth2` to your `INSTALLED_APPS` in your Django settings:\r\n\r\n```python\r\nINSTALLED_APPS = [\r\n # Your apps\r\n 'oauth2',\r\n]\r\n```\r\n\r\nInclude the `oauth2` URLs in your project's `urls.py`:\r\n\r\n```python\r\nfrom django.urls import path, include\r\n\r\nurlpatterns = [\r\n # Your URLs\r\n path('auth/', include('oauth2.urls')),\r\n]\r\n```\r\n\r\n## Configuration\r\n\r\nConfigure your OAuth2 providers in your Django settings:\r\n\r\n```python\r\nimport os\r\n\r\nSOCIAL_AUTH_CONFIG = {\r\n \"google\": {\r\n \"client_id\": os.getenv(\"GOOGLE_CLIENT_ID\", \"\"),\r\n \"client_secret\": os.getenv(\"GOOGLE_CLIENT_SECRET\", \"\"),\r\n \"redirect_uri\": os.getenv(\"GOOGLE_REDIRECT_URI\", \"\"),\r\n },\r\n \"github\": {\r\n \"client_id\": os.getenv(\"GITHUB_CLIENT_ID\", \"\"),\r\n \"client_secret\": os.getenv(\"GITHUB_CLIENT_SECRET\", \"\"),\r\n \"redirect_uri\": os.getenv(\"GITHUB_REDIRECT_URI\", \"\"),\r\n },\r\n \"facebook\": {\r\n \"client_id\": os.getenv(\"FACEBOOK_CLIENT_ID\", \"\"),\r\n \"client_secret\": os.getenv(\"FACEBOOK_CLIENT_SECRET\", \"\"),\r\n \"redirect_uri\": os.getenv(\"FACEBOOK_REDIRECT_URI\", \"\"),\r\n \"api_version\": os.getenv(\"FACEBOOK_API_VERSION\", \"v21.0\"),\r\n },\r\n}\r\n```\r\n\r\n## Usage\r\n\r\n### Endpoints\r\n\r\n- `POST /auth/social/register/` - Authenticate with a social provider.\r\n- `GET /auth/social/list/` - List available OAuth2 providers.\r\n\r\n### Example Request\r\n\r\n#### Authenticate with a social provider\r\n\r\n```http\r\nGET /auth/social/register/\r\nContent-Type: application/json\r\n\r\nRequest body:\r\n{\r\n \"provider\": \"google\"\r\n}\r\n\r\nResponse body:\r\n{\r\n \"success\": true,\r\n \"message\": \"Redirecting to provider\",\r\n \"data\": {\r\n \"url\": \"https://github.com/login/oauth/authorize?client_id=CLIENTID&redirect_uri=http://localhost:3001/auth/github/redirect/&scope=user:email\"\r\n }\r\n}\r\n```\r\n\r\n```http\r\nPOST /auth/social/register/\r\nContent-Type: application/json\r\n\r\nRequest body:\r\n{\r\n \"provider\": \"google\",\r\n \"code\": \"authorization-code\"\r\n}\r\n\r\nResponse body:\r\n{\r\n \"success\": true,\r\n \"message\": \"Authentication successful\",\r\n \"data\": {\r\n \"refresh\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTczODI0MTc0MCwiaWF0IjoxNzM4MTU1MzQwLCJqdGkiOiJiNzU3NWQzNzM1MTg0NDIxYmUzZWVjNmUxZmQwZGJkZiIsInVzZXJfaWQiOjJ9.ZaOaoYgXdaoyyPNWHnywkd97kVA6NwGHTLL2BnIrhQA\",\r\n \"access\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzM4MTU1NjQwLCJpYXQiOjE3MzgxNTUzNDAsImp0aSI6IjUzNzBhODQ4MDk4MzRmMzA5ZjdiZmE0ODgzNzFiZGI1IiwidXNlcl9pZCI6Mn0.4CimC7CL25EFeZyiWVTgqSE-KxTsnnjl_CXSCmPDITc\",\r\n \"user\": 2,\r\n \"expired_at\": \"2025-02-28T12:55:40.058390Z\"\r\n }\r\n}\r\n```\r\n\r\n#### List available providers\r\n\r\n```http\r\nGET /auth/social/list/\r\n\r\nResponse body:\r\n{\r\n \"success\": true,\r\n \"message\": \"Available providers\",\r\n \"data\": [\r\n \"github\"\r\n ]\r\n}\r\n```\r\n\r\n## Development\r\n\r\nTo install development dependencies, use:\r\n\r\n```bash\r\npip install -e .[dev]\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "OAuth2 implementation for Django Rest Framework",
"version": "0.3",
"project_urls": {
"Homepage": "https://github.com/JahongirHakimjonov/drf-oauth2"
},
"split_keywords": [
"django",
"rest",
"framework",
"oauth2",
"authentication"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "e93015436693270e25b499f55584d7e49c84860487017d491b88664d93008b56",
"md5": "d3a776d823e7bdd48959524de5551ae5",
"sha256": "8729a54fce091329319adfc28ce4283873d30d6604341aca7fe4f0467b59a931"
},
"downloads": -1,
"filename": "drf_oauth2-0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d3a776d823e7bdd48959524de5551ae5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 14564,
"upload_time": "2025-01-29T13:24:38",
"upload_time_iso_8601": "2025-01-29T13:24:38.786465Z",
"url": "https://files.pythonhosted.org/packages/e9/30/15436693270e25b499f55584d7e49c84860487017d491b88664d93008b56/drf_oauth2-0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d616eca1c8634d66bc0fa13bc5a508c869b3b2f2a6a6fe83c4c3ea5274be84c9",
"md5": "953957793d0d6a91ce5ab48e9e8406c6",
"sha256": "7c845e05b0adcc664a82fb374e9fb218b8f739b11636429c76841756011f7509"
},
"downloads": -1,
"filename": "drf_oauth2-0.3.tar.gz",
"has_sig": false,
"md5_digest": "953957793d0d6a91ce5ab48e9e8406c6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 10502,
"upload_time": "2025-01-29T13:24:40",
"upload_time_iso_8601": "2025-01-29T13:24:40.570671Z",
"url": "https://files.pythonhosted.org/packages/d6/16/eca1c8634d66bc0fa13bc5a508c869b3b2f2a6a6fe83c4c3ea5274be84c9/drf_oauth2-0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-29 13:24:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "JahongirHakimjonov",
"github_project": "drf-oauth2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "drf-oauth2"
}