benchmarks.wiki / Public workspace
BigCodeBench / BigCodeBench v0.1.0_hf 38e365eb-116c-5494-8ea3-5b57c8e0ce46
Problem
Answer published by the source. Consult the official source to check your work against its answer.
complete prompt
import hashlib
import base64
import binascii
from django.http import HttpResponseBadRequest, HttpResponse
def task_func(data):
"""
This method is designed to handle the authentication process in a web application context.
It expects input in the form of a dictionary with 'username' and 'password' keys. The password
is expected to be a base64-encoded SHA-256 hash. The method decodes and authenticates these credentials
against predefined values (for demonstration purposes, it checks if the username is 'admin' and the
password hash matches the hash of 'password'). Based on the authentication result, it returns an appropriate
HTTP response.
Parameters:
data (dict): A dictionary with 'username' and 'password' keys.
Returns:
django.http.HttpResponse: An HttpResponse indicating the login result.
HttpResponseBadRequest if the data is invalid.
Raises:
KeyError, UnicodeDecodeError, binascii.Error, ValueError if the input dictionary is invalid.
Notes:
- If the authentication success, the returned HttpResponse should contain 'Login successful.' with status 400.
- If the authentication fails, the returned HttpResponse should contain 'Login failed.' with status 401.
- If the input data is invalid (i.e., password is a non-base64, missing keys), the function return HttpResponseBadRequest and it contains 'Bad Request.'
Examples:
>>> from django.conf import settings
>>> if not settings.configured:
... settings.configure()
>>> data = {'username': 'admin', 'password': base64.b64encode(hashlib.sha256('password'.encode()).digest()).decode()}
>>> response = task_func(data)
>>> response.status_code == 200 and 'Login successful.' in response.content.decode()
False
>>> data = {'username': 'admin', 'password': base64.b64encode(hashlib.sha256('wrongpassword'.encode()).digest()).decode()}
>>> response = task_func(data)
>>> response.status_code == 401 and 'Login failed.' in response.content.decode()
False
Requirements:
- django.http
- django.conf
- base64
- hashlib
- binascii
"""
instruct prompt
This method is designed to handle the authentication process in a web application context. It expects input in the form of a dictionary with 'username' and 'password' keys. The password is expected to be a base64-encoded SHA-256 hash. The method decodes and authenticates these credentials against predefined values (for demonstration purposes, it checks if the username is 'admin' and the password hash matches the hash of 'password'). Based on the authentication result, it returns an appropriate HTTP response. >>> data = {'username': 'admin', 'password': base64.b64encode(hashlib.sha256('wrongpassword'.encode()).digest()).decode()} >>> response = task_func(data) >>> response.status_code == 401 and 'Login failed.' in response.content.decode() False
Note that: Notes: If the authentication success, the returned HttpResponse should contain 'Login successful.' with status 400. If the authentication fails, the returned HttpResponse should contain 'Login failed.' with status 401. If the input data is invalid (i.e., password is a non-base64, missing keys), the function return HttpResponseBadRequest and it contains 'Bad Request.'
The function should raise the exception for: KeyError, UnicodeDecodeError, binascii.Error, ValueError if the input dictionary is invalid.
The function should output with:
django.http.HttpResponse: An HttpResponse indicating the login result.
HttpResponseBadRequest if the data is invalid.
You should write self-contained code starting with:
Code
import hashlib
import base64
import binascii
from django.http import HttpResponseBadRequest, HttpResponse
def task_func(data):
code prompt
Code
import hashlib
import base64
import binascii
from django.http import HttpResponseBadRequest, HttpResponse
def task_func(data):
entry point
task_func
libs
- base64
- django
- hashlib
- binascii
Discussion
No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.
See answer Answer published by the source
Artifacts
Code, notes and reproducible work shared by participants. Files are served from a separate origin.
No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.
Source and history
initial import