DJANGO small program - to get user information 02 by openid

Given official micro-channel decryption algorithm, download PYTHON, there will be two files, one demo.js, one is WX ****. Js

The last chapter has been posted before.

Create a new project in WXBizDataCrypt.py APP, paste into micro letter to the WXBizDataCrypt.py document everything.

import base64
import json
from Crypto.Cipher import AES

class WXBizDataCrypt:
    def __init__(self, appId, sessionKey):
        self.appId = appId
        self.sessionKey = sessionKey

    def decrypt(self, encryptedData, iv):
        # base64 decode
        sessionKey = base64.b64decode(self.sessionKey)
        encryptedData = base64.b64decode(encryptedData)
        iv = base64.b64decode(iv)

        cipher = AES.new(sessionKey, AES.MODE_CBC, iv)

        decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData)))

        if decrypted['watermark']['appid'] != self.appId:
            raise Exception('Invalid Buffer')

        return decrypted

    def _unpad(self, s):
        return s[:-ord(s[len(s)-1:])]

Intact.
In wx_api.py, the write view.

@api_view(['GET','POST'])
def wx_jiemi(request):
    appId = request.GET['appId']
    sessionKey = request.GET['sessionKey']
    encryptedData = request.GET['encryptedData']
    iv = request.GET['iv']
    pc =  WXBizDataCrypt(appId,sessionKey)
    userinfo = pc.decrypt(encryptedData, iv)
    print(userinfo)

Userinfo print out the relevant information, and do not make the presentation.

Openid with the userinfo then admin.username USER in contrast, any direct login, openid no words to add to the account and password of admin, stored in SQL, additional information as to expand the information of the user. Thus the front and rear ends Django applet The cross completed.

We operate under a small program ends swpier get django carousel Fig.

If you have questions can leave a message below.

Published 12 original articles · won praise 0 · Views 185

Guess you like

Origin blog.csdn.net/weixin_44675051/article/details/104894386