简体   繁体   English

PHP oAuth签名无效-Apple DEP

[英]PHP oAuth Signature Invalid - 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. 我正在尝试从Apple的DEP系统获取我正在使用PHP的MDM Server的auth_session_token。 I am so close but for some reason keep getting the following response from Apple's server: 我是如此亲密,但由于某种原因,请继续从Apple的服务器获得以下响应:

signature_invalidUnauthorized

I have tried a slue of different things from doing research online and trying different methods people have used regarding oAuth. 从在线研究和尝试人们对oAuth使用的不同方法,我尝试了各种不同的方法。 But there sadly hasn't been anything specific regarding Apple's DEP servers oAuth. 但是令人遗憾的是,关于Apple的DEP服务器oAuth并没有任何具体的规定。

A lot of people recommend using an oAuth class, but none of which support Apple's system. 许多人建议使用oAuth类,但都不支持Apple的系统。 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. 我采用了在我的PHP代码中使用cURL的方法,这看起来很好用,但是我无法正确获得签名。

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. 所以$ RFC3986signature最终是我在对oauth_signature参数的正式请求中发送的,但最终并没有被接受。

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... 在DEP Portal中添加服务器时,我尝试使用从Apple获得的不同秘密和/或令牌进行签名,尝试使用由&符号分隔的多个代码/秘密,将它们翻转等等,但是...一样...

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! 因此,在生成签名时,请务必确保参数按字母顺序排列!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM