douhua1760 2016-12-31 10:34
浏览 41

PHP oAuth签名无效 - Apple DEP

I am trying to get my auth_session_token from Apple's DEP system for a MDM Server I am working on with PHP. I am so close but for some reason keep getting the following response from Apple's server:

signature_invalidUnauthorized

I have tried a slue of different things from doing research online and trying different methods people have used regarding oAuth. But there sadly hasn't been anything specific regarding Apple's DEP servers oAuth.

A lot of people recommend using an oAuth class, but none of which support Apple's system. So it looks like it all needs to be done manually.

I have taken the approach of using cURL with my PHP code, this appears to work fine, minus the fact that I can't get the signature right.

Here is how I am trying to create the signature (and again I have tried a slue of different methods so this is my most recent attempt):

function rfc3986_encode($string) {
    $result = rawurlencode($string);
    $result = str_replace('%7E', '~', $result);
    $result = str_replace('=', '%3D', $result);
    $result = str_replace('+', '%2B', $result);

    return $result;
}

$consumer = "CK_REDACTED";
$secret = "CS_REDACTED";
$secret2 = "AS_REDACTED";
$token = "AT_REDACTED";
$sign_method = "HMAC-SHA1";
$version = "1.0";
$url = "REDACTED";
$path = "REDACTED";

$timestamp = strtotime('now');
$mt = microtime();
$rand = mt_rand();
$nonce = md5($mt.$rand);

$post = array(
    'oauth_consumer_key' => rfc3986_encode($consumer),
    'oauth_token' => rfc3986_encode($token),
    'oauth_signature_method' => rfc3986_encode($sign_method),
    'oauth_timestamp' => rfc3986_encode($timestamp),
    'oauth_nonce' => rfc3986_encode($nonce),
    'oauth_version' => rfc3986_encode($version)
);

$signatureParameters = array();
foreach ($post as $parameter => $value) {
    $signatureParameters[] = rfc3986_encode($parameter) . '=' . rfc3986_encode($value);
}

$signatureParameters = implode('&', $signatureParameters);

$baseString = "GET"
             ."&".rfc3986_encode($url)
             ."&".rfc3986_encode($signatureParameters);

$key = rfc3986_encode($consumer) ."&";

$signature = base64_encode(hash_hmac('sha1', $baseString, $key));
$RFC3986signature = rfc3986_encode($signature);

So $RFC3986signature is what I end up sending in the official request for the oauth_signature parameter, but it doesn't end up getting accepted.

Does anybody know how to solve this? I have tried signing with the different secrets and / or tokens from above as obtained from Apple when adding my server in the DEP Portal, tried using multiple codes / secrets separated by the & symbol, flipped them around, and so on...but same thing...

展开全部

  • 写回答

1条回答 默认 最新

  • dpsu84620 2016-12-31 19:33
    关注

    I was able to figure this out after long time testing and trying and restarting and frustration. Turns out in my signature, before generating it, I didn't have the parameters listed in alphabetical order, and thus the signature was a mismatch.

    So the key to this, when generating your signature, make absolutely sure your parameters are in alphabetical order!

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部