Skip to content

Instantly share code, notes, and snippets.

@elsonwu
Last active August 29, 2015 14:02
Show Gist options
  • Save elsonwu/8688a1f597ef1304eabf to your computer and use it in GitHub Desktop.
Save elsonwu/8688a1f597ef1304eabf to your computer and use it in GitHub Desktop.
第三方应用验证流程

约定:

app: 代表第三方应用

server: 代表api服务器

申请token

  • 申请一个app时会得到client_id和secret_key,这个secret_key只有app和server知道

  • 当一个app需要以某个用户申请读取其数据,需要得到用户授权

  • 授权页面需要client_id和secret_key

  • 用户输入账号密码登陆并授权该应用,则返回user_id和token给app

请求api验证

  • 之后app每请求一个api都必须带上下面的参数: client_id, user_id, token, signature, timestamp, nonce

  • 其中timestamp是时间戳,nonce是随机数,signature是app通过把timestamp, nonce, token和secret_key一起加密(例如SHA1等)算出

  • server的api开始验证

    • 验证timestamp是否过期,假定是5秒内有效,因为要考虑网路慢的情况
    • 通过client_id和user_id找出app的信息
    • 判断传来的token是否存在并在有效期内
    • 把传过来的timestamp,nonce, token还有app对应的secret_key重算signature,如果两个signature相同,则验证通过
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment