一般的に言えば、URL にユーザー名とパスワードだけを要求するのは悪い考えです。SSL を使用している場合でも、URL とそのすべてのクエリ パラメータは安全ではありません。フィードを安全にしたい場合は、HMAC-SHA1 や HMAC-SHA256 などのハッシュ方法を使用して、秘密鍵でリクエストに署名できます。こんな感じで動作します。
クライアントには、公開鍵と秘密鍵の 2 つの鍵が必要です
PublicKey: ABCDJJJZ7BW9P0IH2NI3
PrivateKey: uFehWXvcM7mf==c8ZhOe3Fz+6d+zyQ2ja4A3De1N
動詞、URL、ヘッダー、本文から文字列を作成します。ハッカーが効果的にリプレイ攻撃を実行するのを防ぐために、SSL を使用するか、URL に Expire パラメーターを含めることをお勧めします。日付とホストがヘッダーに含まれていることを確認してください。日付は、署名が常に変化していることを確認するために非常に重要です。
verb + \n
url + \n
header1: value + \n
header2: value + \n
body
次に、クライアントは、前述のハッシュ方法を使用してリクエストに署名します。擬似コードは次のようになります。
base64encode(hmac-sha1($private_key, $string_to_sign))
a ヘッダーは、署名と公開鍵を含むリクエストに追加されます
Authorization: $public_key:$signature
Authorization
サーバーがメッセージを受信すると、ヘッダーで受信した公開鍵を使用して、ユーザーの秘密鍵を取得します。次に、サーバーは同じアルゴリズムを実行し、2 つの署名が一致する場合、正しい公開鍵と秘密鍵を持つクライアントだけが同じ署名を生成できるため、サーバーは要求が認証されたことを認識します。
このアルゴリズムを使用してリクエストに署名する PHP コードの抜粋を次に示します。
public function sign() {
$time = time();
$date = gmdate(self::DATE_FORMAT_RFC2616, $time);
$verb = $this->payload['verb'];
$headers = $this->payload['headers'];
$query = $this->payload['query'];
$body = $this->payload['body'];
// Build the content string
$endpoint = $this->endpoint;
// Set the expiration time of the request
if($this->expires != 0 && !isset($query['Expires'])) {
$query['Expires'] = $time + ($this->expires * 60);
}
// Add the date to the request headers
$headers['date'] = $date;
// Add the host to the request headers
$headers['host'] = $this->host;
// Sort the query array and convert to querystring
uksort($query, 'strnatcasecmp');
$querystring = SUUtilities::to_query_string($query);
// Sort the headers to be sure they're in the right order
$modified_headers = array();
uksort($headers, 'strnatcasecmp');
// Concatinate the headers so the key and value are a single string
$include = array('date', 'host');
foreach($headers as $key => $value) {
if(in_array($key, $include) || substr($key, 0, 5) == "x-sbr") {
$modified_headers[] = preg_replace('/\s/', '', strtolower($key)).": ".$value;
}
}
// Build the string to sign
$string_to_sign = $verb.PHP_EOL
.$endpoint."?".$querystring.PHP_EOL
.implode(PHP_EOL, $modified_headers).PHP_EOL
.$body;
// Create the signature for the request
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->credentials['private_key'], true));
// Add the authorization header to the list of headers.
array_push($modified_headers, 'authorization: SBR '.$this->credentials['key'].':'.$signature);
$request = array();
$request['headers'] = $modified_headers;
$request['host'] = $this->host;
$request['endpoint'] = $endpoint;
$request['query'] = $query;
return $request;
}
その他のリソース: