{"id":337607,"date":"2022-08-27T15:00:20","date_gmt":"2022-08-27T15:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=337607"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=337607","title":{"rendered":"<span>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432 Django \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Metamask<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043b\u0435\u0433\u043a\u0438\u0439 \u0441\u043d\u0438\u043f\u043f\u0435\u0442 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 Metamask.   <\/p>\n<p>\u0421\u043d\u0438\u043f\u043f\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u0434\u0432\u0443\u0445 \u0447\u0430\u0441\u0442\u0435\u0439 &#8212; <code>django template<\/code> \u0438 <code>django view<\/code>.<\/p>\n<hr\/>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 <code>django template<\/code>.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 <code>templates\/login.html<\/code><\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>\u043a\u043d\u043e\u043f\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/code> \u043f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d Metamask.<\/p>\n<pre><code>&lt;button type=\"button\" onclick=\"web3Login()\">     Log in with Metamask &lt;\/button><\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<pre><code>&lt;script src=\"https:\/\/cdn.ethers.io\/lib\/ethers-5.2.umd.min.js\" type=\"application\/javascript\">&lt;\/script> &lt;script> function web3Login() {   \/* \u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 *\/     try {         window.ethereum.enable().then(function () {             provider = new ethers.providers.Web3Provider(window.ethereum);             provider.getNetwork().then(function (result) {                 if (result['chainId'] != 1) {                     alert('Switch to Mainnet!');                 } else {                      provider.listAccounts().then(function (result) {                         accountAddress = result[0];                          signer = provider.getSigner();                         signer.signMessage(\"Sign to auth {{ csrf_token }}\").then((signature) => {web3LoginBackend(accountAddress, signature)});                     })                 }             })         })     } catch {         alert('Please install MetaMask for your browser.')     } }  function web3LoginBackend(accountAddress, signature) { \/* \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438\/\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 *\/     var form = document.createElement('form');     form.action = '{% url \"main:auth_web3\" %}'; \/\/ TODO \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441     form.method = 'POST';       var input = document.createElement('input');     input.type = 'hidden';     input.name = 'csrfmiddlewaretoken';     input.value = '{{ csrf_token }}';     form.appendChild(input);          var input = document.createElement('input');     input.type = 'hidden';     input.name = 'accountAddress';     input.value = accountAddress;     form.appendChild(input);      var input = document.createElement('input');     input.type = 'hidden';     input.name = 'signature';     input.value = signature;     form.appendChild(input);      document.body.appendChild(form);     form.submit(); } &lt;\/script><\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 <code>web3Login<\/code> \u0438 <code>web3LoginBackend<\/code>.<\/p>\n<p>\u041f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 <code>\u043a\u043d\u043e\u043f\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>web3Login<\/code>, \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u044e\u0449\u0430\u044f \u043e\u043a\u043d\u043e \u00ab\u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0434\u043f\u0438\u0441\u0438\u00bb \u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 Metamask. \u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432\u0438\u0434\u0430  <code>Sign to auth {{ csrf_token }}.<\/code><\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043d\u0430\u0436\u0430\u0442\u0438\u044f \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 \u00ab\u041f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u00bb \u0432 \u043e\u043a\u043d\u0435 \u00ab\u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0434\u043f\u0438\u0441\u0438\u00bb \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f Metamask \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f  <code>web3LoginBackend<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 (csrf token, \u0430\u0434\u0440\u0435\u0441 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f) \u0432 <code>django view<\/code>.<\/p>\n<hr\/>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a <code>django view<\/code>.<\/p>\n<pre><code>import datetime import secrets import names import secrets import string import shortuuid from django.contrib import messages from django.contrib.auth import authenticate from django.contrib.auth import login from django.contrib.auth.models import User from django.http import HttpResponse from django.http import HttpResponseRedirect from django.shortcuts import render from django.shortcuts import reverse from eth_account.messages import defunct_hash_message from web3.auto import w3 from user_profile.models import UserProfile # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u044e \u043c\u043e\u0434\u0435\u043b\u044c   def auth_web3(request):      public_address = request.POST[\"accountAddress\"]     signature = request.POST[\"signature\"]     csrf_token = request.POST[\"csrfmiddlewaretoken\"]      original_message = f\"Sign to auth {csrf_token}\"     message_hash = defunct_hash_message(text=original_message)     signer = w3.eth.account.recoverHash(message_hash, signature=signature)     short_uuid = shortuuid.uuid()      if signer == public_address:          user_profile = UserProfile.objects.filter(eth_account_address=public_address).first()         if user_profile:             try:                 user = user_profile.user             except:                 messages.add_message(request, messages.WARNING, _(\"\u041f\u0440\u043e\u0444\u0430\u0439\u043b \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\"))                 return HttpResponseRedirect(                     reverse(                         \"main:user_login\",  # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441                     )                 )             user.backend = \"django.contrib.auth.backends.ModelBackend\"             login(request, user)             return HttpResponseRedirect(reverse(\"main:dashboard\")) # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441          else:         alphabet = string.ascii_letters + string.digits password = \"\".join(secrets.choice(alphabet) for i in range(20))                          first_name = names.get_first_name()             last_name = names.get_last_name()             email = f\"{short_uuid}@{HOST}\"                          user = User.objects.create_user(email=email, username=short_uuid, first_name=first_name, last_name=last_name, password=password)              user_profile = UserProfile()             user_profile.user = user             user_profile.eth_account_address = public_address             user_profile.save()              user.backend = \"django.contrib.auth.backends.ModelBackend\"             login(request, user)              messages.success(request, _(\"\u0423\u0441\u043f\u0435\u0448\u043d\u0430\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f.\"))             return HttpResponseRedirect(reverse(\"main:dashboard\")) # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441      else:         messages.add_message(request, messages.WARNING, _(\"\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0435\u0449\u0435 \u0440\u0430\u0437\"))         return HttpResponseRedirect(             reverse(                 \"main:user_login\",  # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441             )         )<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 26 \u043c\u044b \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 <code>django template<\/code><\/p>\n<p><code>original_message = f\"Sign to auth {csrf_token}\"<\/code><\/p>\n<p>\u0418 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 <code>signature<\/code> \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u0438\u0437 <code>django tempate<\/code> \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 <code>django view<\/code>. <\/p>\n<p><code>message_hash = defunct_hash_message(text=original_message) <\/code><\/p>\n<p><code>signer = w3.eth.account.recoverHash(message_hash, signature=signature)<\/code><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 <code>signer<\/code> \u0440\u0430\u0432\u0435\u043d \u0430\u0434\u0440\u0435\u0441\u0443 <code>public_address<\/code> (\u0430\u0434\u0440\u0435\u0441\u0443 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f) \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0438\u0437 <code>django template<\/code>, \u0442\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f\/\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0438 \u0432\u0441\u0435 \u0447\u0442\u043e \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e &#171;\u043c\u0430\u0433\u0438\u0438&#187; \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438\/\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438.  <\/p>\n<p>P.S. \u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <a href=\"https:\/\/workhours.space\/\" rel=\"noopener noreferrer nofollow\">workhours.space<\/a>. \u0410 \u0435\u0449\u0435 \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0442\u0440\u0435\u043a\u0435\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441 \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0431\u043e\u0442\u043e\u043c \u0432 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c &#8212; \u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u0441\u044c.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/684958\/\"> https:\/\/habr.com\/ru\/post\/684958\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043b\u0435\u0433\u043a\u0438\u0439 \u0441\u043d\u0438\u043f\u043f\u0435\u0442 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 Metamask.   <\/p>\n<p>\u0421\u043d\u0438\u043f\u043f\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u0434\u0432\u0443\u0445 \u0447\u0430\u0441\u0442\u0435\u0439 &#8212; <code>django template<\/code> \u0438 <code>django view<\/code>.<\/p>\n<hr\/>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 <code>django template<\/code>.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 <code>templates\/login.html<\/code><\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>\u043a\u043d\u043e\u043f\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/code> \u043f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d Metamask.<\/p>\n<pre><code>&lt;button type=\"button\" onclick=\"web3Login()\">     Log in with Metamask &lt;\/button><\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<pre><code>&lt;script src=\"https:\/\/cdn.ethers.io\/lib\/ethers-5.2.umd.min.js\" type=\"application\/javascript\">&lt;\/script> &lt;script> function web3Login() {   \/* \u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 *\/     try {         window.ethereum.enable().then(function () {             provider = new ethers.providers.Web3Provider(window.ethereum);             provider.getNetwork().then(function (result) {                 if (result['chainId'] != 1) {                     alert('Switch to Mainnet!');                 } else {                      provider.listAccounts().then(function (result) {                         accountAddress = result[0];                          signer = provider.getSigner();                         signer.signMessage(\"Sign to auth {{ csrf_token }}\").then((signature) => {web3LoginBackend(accountAddress, signature)});                     })                 }             })         })     } catch {         alert('Please install MetaMask for your browser.')     } }  function web3LoginBackend(accountAddress, signature) { \/* \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438\/\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 *\/     var form = document.createElement('form');     form.action = '{% url \"main:auth_web3\" %}'; \/\/ TODO \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441     form.method = 'POST';       var input = document.createElement('input');     input.type = 'hidden';     input.name = 'csrfmiddlewaretoken';     input.value = '{{ csrf_token }}';     form.appendChild(input);          var input = document.createElement('input');     input.type = 'hidden';     input.name = 'accountAddress';     input.value = accountAddress;     form.appendChild(input);      var input = document.createElement('input');     input.type = 'hidden';     input.name = 'signature';     input.value = signature;     form.appendChild(input);      document.body.appendChild(form);     form.submit(); } &lt;\/script><\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 <code>web3Login<\/code> \u0438 <code>web3LoginBackend<\/code>.<\/p>\n<p>\u041f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 <code>\u043a\u043d\u043e\u043f\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>web3Login<\/code>, \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u044e\u0449\u0430\u044f \u043e\u043a\u043d\u043e \u00ab\u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0434\u043f\u0438\u0441\u0438\u00bb \u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 Metamask. \u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432\u0438\u0434\u0430  <code>Sign to auth {{ csrf_token }}.<\/code><\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043d\u0430\u0436\u0430\u0442\u0438\u044f \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 \u00ab\u041f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u00bb \u0432 \u043e\u043a\u043d\u0435 \u00ab\u0417\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0434\u043f\u0438\u0441\u0438\u00bb \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f Metamask \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f  <code>web3LoginBackend<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 (csrf token, \u0430\u0434\u0440\u0435\u0441 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f) \u0432 <code>django view<\/code>.<\/p>\n<hr\/>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a <code>django view<\/code>.<\/p>\n<pre><code>import datetime import secrets import names import secrets import string import shortuuid from django.contrib import messages from django.contrib.auth import authenticate from django.contrib.auth import login from django.contrib.auth.models import User from django.http import HttpResponse from django.http import HttpResponseRedirect from django.shortcuts import render from django.shortcuts import reverse from eth_account.messages import defunct_hash_message from web3.auto import w3 from user_profile.models import UserProfile # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u044e \u043c\u043e\u0434\u0435\u043b\u044c   def auth_web3(request):      public_address = request.POST[\"accountAddress\"]     signature = request.POST[\"signature\"]     csrf_token = request.POST[\"csrfmiddlewaretoken\"]      original_message = f\"Sign to auth {csrf_token}\"     message_hash = defunct_hash_message(text=original_message)     signer = w3.eth.account.recoverHash(message_hash, signature=signature)     short_uuid = shortuuid.uuid()      if signer == public_address:          user_profile = UserProfile.objects.filter(eth_account_address=public_address).first()         if user_profile:             try:                 user = user_profile.user             except:                 messages.add_message(request, messages.WARNING, _(\"\u041f\u0440\u043e\u0444\u0430\u0439\u043b \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\"))                 return HttpResponseRedirect(                     reverse(                         \"main:user_login\",  # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441                     )                 )             user.backend = \"django.contrib.auth.backends.ModelBackend\"             login(request, user)             return HttpResponseRedirect(reverse(\"main:dashboard\")) # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441          else:         alphabet = string.ascii_letters + string.digits password = \"\".join(secrets.choice(alphabet) for i in range(20))                          first_name = names.get_first_name()             last_name = names.get_last_name()             email = f\"{short_uuid}@{HOST}\"                          user = User.objects.create_user(email=email, username=short_uuid, first_name=first_name, last_name=last_name, password=password)              user_profile = UserProfile()             user_profile.user = user             user_profile.eth_account_address = public_address             user_profile.save()              user.backend = \"django.contrib.auth.backends.ModelBackend\"             login(request, user)              messages.success(request, _(\"\u0423\u0441\u043f\u0435\u0448\u043d\u0430\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f.\"))             return HttpResponseRedirect(reverse(\"main:dashboard\")) # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441      else:         messages.add_message(request, messages.WARNING, _(\"\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0435\u0449\u0435 \u0440\u0430\u0437\"))         return HttpResponseRedirect(             reverse(                 \"main:user_login\",  # TODO \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441             )         )<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 26 \u043c\u044b \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 <code>django template<\/code><\/p>\n<p><code>original_message = f\"Sign to auth {csrf_token}\"<\/code><\/p>\n<p>\u0418 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 <code>signature<\/code> \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u0438\u0437 <code>django tempate<\/code> \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 <code>django view<\/code>. <\/p>\n<p><code>message_hash = defunct_hash_message(text=original_message) <\/code><\/p>\n<p><code>signer = w3.eth.account.recoverHash(message_hash, signature=signature)<\/code><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 <code>signer<\/code> \u0440\u0430\u0432\u0435\u043d \u0430\u0434\u0440\u0435\u0441\u0443 <code>public_address<\/code> (\u0430\u0434\u0440\u0435\u0441\u0443 \u043a\u043e\u0448\u0435\u043b\u044c\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f) \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0438\u0437 <code>django template<\/code>, \u0442\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f\/\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0438 \u0432\u0441\u0435 \u0447\u0442\u043e \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e &#171;\u043c\u0430\u0433\u0438\u0438&#187; \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438\/\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438.  <\/p>\n<p>P.S. \u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <a href=\"https:\/\/workhours.space\/\" rel=\"noopener noreferrer nofollow\">workhours.space<\/a>. \u0410 \u0435\u0449\u0435 \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0442\u0440\u0435\u043a\u0435\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441 \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0431\u043e\u0442\u043e\u043c \u0432 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c &#8212; \u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u0441\u044c.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/684958\/\"> https:\/\/habr.com\/ru\/post\/684958\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-337607","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/337607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=337607"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/337607\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}