{"id":23274529,"url":"https://github.com/ahlem-phantom/django-auth-api","last_synced_at":"2025-04-06T10:46:10.584Z","repository":{"id":268780900,"uuid":"901806902","full_name":"ahlem-phantom/django-auth-api","owner":"ahlem-phantom","description":"This is a Django project that implements three different  authentication methods for accessing API endpoints","archived":false,"fork":false,"pushed_at":"2024-12-18T21:12:59.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T16:31:18.604Z","etag":null,"topics":["api-key-authentication","jwt-authentication","user-session"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahlem-phantom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-11T10:55:55.000Z","updated_at":"2024-12-18T21:13:02.000Z","dependencies_parsed_at":"2024-12-18T22:25:20.311Z","dependency_job_id":"2b1206dd-0387-470b-a569-97e37e083d58","html_url":"https://github.com/ahlem-phantom/django-auth-api","commit_stats":null,"previous_names":["ahlem-phantom/django-auth-api"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahlem-phantom%2Fdjango-auth-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahlem-phantom%2Fdjango-auth-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahlem-phantom%2Fdjango-auth-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahlem-phantom%2Fdjango-auth-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahlem-phantom","download_url":"https://codeload.github.com/ahlem-phantom/django-auth-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471396,"owners_count":20944154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-key-authentication","jwt-authentication","user-session"],"created_at":"2024-12-19T20:13:44.611Z","updated_at":"2025-04-06T10:46:10.546Z","avatar_url":"https://github.com/ahlem-phantom.png","language":"Python","readme":"# Django Authentication API\n\nThis project demonstrates how to implement three different authentication methods in a Django REST Framework (DRF) API: JWT (JSON Web Token), API key, and user session. The project includes three different API endpoints that require different types of authentication. The endpoints allow you to manage and interact with posts created by users.\n\n### 📐 Project Features \n\n- **User Authentication**: Custom user model inheriting from `AbstractUser` with fields `username`, `email`, `password`, `first_name`, `last_name`, and `api_key`.\n- **Post Model**: Model for posts with `title`, `content`, `author` (a foreign key to the User model), and `created_at` (auto-generated).\n- **Three API Endpoints**:\n  - `/posts/` (GET): Returns a list of all posts (requires JWT authentication).\n  - `/posts/\u003cid\u003e/` (GET): Returns a specific post by ID (requires API key authentication).\n  - `/posts/create/` (POST): Creates a new post (requires user session authentication).\n\n## Authentication Methods\n\n- **JWT Authentication**: Users can obtain a JWT token by logging in via `/api/token/`. This token is used to authenticate requests to the `/posts/` endpoint.\n- **API Key Authentication**: Users can obtain an API key by accessing the Django admin panel (`/admin/`) and creating a new API key for the user. This API key is used to authenticate requests to the `/posts/\u003cid\u003e/` endpoint.\n- **Session Authentication**: Users can log in with a username and password to obtain a session cookie. This cookie is used to authenticate requests to the `/posts/create/` endpoint.\n\n## ✨ Getting Started\nTo get a local copy up and running follow these simple example steps.\n\n\n### 🚧 Prerequisites\n\nYou may find below the list of things required for this project :\n- **Python**: 3.9 or higher\n- **Django**: 4.2 or higher\n- **Django REST Framework**: The primary package for building the API\n- **Simple JWT**: For JWT-based authentication (`django-rest-framework-simplejwt` version 5.2.2)\n- **django-rest-framework-api-key**: For API key-based authentication\n- **Django's built-in authentication system**: For user session authentication\n\n\n ### 🛠 Installation\n_In order to install the app you need to follow the instructions below :_\n1. Clone the repo\n   ```sh\n   git clone https://github.com/ahlem-phantom/django-travel-agency.git\n   ```\n\n2. Create a virtual environement and activate it \n   ```sh\n   ($) python3 -m venv venv\n   ($) .\\venv\\Scripts\\activate.bat\n   ```  \n3. Install flask dependecies using the file \"requirements.txt\"\n   ```sh\n   pip install -r requirements.txt\n   ```\n\n4. Run Django Migrations \n   ```sh\n   python manage.py makemigrations\n   python manage.py migrate\n   ```\n\n5. Create a Superuser Account for Admin Access\n   ```sh\n   python manage.py createsuperuser\n   ```\nYou'll be prompted to enter a username, email, and password for the admin account. After the superuser is created, you'll be able to log in to the Django admin dashboard.\n\n6. Run the django server\n   ```sh\n   python manage.py runserver\n   ```\n\n7. Open localhost:8000 to enjoy the app.\n\n\n## ⚡ Usage\n  - `/token/`  - Token Obtain Pair: Get JWT access and refresh tokens using username and password.\n  - ` /token/refresh/`  - Token Refresh: Refresh an expired access token using a refresh token.\n  - ` /login/`  - User Login: Authenticate and get a session cookie for session-based authentication.\n  - ` /posts/`  - Get All Posts: Retrieve a list of all posts.\n  - `/posts/\u003cint:id\u003e/`  - Get Post By ID: Retrieve a specific post by its ID.\n  - `/posts/create/`  - Create a New Post: Create a new post with authentication. \n\n\nIn this step, I logged in to get the session cookies (including the session ID and CSRF token). These cookies were then used to authenticate a request for creating a new post.\n| \u003cimg src=\"https://github.com/user-attachments/assets/10013867-4057-470e-8e33-399e9408f706\" /\u003e\u003cbr\u003e **Login to get session cookies **| \u003cimg src=\"https://github.com/user-attachments/assets/e18daf12-11d7-4393-8dd8-e518c8e507ee\" /\u003e  \u003cbr\u003e**Create Post**| \n| ------------- | ------------- | \n\nAfter logging in, I generated a JWT token from the token endpoint. This token was then used to authenticate and fetch all posts.\n| \u003cimg src=\"https://github.com/user-attachments/assets/06ed6a87-38bc-4c72-b594-4a02040df8de\" /\u003e\u003cbr\u003e **Get the JWT token from the token endpoint**| \u003cimg src=\"https://github.com/user-attachments/assets/4cc552a6-cac8-4eb1-92bd-dde8f819343a\" /\u003e  \u003cbr\u003e**Get All Posts**| \n| ------------- | ------------- | \n\nFinally, I manually created an API key through the Django admin dashboard to authenticate requests using API key-based authentication.\n| \u003cimg src=\"https://github.com/user-attachments/assets/ac732a65-74d6-46c3-98e7-ca3f65321df4\" /\u003e\u003cbr\u003e **Get Post By ID**|\n| ------------- | \n\n\n\n\u003c!-- CONTACT --\u003e\n## 💌 Contact\n\n\u003cb\u003eProject Author :\u003c/b\u003e \n| \u003cimg src=\"https://user-images.githubusercontent.com/78981558/157719496-9aec4730-512f-4188-87ca-8dbe6271ebfc.jpg\" width=\"150\" height=\"150\"/\u003e  \u003cbr\u003e **Ahlem Laajili**| \n| ------------- |\n|\u003cdiv align=\"center\"\u003e\u003ca href=\"mailto:ahlem.laajili@esprit.tn\"\u003e\u003cimg src=\"https://img.shields.io/badge/Gmail-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white\" alt=\"Gmail Badge\"/\u003e\u003c/a\u003e\u003ca href=\"https://github.com/ahlem-phantom\"\u003e\u003cimg title=\"Follow on GitHub\" src=\"https://img.shields.io/badge/GitHub-100000?style=for-the-badge\u0026logo=github\u0026logoColor=white\"/\u003e\u003c/a\u003e\u003c/div\u003e  |\n\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\n\nDeveloped with 💕 by **ahlem-phantom**.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahlem-phantom%2Fdjango-auth-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahlem-phantom%2Fdjango-auth-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahlem-phantom%2Fdjango-auth-api/lists"}