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. ์‚ฌ์šฉ์ž <--- ์ฟ ํ‚ค <--- ์ฟ ํŒก

  3. ์žฅ๋ฐ”๊ตฌ๋‹ˆ == cookie

  4. ๊ตฌ๋งค๋‚ด์—ญ == 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