HTTP 기본 인증 ( Basic authentication )

2021. 7. 12. 09:402018년 이전 관심사/보안

반응형

기본 인증(Basic authentication)의 예

 

자료 출처:  http://www.asp.net/web-api/overview/security/basic-authentication

  1. 서버가 사용자에게 인증 요구를 원할 때, 서버는 401 Unauthorized 응답과 함께 WWW-Authenticate 헤더를 포함해서 클라이언트에게 전송한다.
    • 이 정보에는 클라이언트가 어떻게 인증해야 할지 포함되어 있고, 위 예시에서는 Basic 인증을 요청 한다.
  2. 해당 메시지를 받은 클라이언트는 서버에게 인증 메시지를 보낼 수 있다.
    • 위 그림에서는 서버가 Basic 인증을 요청 했기 때문에, 인토딩(BASE64)된 비밀번호와 그 외 인증 파라미터들을 Authorization 헤더에 담아서 메시지를 보낸다.
  3. 성공적으로 완료되면 서버는 정상적으로 200 OK 코드를 반환한다.
    • 추가적인 인증 알고리즘에 대한 정보는 Authentication-Info 헤더에 기술한다.

기본 인증 테스트

기본 인증 테스트를 위해 PostMan을 사용하여 서버에게 기본인증(Basic Authentication)메시지를 보내고, 응답이 제대로 오는지 확인 한다.

 

아래 그림과 같이 PostMan의 Authorization 항목에 Basic Auth를 선택하고 Username과 Password를 입력하면, 클라이언트가 보낼 메시지의 Header에 BASE64로 인코딩 된 값을 설정 하는 것을 볼 수 있다.

Authorization : Basic a2FpemVuOjEyMzQ1 ( Username:Password ) [ kaizen:12345 ]

 

 

 

https://www.convertstring.com/ko/EncodeDecode/Base64Decode

 

 

Basic Auth를 처리하기 위한 Server Code(Django)

 

from django.shortcuts import render
from django.views import View
from django.contrib.auth.models import User

# Create your views here.
from rest_framework import viewsets
from .serializer import Vdv261VehicleInfoSerializer
from .models import Vdv261VehicleInfo, Vdv261BackendInfo
from .forms import Vdv261VehicleInfoForm, Vdv261BackendInfoForm
from django.http import JsonResponse, HttpResponse

import base64
import json

class Vdv261PostView(View):
    def post(self, request):
        auth_header = request.META.get('HTTP_AUTHORIZATION', '') 
            
        # token_type : Basic, credentials
        token_type, _, credentials = auth_header.partition(' ')
                               
        username, password = base64.b64decode(credentials).decode('utf-8').split(':')

        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            return HttpResonse(status=401)

        password_valid = user.check_password(password)

        if token_type != 'Basic' or not password_valid:
            return HttpResponse(status=401)

        json_data = json.loads(request.body)

 

 

참고 자료

- https://hamait.tistory.com/416

 

HTTP 기본인증 (Basic authentication)

http://iloveulhj.github.io/posts/http/http-basic-auth.html 펌 [HTTP] 기본 인증 Feb 8, 2015 이 포스트는 “HTTP 완벽가이드”의 “12장, 기본 인증”을 정리한 내용입니다. 수 많은 사람들이 웹을 통해 업무..

hamait.tistory.com

 

 

반응형