API認證方法綜述

Open api authentication

  1. Amazon
  2. DigitalOcean
  3. Webchat
  4. Weibo
  5. QQ

1. Amazon Web Services

HMAC Hash Message Authentication Code

核心思路

  • 【Client】以某種次序組合數(shù)據(jù)
  • 【Client】使用private key加密組合數(shù)據(jù)生成HMAC
  • 【Client】將數(shù)據(jù)發(fā)送至Server端,包括
    • 用戶標識信息,如API Key,Client ID, User ID;用以標識你是誰
    • Timestamp,避免重復攻擊
    • 生成的HMAC
    • 其他數(shù)據(jù)

  • 【Server】接收所有數(shù)據(jù)
  • 【Server】檢查Timestamp
  • 【Server】使用用戶標識信息,查詢private key
  • 【Server】從所有數(shù)據(jù)中取出HMAC
  • 【Server】以Client相同的次序組合數(shù)據(jù)
  • 【Server】使用private key加密組合數(shù)據(jù)生成HMAC
  • 【Server】匹配一致,認為請求合法

提示:private key不傳輸

http://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html

http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html

http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/

1.1 Amazon Simple Storage Service (One of AWS)

Developers are issued an AWS access key ID and AWS secret access key when they register.

The Amazon S3 REST API uses the standard HTTP Authorization header to pass authentication information.

Authorization: AWS AWSAccessKeyId:Signature

開發(fā)者在注冊的時候,會得到一個AWS access key ID和AWS secret access key。關于請求認證,
AWSAccessKeyId用來計算signature以及標識是哪一個開發(fā)者發(fā)起的請求。

Signature是請求中選定元素的RFC 2104 HMAC-SHA1算法生成的。

Authorization = "AWS" + " " + AWSAccessKeyId + ":" + Signature;

Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) )

http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheAuthenticationHeader

2. DigitalOcean

https

  • 首先使用用戶名密碼登錄管理平臺
  • 在管理平臺,生成OAuth Token(實際上代替了Username和Password)
  • api請求在HTTP header中攜帶OAuth Token

curl例子

curl -X $HTTP_METHOD -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/$OBJECT"

curl -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"name": "example.com", "ip_address": "127.0.0.1"}' -X POST "https://api.digitalocean.com/v2/domains"

https://developers.digitalocean.com/documentation/v2/#authentication

3. Webchat

微信開發(fā)接口調用憑據(jù)

步驟

  • 【Client】獲取access_token,發(fā)送數(shù)據(jù)包括
    • appid
    • secret
  • 其他api調用均需要access_token(access_token有效期為兩小時,需要定時獲?。?/li>

http://wiki.connect.qq.com/openapi調用說明_oauth2-0

4. Weibo

無需登錄授權的api直接使用

需要登錄授權的使用OAuth2協(xié)議,獲取到access_token,發(fā)送請求時提供access_token

http://open.weibo.com/wiki/授權機制說明

5. QQ

OAuth2協(xié)議

http://wiki.connect.qq.com/oauth2-0簡介

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容