Skip to content

Commit

Permalink
Added GitHub Oauth registration and login
Browse files Browse the repository at this point in the history
Added GitHub Oauth which uses JWT REST API for authenticating users.

Fixes #101
  • Loading branch information
codesankalp committed Mar 7, 2021
1 parent f1d266a commit e158599
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ Next follow these instructions.
```
python manage.py migrate
```
7. To run the server:
7. To setup Social Auth for backend see [this](#Setup-Social-Auth).
8. To run the server:
```
python manage.py runserver
```
Expand Down Expand Up @@ -128,13 +129,27 @@ Follow the given instructions for Login into the app.
6. Click Create & View.
7. The API KEY is generated and displayed to you just once. So be sure to copy and save it somewhere.
3. `GITHUB_CALLBACK_URL` - For using GitHub authentication the **Callback URL** is required by GitHub API. Add this env variable in `.env` file or export it to use **Callback URL** which you used while setting Up GitHub App. The default value is: `http://localhost:3000/login`.
Add it to your .env file as follows:
```
export SENDGRID_API_KEY=<your-sendgrid-api-key>
```
3. `SECRET_KEY` - This environment variable is required for running the backend. Add `SECRET_KEY` in `.env` file or export it by using `export SECRET_KEY=<YOUR SECRET KEY>`.
# Setup Social Auth
1. Create a Super User by running this command: `python manage.py createsuperuser`.
2. Login to [Django admin site](http://localhost:8000/admin/) using credentials of the previous step.
3. Go to **Sites dashboard** in admin site. (**URL**: http://localhost:8000/admin/sites/site/).
4. Click on `Add site` button and fill in the information as given in the image.
![site_id](https://user-images.githubusercontent.com/56037184/109974910-0fa79b00-7d20-11eb-9826-44fdf6d770f9.png)
**Note**: After saving this if the site id is not `2` then change the `SITE_ID` in settings.py with the new site id.
5. After this go to **Social Applications Dashboard**. (**URL**: http://localhost:8000/admin/socialaccount/socialapp/).
6. Add the credentials that you get after creating the GitHub app. Fill in the information as given in the image.
![social_add](https://user-images.githubusercontent.com/56037184/109975941-35816f80-7d21-11eb-9a8f-205953306c83.png)
**For creating GitHub App see [this docs](https://docs.github.com/en/developers/apps/creating-a-github-app).**
## Testing
Expand Down
19 changes: 19 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
"corsheaders",
"osp",
"token_auth",
# For Social Authentications
"dj_rest_auth",
"django.contrib.sites",
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.github",
]

MIDDLEWARE = [
Expand All @@ -61,6 +68,8 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

SITE_ID = 2

ROOT_URLCONF = "main.urls"

TEMPLATES = [
Expand All @@ -83,11 +92,14 @@
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
"DEFAULT_PARSER_CLASSES": ["rest_framework.parsers.JSONParser"],
"DEFAULT_AUTHENTICATION_CLASSES": (
"dj_rest_auth.jwt_auth.JWTCookieAuthentication",
"rest_framework.authentication.SessionAuthentication",
"rest_framework_simplejwt.authentication.JWTAuthentication",
),
}

REST_USE_JWT = True

SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(days=2),
"REFRESH_TOKEN_LIFETIME": timedelta(days=30),
Expand Down Expand Up @@ -160,3 +172,10 @@
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = "/static/"

ACCOUNT_EMAIL_VERIFICATION = "none"

if os.environ.get("GITHUB_CALLBACK_URL"):
GITHUB_CALLBACK_URL = os.getenv("GITHUB_CALLBACK_URL")
else:
GITHUB_CALLBACK_URL = "http://localhost:3000/login"
21 changes: 18 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
appdirs==1.4.4
asgiref==3.2.7
astroid==2.4.1
black==20.8b1
certifi==2020.6.20
cffi==1.14.1
chardet==3.0.4
click==7.1.2
cryptography==3.0
defusedxml==0.6.0
distro==1.5.0
dj-rest-auth==2.1.3
Django==3.0.7
django-allauth==0.44.0
django-cors-headers==3.3.0
django-sendgrid-v5==0.8.1
djangorestframework==3.11.0
djangorestframework-jwt==1.11.0
djangorestframework-simplejwt==4.4.0
flake8==3.8.4
future==0.18.2
idna==2.10
isort==4.3.21
isort==5.7.0
lazy-object-proxy==1.4.3
matrix-client==0.3.2
mccabe==0.6.1
mypy-extensions==0.4.3
oauthlib==3.1.0
pathspec==0.8.1
psycopg2==2.8.5
pycodestyle==2.6.0
pycparser==2.20
pyflakes==2.2.0
PyJWT==1.7.1
pylint==2.5.2
pyOpenSSL==19.1.0
python-dotenv==0.13.0
python-http-client==3.2.7
python3-openid==3.2.0
pytz==2020.1
regex==2020.11.13
requests==2.24.0
requests-oauthlib==1.3.0
sendgrid==6.3.1
six==1.15.0
sqlparse==0.3.1
toml==0.10.1
typed-ast==1.4.1
typing-extensions==3.7.4.3
urllib3==1.25.10
wrapt==1.12.1
zulip==0.7.0
zulip==0.7.0
2 changes: 2 additions & 0 deletions token_auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.urls import path
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

from token_auth.views.github_oauth import GithubLogin
from token_auth.views.register import RegisterView

urlpatterns = [
Expand All @@ -14,4 +15,5 @@
# login URLs
path("token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("refresh/", TokenRefreshView.as_view(), name="token_refresh"),
path("github/", GithubLogin.as_view(), name="github_login"),
]
11 changes: 11 additions & 0 deletions token_auth/views/github_oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from dj_rest_auth.registration.views import SocialLoginView
from django.conf import settings


class GithubLogin(SocialLoginView):
authentication_classes = []
adapter_class = GitHubOAuth2Adapter
callback_url = settings.GITHUB_CALLBACK_URL
client_class = OAuth2Client

0 comments on commit e158599

Please sign in to comment.