当前位置: 首页 > news >正文

苹果账号登录后端验证两种方式 python2

import time
import jwt  
import requests  
import json 
import base64def decode_jwt(jwt_token):try:h,p,s = jwt_token.split('.')except:return {},{},{},"","",""header = json.loads(base64.urlsafe_b64decode(h + '==='))  # 可能需要调整填充payload = json.loads(base64.urlsafe_b64decode(p + '==='))  # 可能需要调整填充sign = base64.urlsafe_b64decode(s + '===')  # 可能需要调整填充return header,payload,sign,h,p,sdef auth_apple_by_token(client_id,client_secret,code):"""使用token方式 """url = "https://appleid.apple.com/auth/token"params = {'client_id':client_id,'client_secret':client_secret,'code':code,'grant_type':"authorization_code",}headers = {'Content-Type': 'application/x-www-form-urlencoded'}r = requests.post(url,data=params,headers=headers)print r.status_codeprint r.json()if r.status_code != 200:returnrjson = r.json()id_token,access_token = rjson['id_token'],rjson['access_token']header,payload,sign,h,p,s = decode_jwt(id_token)def auth_apple_by_keys(id_token):"""使用keys 方式"""    header,payload,sign,h,p,s = decode_jwt(id_token)url = "https://appleid.apple.com/auth/keys"r = requests.get(url)keys = r.json()['keys']n,e = '',''for x in keys:if x['kid'] == header['kid']:n,e = x['n'],x['e']breakif not n:n = keys[0]['n']e = keys[0]['e']n_bytes = base64.urlsafe_b64decode(str(n)+"===")e_bytes = base64.urlsafe_b64decode(str(e)+"===")n,e = 0,0for byte in e_bytes:e = (e << 8) | ord(byte)for byte in n_bytes:n = (n << 8) | ord(byte)from cryptography.hazmat.backends import default_backendfrom cryptography.hazmat.primitives import serialization,hashesfrom cryptography.hazmat.primitives.asymmetric import rsa, padding#构建公钥public_key = rsa.RSAPublicNumbers(e, n).public_key(default_backend())# 如果需要,可以将公钥序列化为PEM格式pem = public_key.public_bytes(serialization.Encoding.PEM,serialization.PublicFormat.SubjectPublicKeyInfo)#print(pem)try:decoded = jwt.decode(id_token, key=pem, algorithms=['RS256'],verify=True,audience=payload['aud'])print 'valid1'except Exception as e:print 'invalid1'# 另一种验证方式# hp = h + "." + p# now_ts = time.time()# try:#     public_key.verify(sign,hp,padding.PKCS1v15(),hashes.SHA256())#     if now_ts <= payload['exp']:#         print 'valid2'#     else:#         print 'invalid22'# except Exception as e:#     print 'invalid2',etoken = ""auth_apple_by_keys(token)

相关链接 : https://www.jianshu.com/p/655972b0e7da


http://www.mrgr.cn/news/22930.html

相关文章:

  • Qt工程使用MQTT-C库与mqtt服务器数据通信
  • 【C++11 ——— 右值引用和移动语义】
  • 久久派搭建风电系统网站(基于mariadb数据库)
  • PVE动态核显直通
  • 一个数组向左移动i位(学会分析问题,找出规律,不要小看任何一个小程序;小程序都是实现大的功能的基础--体现问题分解的思想)
  • Python数据分析高频面试题及答案
  • 使用gdb跟踪调试linux内核
  • 仕考网:公务员国考考什么?
  • 三维点云骨架提取(以树木为例 python 代码)
  • 月考结束老师如何发布成绩查询?
  • 时间戳和日期相互转换+检验日期合法性功能C语言
  • 推荐4款高效的录屏工具,教你如何快速录屏。
  • 风趣图解LLMs RAG的15种设计模式-第三课
  • gpt4最新保姆级教程
  • 一些面试和找工作的技巧-新资要的低并不会给你加分薪资要的高不会成为公司拒绝你的核心理由
  • OZON商品免收仓储费,OZON隐藏被取消订单的评论
  • 驾驭复杂市场,商品计划软件:企业制胜的智囊团
  • web基础之信息泄露
  • 茶则电子秤方案设计
  • 多个pdf怎么合并成一个pdf?推荐5种方法轻松合并pdf文件