我在获取OAuth2与通用OAuth2提供者一起工作时遇到了很多问题。这是情况。OAuth2与卫星和通用OAuth2提供者
服务提供了一个OAuth2身份验证方法,用于我要授权的位置。我创建了一个AngularJS应用程序,有satellizer以下配置:
authProvider.baseUrl = 'http://localhost:3030/user/authorize';
$authProvider.oauth2({
name: 'customname',
url: '/token',
clientId: 'someapp',
requiredUrlParams: ['scope'],
scope: ['profile'],
authorizationEndpoint: 'http://location.to.oathserver',
redirectUri: 'http://localhost:3000'
});
的的baseUrl指向应处理中间件部分我的节点服务器。
我也有下面的代码触发验证部分。
$scope.authenticate = function(provider) {
$auth.authenticate(provider)
.then(function(response) {
console.log(response);
})
.catch(function() {
//something went wrong
});
}
到目前为止,这一切似乎都很好,看起来与Satellizer记录的非常相似!现在,一旦我启动角度应用程序并开始身份验证,就会看到目标Node服务发出的请求。
Next我有我的node.js服务挂钩到'用户/授权/令牌'URL。下面的代码:
router.options('/authorize/token', function(req, res, next) {
//var token = req.header('Authorization').split(' ')[1];
res.end();
});
和:
router.post('/authorize/token', function(req, res, next) {
var authCode = req.param('code');
var cliendId = req.param('clientId');
var payload = jwt.decode(authCode, 'mySecret');
});
在此处,这一切似乎出问题。首先我似乎得到一个OPTIONS请求。我真的不知道该如何处理它,因为我似乎无法在有关OPTIONS请求的文档中找到任何内容。我认为它可能会包含'授权'标题,但似乎并非如此,所以我用res.end()关闭了连接。
我也检查了Chrome中的请求,但我似乎无法找到具有此确切名称的标头。
接下来我得到一个POST请求。这似乎包含一些东西,万岁!我得到以下对象:
{
code: "ZFFeat9pWfHzw4rGmjFYwucPRMFnBOkd2odEObvo",
cliendId: "someapp",
redirectiUri: "http://localhost:3000"
}
这在我看来像我应该有解码的授权码。这就是你看到我在上面的代码中试着做的。不幸的是,这似乎把我的错误
Error: Not enough or too many segments
这告诉我,我做的可能出错了,我卡住了。
我确实有一些似乎适用于其他人的PHP代码,但我并没有完全理解并且不能真正将代码与我的代码联系起来,因为PHP不是我的专业,node.js/JavaScript也不是他的代码。所以在这里不用PHP代码:
handle_cors(); // Handle CORS for cross domain requests
// Get JSON data
$input = json_decode(file_get_contents("php://input"), true);
// Create Provider
$provider = new SomeApp\OAuth2\Client\Provider\SomeApp([
'clientId' => 'someapp',
'clientSecret' => 'mySecret',
'redirectUri' => $input['redirectUri'],
]);
// Optional: Now you have a token you can look up a users profile data
try {
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $input['code']
]);
// We got an access token, let's now get the user's details
$user = $provider->getResourceOwner($token);
header('Content-Type: application/json');
$result = $user->toArray();
$result['token'] = create_token('my-example-key', $user->getId());
echo json_encode($result);
exit();
} catch (Exception $e) {
// Failed to get user details
exit('Oh dear...' . $e->getMessage());
}
希望有人能帮助我!提前致谢。