Skip to content

Commit e9b7667

Browse files
committed
Copy project files to git
1 parent 1f0cc91 commit e9b7667

21 files changed

+774
-14
lines changed

graph_tutorial/graph_tutorial/asgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
It exposes the ASGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
88
"""
99

1010
import os

graph_tutorial/graph_tutorial/settings.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
22
Django settings for graph_tutorial project.
33
4-
Generated by 'django-admin startproject' using Django 3.1.7.
4+
Generated by 'django-admin startproject' using Django 3.2.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.1/topics/settings/
7+
https://docs.djangoproject.com/en/3.2/topics/settings/
88
99
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/3.1/ref/settings/
10+
https://docs.djangoproject.com/en/3.2/ref/settings/
1111
"""
1212

1313
from pathlib import Path
@@ -17,10 +17,10 @@
1717

1818

1919
# Quick-start development settings - unsuitable for production
20-
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
20+
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
2121

2222
# SECURITY WARNING: keep the secret key used in production secret!
23-
SECRET_KEY = 'x4s6io7a+od9)9x7u+0m4&yqv8z)a1$)j^bi3*3mww#k!k!uo7'
23+
SECRET_KEY = 'django-insecure-7f4qkv38f$uqo)mqd^v14ijirypn4a=u)_%t(x5%vk(sq#_-5i'
2424

2525
# SECURITY WARNING: don't run with debug turned on in production!
2626
DEBUG = True
@@ -30,7 +30,6 @@
3030

3131
# Application definition
3232

33-
# <InstalledAppsSnippet>
3433
INSTALLED_APPS = [
3534
'django.contrib.admin',
3635
'django.contrib.auth',
@@ -40,7 +39,6 @@
4039
'django.contrib.staticfiles',
4140
'tutorial',
4241
]
43-
# </InstalledAppsSnippet>
4442

4543
MIDDLEWARE = [
4644
'django.middleware.security.SecurityMiddleware',
@@ -74,7 +72,7 @@
7472

7573

7674
# Database
77-
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
75+
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
7876

7977
DATABASES = {
8078
'default': {
@@ -85,7 +83,7 @@
8583

8684

8785
# Password validation
88-
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
86+
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
8987

9088
AUTH_PASSWORD_VALIDATORS = [
9189
{
@@ -104,7 +102,7 @@
104102

105103

106104
# Internationalization
107-
# https://docs.djangoproject.com/en/3.1/topics/i18n/
105+
# https://docs.djangoproject.com/en/3.2/topics/i18n/
108106

109107
LANGUAGE_CODE = 'en-us'
110108

@@ -118,6 +116,11 @@
118116

119117

120118
# Static files (CSS, JavaScript, Images)
121-
# https://docs.djangoproject.com/en/3.1/howto/static-files/
119+
# https://docs.djangoproject.com/en/3.2/howto/static-files/
122120

123121
STATIC_URL = '/static/'
122+
123+
# Default primary key field type
124+
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
125+
126+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

graph_tutorial/graph_tutorial/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
path('', include('tutorial.urls')),
1111
path('admin/', admin.site.urls),
1212
]
13-
# </UrlConfSnippet>
13+
# </UrlConfSnippet>

graph_tutorial/graph_tutorial/wsgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
88
"""
99

1010
import os

graph_tutorial/manage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
def main():
8+
"""Run administrative tasks."""
89
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graph_tutorial.settings')
910
try:
1011
from django.core.management import execute_from_command_line

graph_tutorial/tutorial/__init__.py

Whitespace-only changes.

graph_tutorial/tutorial/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

graph_tutorial/tutorial/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class TutorialConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'tutorial'
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# <FirstCodeSnippet>
2+
import yaml
3+
import msal
4+
import os
5+
import time
6+
7+
# Load the oauth_settings.yml file
8+
stream = open('oauth_settings.yml', 'r')
9+
settings = yaml.load(stream, yaml.SafeLoader)
10+
11+
def load_cache(request):
12+
# Check for a token cache in the session
13+
cache = msal.SerializableTokenCache()
14+
if request.session.get('token_cache'):
15+
cache.deserialize(request.session['token_cache'])
16+
17+
return cache
18+
19+
def save_cache(request, cache):
20+
# If cache has changed, persist back to session
21+
if cache.has_state_changed:
22+
request.session['token_cache'] = cache.serialize()
23+
24+
def get_msal_app(cache=None):
25+
# Initialize the MSAL confidential client
26+
auth_app = msal.ConfidentialClientApplication(
27+
settings['app_id'],
28+
authority=settings['authority'],
29+
client_credential=settings['app_secret'],
30+
token_cache=cache)
31+
32+
return auth_app
33+
34+
# Method to generate a sign-in flow
35+
def get_sign_in_flow():
36+
auth_app = get_msal_app()
37+
38+
return auth_app.initiate_auth_code_flow(
39+
settings['scopes'],
40+
redirect_uri=settings['redirect'])
41+
42+
# Method to exchange auth code for access token
43+
def get_token_from_code(request):
44+
cache = load_cache(request)
45+
auth_app = get_msal_app(cache)
46+
47+
# Get the flow saved in session
48+
flow = request.session.pop('auth_flow', {})
49+
50+
result = auth_app.acquire_token_by_auth_code_flow(flow, request.GET)
51+
save_cache(request, cache)
52+
53+
return result
54+
# </FirstCodeSnippet>
55+
56+
# <SecondCodeSnippet>
57+
def store_user(request, user):
58+
try:
59+
request.session['user'] = {
60+
'is_authenticated': True,
61+
'name': user['displayName'],
62+
'email': user['mail'] if (user['mail'] != None) else user['userPrincipalName'],
63+
'timeZone': user['mailboxSettings']['timeZone'] if ('timeZone' in user['mailboxSettings']) else 'UTC'
64+
}
65+
except Exception as e:
66+
print(e)
67+
68+
def get_token(request):
69+
cache = load_cache(request)
70+
auth_app = get_msal_app(cache)
71+
72+
accounts = auth_app.get_accounts()
73+
if accounts:
74+
result = auth_app.acquire_token_silent(
75+
settings['scopes'],
76+
account=accounts[0])
77+
78+
save_cache(request, cache)
79+
80+
return result['access_token']
81+
82+
def remove_user_and_token(request):
83+
if 'token_cache' in request.session:
84+
del request.session['token_cache']
85+
86+
if 'user' in request.session:
87+
del request.session['user']
88+
# </SecondCodeSnippet>

0 commit comments

Comments
 (0)