Skip to content

Commit

Permalink
Login succesful, but problem fetching token
Browse files Browse the repository at this point in the history
  • Loading branch information
ticoucke committed Feb 26, 2024
1 parent b8e287e commit 12081f2
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 4 deletions.
5 changes: 5 additions & 0 deletions api/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
client_id='239ce609-e362-4cf6-919f-97e6935ef5f5'
client_secret='Eyv8Q~PoW8FW_y3Wdprc~HdCQg6whiIz8wi0ddbm'
tenant_id='d7811cde-ecef-496c-8f91-a1786241b99c'

ad_url='https://login.microsoftonline.com/d7811cde-ecef-496c-8f91-a1786241b99c/oauth2/v2.0/token?'
Binary file added api/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/views.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/wsgi.cpython-311.pyc
Binary file not shown.
36 changes: 35 additions & 1 deletion api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"""

from pathlib import Path
import os
from dotenv import load_dotenv

load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -37,6 +41,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_auth_adfs',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -126,5 +131,34 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')


CLIENT_ID = os.getenv('client_id')
CLIENT_SECRET = os.getenv('client_secret')
TENANT_ID = os.getenv('tenant_id')

AD_URL = os.getenv('ad_url')


AUTH_ADFS = {
'AUDIENCE': CLIENT_ID,
'CLIENT_ID': CLIENT_ID,
'CLIENT_SECRET': CLIENT_SECRET,
'CLAIM_MAPPING': {'first_name': 'given_name',
'last_name': 'family_name',
'email': 'upn'},
'GROUPS_CLAIM': 'roles',
'MIRROR_GROUPS': True,
'USERNAME_CLAIM': 'upn',
'TENANT_ID': TENANT_ID,
'RELYING_PARTY_ID': CLIENT_ID,
}

AUTHENTICATION_BACKENDS = [
'django_auth_adfs.backend.AdfsAuthCodeBackend',
]

LOGIN_URL = "django_auth_adfs:login"
LOGIN_REDIRECT_URL = "/api/logged_in"

6 changes: 4 additions & 2 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

from .views import main, microsoft_association
from .views import main, microsoft_association, succesfully_logged_in

urlpatterns = [
path('.well-known/microsoft-identity-association.json', microsoft_association),
path('api/', main),
path('admin/', admin.site.urls),
path('oauth2/', include('django_auth_adfs.urls')),
path('api/logged_in', succesfully_logged_in)
]
43 changes: 43 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@
from django.http import HttpResponse

from django.http import JsonResponse
from django.conf import settings

import requests

def get_graph_token():
"""
Get graph token from AD url.
"""
try:
url = settings.AD_URL

headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}

data = {
'grant_type': 'client_credentials',
'client_id': settings.CLIENT_ID,
'client_secret': settings.CLIENT_SECRET,
'scope': 'https://graph.microsoft.com/.default',
}

print('hallo')
response = requests.post(url=url, headers=headers, data=data)
print(response)
return response.json()
except:
return None




def succesfully_logged_in(request):
"""
Get user details from microsoft graph apis.
"""
graph_token = get_graph_token()
print(graph_token)
"""if graph_token:
url = 'https://graph.microsoft.com/v1.0/users/' + request.user.username
headers = {
'Authorization': 'Bearer ' + graph_token
}"""
return HttpResponse("Logged in!")

def microsoft_association(request):
return JsonResponse({"associatedApplications": [{ "applicationId": "239ce609-e362-4cf6-919f-97e6935ef5f5" }]})
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
django
gunicorn
psycopg2-binary
psycopg2-binary
django-auth-adfs
python-dotenv

1 comment on commit 12081f2

@mathis2003
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zijn er hiervoor extra libraries geinstalleerd?

Please sign in to comment.