Authentication

νšŒμ› κ°€μž…

  • λΉ„λ°€λ²ˆν˜Έ 제곡 및 확인

    • UserCreateionFormμΆ”κ°€ column μ •μ˜

    • μ €μž₯ logicμ—μ„œ μΌμΉ˜ν•˜λŠ”μ§€ 확인

  • λΉ„λ°€λ²ˆν˜Έ μ•”ν˜Έν™” μ €μž₯

    • User.objects.create_user(username, email=None, password=None)

    • user.set_password(password)

둜그인

μ‚¬μš©μžκ°€ 둜그인 ν•œ μ‚¬λžŒμ΄λ‹€?

Stateless & Connectless

  • 맀 μš”μ²­μ΄ 독립 사건

    • cookieκ°€ 이걸 이어쀀닀!

User Object

from django.contrib.auth.models import User
  • core of the authentication system

  • 'superusers' or admin 'staff' users are just user objects with special attributes set, not different classes of user objects

  • AbstractBaseUser

  • AbstractUser

  • User

Primary attributes of default user

  • username

  • password

  • email

  • first_name

  • last_name

Creating Users

from django.contrib.auth.models import User
user = User.objects.create_user('chloe', 'email-address@gmail.com', 'password-goes-here')

# At this point, user is a User object that has already been saved to the database. 

# You can continue to change its attributes, if you want to change other fields.
user.last_name = 'kim'
user.save()

Changing Password

1. Using command line

$ python manage.py changepassword haha
Changing password for user 'haha'
Password: 
Password (again): 

2. Using set_password()

In [6]: ha = User.objects.get(username='haha')                                                                  

In [7]: ha                                                                                                      
Out[7]: <User: haha>

In [8]: ha.set_password('dkgkgkgk')                                                                             
In [9]: ha.save()             

Authenticating Users

authenticate(request=None, **credentials)

  • use it to verify a set of credentials

  • takes credentials as keyword arguments

    • username and password for the default cases

  • returns User object if credentials are valid for a backend

from django.contrib.auth import authenticate
user = authenticate(username='chloe', password='dkgkgkgk')
if user is not None:
    # A backend authenticated the credentials
else:
    # No backend authenticated the credentials

μž₯λ°”κ΅¬λ‹ˆ

  1. μ‚¬μš©μž ---> μž₯λ°”κ΅¬λ‹ˆ ---> 쿠팑

  2. μ‚¬μš©μž <--- μΏ ν‚€ <--- 쿠팑

  • μž₯λ°”κ΅¬λ‹ˆ == cookie

  • ꡬ맀내역 == data

둜그인 == create

λ‘œκ·Έμ•„μ›ƒ == delete

둜그인 Form

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
  • AutehticationForm은 ModelForm 이 μ•„λ‹ˆλΌ κ·Έλƒ₯ Form 이닀!

둜그인 ν•¨μˆ˜

from django.contrib.auth import get_user_model, login

def signin(request):
    if request.method == 'POST':
        # μ‚¬μš©μžκ°€ 보낸 κ°’ -> form
        form = AuthenticationForm(request, request.POST)
        # 검증
        # -> 검증 μ™„λ£Œ μ‹œ 둜그인
        if form.is_valid():
            login(request, form.get_user())
            return redirect('accounts:index')
    else:    
        form = AuthenticationForm()
    context = {
        'form':form 
    }
    return render(request, 'accounts/signin.html', context)
  • elseλ¬Έ 처리λ₯Ό λ§€λ„λŸ½κ²Œ ν•˜κΈ° μœ„ν•΄ 첫번째 if둜 POSTλ₯Ό λ¨Όμ € κ±°λ₯Έλ‹€

    • why?

      • λ§Œμ•½ GET을 λ¨Όμ € κ±°λ₯΄λ©΄, POSTμ—μ„œ .is_valid()에 걸리지 μ•Šκ³  else 둜 떨어지면 λ‹€μ‹œ renderν•˜λŠ” μ½”λ“œ μ¨μ€˜μ•Όν•΄μ„œ!

      • 즉, *code의 κ²½μ œμ„±μ„ μœ„ν•΄ POST λ₯Ό λ¨Όμ € μ“΄λ‹€!

+

POST 둜 λ¨Όμ € λΆ„κΈ°ν•˜λŠ” 이유

  1. μ½”λ“œμ˜ κ°„κ²°μ„±

  2. REST API λŒ€μ‘

  • ν˜„μž¬ μš°λ¦¬λŠ” GET & POST만 λŒ€μ‘ν•˜κ³  μžˆλŠ”λ° 이후에 RESTful ν•˜κ²Œ λ©”μ†Œλ“œ ꡬ성할 경우 GET/POST/PUT/DELETE μ—¬λŸ¬κ°œμ˜ λ©”μ†Œλ“œκ°€ 였게 되고 GET methodκ°€ λ§ˆμ§€λ§‰μ—μ— ν•Έλ“€λ§λ˜λŠ” ν˜•νƒœκ°€ κ°€μž₯ κ°„κ²°ν•œ μ½”λ“œ ꡬ성이 κ°€λŠ₯!

Message Framework

new

-> κΈ€ μž‘μ„± νŽ˜μ΄μ§€ (form)

create

-> DB에저μž₯

-> render

-> redirect(성곡여뢀)

-> redirect('articles:index')

HTTPλŠ” request와 response의 λ°˜λ³΅μ΄λ‹€!

HTTP

  • stateless (무 μƒνƒœμ„±)

    • ν•œλ²ˆ μš”μ²­μ„ 보내면 μƒνƒœ(κ³Όκ±°)λ₯Ό μ•Œ 수 μ—†μŒ

    • λͺ¨λ“  μš”μ²­ & 응닡은 μΌνšŒμ„±μ΄λ‹€

    • HTTPλŠ” λ‹¨μ ˆμ μΈ protocol

  • connectionless (무 μ—°κ²°μ„±)

Message Framework

  • μ΄μ „μ˜ μƒνƒœλ₯Ό λ‹€μŒ Request & Response에 λ„˜κ²¨μ€€λ‹€λŠ” 것이 μ˜λ―Έκ°€ μžˆλ‹€

    • Fallback Storage

      • Cookie κ°€ μ•ˆλ˜λ©΄ Session

Dynamic view

Article CRUD

  • title, content, create_at, updated_at

User CRUD (직접 < Django)

+

  • in memory cache -> ram에 λ„μ›Œλ†“λŠ” cache라고 μƒκ°ν•˜λ©΄ 됨

    • memcached

    • redis

  • ꡬ글 κ΄‘κ³  아이디......gdpr

  • macaddress = 기기정보

Last updated