从这个请求重定向URI不会从授权请求匹配一个即使它们是相同的
所以我用ORY /水润举办一个OAuth 2.0 Server和我在建设一个样本客户机来演示流的过程。我所说的/oauth2/auth
端点的查询参数的redirect_uri
,我用simple-oauth2
后来打电话/oauth2/token
来获取访问令牌。
我可以通过我的API创建客户端进入水润服务器和响应与回调URL的是`http://localhost:3000/callback”的一个有效的JSON
{
"id": "cbf09258-7f8e-4147-93c1-aa7e2e7b99b3",
"name": "Test App 1",
"clientId": "515e7876-881e-4f3a-b489-20ed7300c745",
"clientSecret": "deleted",
"clientSecretExpiresAt": 0,
"token":
"$2a$08$bWZMUf5wgEpOcoUjsJ5l/uS5LaTmqrC40FTnfegzelE69H8JAFrMW",
"callbackUrl": [
"127.0.0.1:3000",
"localhost:3000/callback",
"http://localhost:3000/callback"
],
"url": "",
"imageBanner": "",
"imageIcon": "",
"createdAt": "2019-02-04T19:14:22.193152Z",
"updatedAt": "2019-02-04T19:14:22.193152Z"
}
流程开始于localhost:3000/callback
以及和我的玉文件呈现一个链接调用/oauth2/auth
如下
block content
h1 Whew
a(href="http://localhost:4444/oauth2/auth?client_id=" + clientid + "&scope=openid offline&response_type=code&redirect_uri=http://localhost:3000/callback&state=haardik123") Authorize
最后,该处理器包括代码来调用/oauth2/token
如果code
参数是存在于所述查询,如下所示:(callback.js
)
const oauth2 = simpleOauthModule.create({
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
},
auth: {
tokenHost: 'http://localhost:4444',
tokenPath: '/oauth2/token',
authorizePath: '/oauth2/auth',
},
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: 'openid offline',
state: 'haardik123',
});
router.get('/', async function (req, res, next) {
var query = url.parse(req.url, true).query;
var clientid = process.env.CLIENT_ID;
var code = query.code;
const options = {
code,
};
if (code) {
try {
const result = await oauth2.authorizationCode.getToken(options);
console.log('The resulting token: ', result);
const token = oauth2.accessToken.create(result);
return res.status(200).json(token)
} catch(error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
}
res.render('callback', {
clientid: clientid
});
});
流程通常去,直到我得到重定向回用在查询参数一个localhost:3000/callback
到code
但后来它说Status 400: Bad request - Authentication Failed
水润日志显示
time="2019-02-04T19:16:05Z" level=info msg="started handling request" method=POST remote="172.29.0.1:35130" request=/oauth2/token
time="2019-02-04T19:16:05Z" level=error msg="An error occurred" description="The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed" error=invalid_request hint="The \"redirect_uri\" from this request does not match the one from the authorize request."
time="2019-02-04T19:16:05Z" level=info msg="completed handling request" measure#http://localhost:4444.latency=60183900 method=POST remote="172.29.0.1:35130" request=/oauth2/token status=400 text_status="Bad Request" took=60.1839ms
我不知道为什么REDIRECT_URI将不匹配,因为它好像我做的一切都是精 - 喜欢这个任何见解,谢谢!
回答如下:这是通过添加redirect_uri
到options
对象解决被传递到oauth2.authorizationCode.getToken(options)
更改对象
const options = {
code,
redirect_uri: "http://localhost:3000/callback",
};
成功了!