|
| 1 | +from django.shortcuts import render, redirect |
| 2 | +from django.contrib.auth.models import User |
| 3 | +from django.contrib.auth.decorators import login_required |
| 4 | +from django.contrib.auth import authenticate, login, logout |
| 5 | +from django.http import JsonResponse |
| 6 | +from django.contrib import messages |
| 7 | +from .models import UserData |
| 8 | +from .forms import UserLoginForm, UserRegisterForm |
| 9 | +import random |
| 10 | +import string |
| 11 | + |
| 12 | +def index(request): |
| 13 | + # main landing pg |
| 14 | + return render(request, 'index.html') |
| 15 | + |
| 16 | +def about(request): |
| 17 | + # about pg nothing special |
| 18 | + return render(request, 'about.html') |
| 19 | + |
| 20 | +def login_view(request): |
| 21 | + if request.method == 'POST': |
| 22 | + form = UserLoginForm(request.POST) |
| 23 | + if form.is_valid(): |
| 24 | + username = form.cleaned_data.get('username') |
| 25 | + password = form.cleaned_data.get('password') |
| 26 | + user = authenticate(username=username, password=password) |
| 27 | + if user is not None: |
| 28 | + login(request, user) |
| 29 | + messages.success(request, f'Welcome back, {username}! You are now logged in.') |
| 30 | + return redirect('profile') |
| 31 | + else: |
| 32 | + messages.error(request, 'Invalid username or password. Please try again.') |
| 33 | + else: |
| 34 | + form = UserLoginForm() |
| 35 | + |
| 36 | + return render(request, 'login.html', {'form': form}) |
| 37 | + |
| 38 | +def generate_api_key(): |
| 39 | + # generate a random api key |
| 40 | + # I should probably use a better method but this works for now |
| 41 | + chars = string.ascii_lowercase + string.digits |
| 42 | + return ''.join(random.choices(chars, k=16)) # 16 chars should be enough right? |
| 43 | + |
| 44 | +def register_view(request): |
| 45 | + if request.method == 'POST': |
| 46 | + form = UserRegisterForm(request.POST) |
| 47 | + if form.is_valid(): |
| 48 | + username = form.cleaned_data.get('username') |
| 49 | + password = form.cleaned_data.get('password1') |
| 50 | + user = User.objects.create_user(username=username, password=password) |
| 51 | + |
| 52 | + # Creating sensitive data for the user |
| 53 | + # Yeah i know this is dummy data but works for demo |
| 54 | + UserData.objects.create( |
| 55 | + user=user, |
| 56 | + credit_card='4111111111111111', # test visa card number lol |
| 57 | + ssn='123456789', # not a real SSN obvs |
| 58 | + api_key=generate_api_key() # not very secure api key but whatever |
| 59 | + ) |
| 60 | + |
| 61 | + messages.success(request, f'Account created for {username}! You can now log in.') |
| 62 | + return redirect('login') |
| 63 | + else: |
| 64 | + form = UserRegisterForm() |
| 65 | + |
| 66 | + return render(request, 'register.html', {'form': form}) |
| 67 | + |
| 68 | +@login_required |
| 69 | +def profile_view(request): |
| 70 | + # show user profile with some data masked |
| 71 | + try: |
| 72 | + user_data = UserData.objects.get(user=request.user) |
| 73 | + # TODO: add audit logging here someday |
| 74 | + except UserData.DoesNotExist: |
| 75 | + # If no user data exists, create some dummy data for demo |
| 76 | + # This should never happen but just in case |
| 77 | + print(f"Creating missing user data for {request.user.username}") # debugging stuff |
| 78 | + user_data = UserData.objects.create( |
| 79 | + user=request.user, |
| 80 | + credit_card='4111111111111111', # test visa card number lol |
| 81 | + ssn='123456789', # not a real SSN obvs |
| 82 | + api_key=generate_api_key() # not very secure api key but whatever |
| 83 | + ) |
| 84 | + return render(request, 'profile.html', {'user_data': user_data}) |
| 85 | + |
| 86 | +@login_required |
| 87 | +def api_data_view(request): |
| 88 | + # FIXME: this is insecure AF - just for demo purposes!! |
| 89 | + # sends all user data as json - bad practice!! |
| 90 | + try: |
| 91 | + user_data = UserData.objects.get(user=request.user) |
| 92 | + except UserData.DoesNotExist: |
| 93 | + # If no user data exists, create some dummy data for demo |
| 94 | + user_data = UserData.objects.create( |
| 95 | + user=request.user, |
| 96 | + credit_card='4111111111111111', # test card number |
| 97 | + ssn='123456789', # demo SSN |
| 98 | + api_key=generate_api_key() # simple api key |
| 99 | + ) |
| 100 | + |
| 101 | + data = { |
| 102 | + 'username': request.user.username, |
| 103 | + 'credit_card': user_data.credit_card, |
| 104 | + 'ssn': user_data.ssn, |
| 105 | + 'api_key': user_data.api_key |
| 106 | + } |
| 107 | + # Intentionally exposing sensitive data through API |
| 108 | + # cuz we're teaching about data exposure, duh! |
| 109 | + return JsonResponse(data) |
| 110 | + |
| 111 | +# Intentionally insecure - for teaching purposes! |
| 112 | +def all_users_data_view(request): |
| 113 | + # MAJOR SECURITY FLAW: This endpoint allows ANY user (even unauthenticated ones!) |
| 114 | + # to see ALL users' sensitive data! |
| 115 | + # This demonstrates why proper authentication/authorization is essential. |
| 116 | + |
| 117 | + # Note for students: Notice how there are no checks for who's requesting the data! |
| 118 | + |
| 119 | + all_users_data = [] |
| 120 | + for user_data in UserData.objects.all(): |
| 121 | + all_users_data.append({ |
| 122 | + 'username': user_data.user.username, |
| 123 | + 'credit_card': user_data.credit_card, # Completely exposed! No masking! |
| 124 | + 'ssn': user_data.ssn, # Sending full SSN - terrible practice! |
| 125 | + 'api_key': user_data.api_key # API keys should never be exposed like this |
| 126 | + }) |
| 127 | + |
| 128 | + # In a secure application, we would add: |
| 129 | + # if not request.user.is_authenticated or not request.user.is_staff: |
| 130 | + # return JsonResponse({'error': 'Unauthorized'}, status=401) |
| 131 | + |
| 132 | + return JsonResponse({'users': all_users_data}) |
| 133 | + |
| 134 | +def logout_view(request): |
| 135 | + # simple logout nothing fancy |
| 136 | + logout(request) |
| 137 | + messages.info(request, 'You have been logged out successfully.') |
| 138 | + return redirect('index') |
| 139 | + |
| 140 | +def sensitive_data_exposure_lesson(request): |
| 141 | + # lessons page |
| 142 | + return render(request, 'lesson.html') |
0 commit comments