Azard / egg-oauth2-server

:star2: OAuth2 server plugin for egg.js based on node-oauth2-server
MIT License
179 stars 45 forks source link

options.headers.hasOwnProperty is not a function #11

Closed chenyulun closed 6 years ago

chenyulun commented 6 years ago

2017-09-21 16:37:56,696 ERROR 32793 [-/127.0.0.1/-/10737ms GET /user/token] nodejs.TypeError: options.headers.hasOwnProperty is not a function at new Response (/Users/tusm/github/mygithub/egg-cms/node_modules/oauth2-server/lib/response.js:16:25) at Object. (/Users/tusm/github/mygithub/egg-cms/node_modules/egg-oauth2-server/lib/server.js:63:24) at Generator.next () at Object.dispatch (/Users/tusm/github/mygithub/egg-cms/node_modules/koa-router/lib/router.js:334:12) at dispatch.next () at onFulfilled (/Users/tusm/github/mygithub/egg-cms/node_modules/co/index.js:65:19) at /Users/tusm/github/mygithub/egg-cms/node_modules/co/index.js:54:5 at Promise () at Object.co (/Users/tusm/github/mygithub/egg-cms/node_modules/co/index.js:50:10)

// Store the headers in lower case.
  for (var field in options.headers) {
    if (options.headers.hasOwnProperty(field)) {
      this.headers[field.toLowerCase()] = options.headers[field];
    }
  }

‌‌typeof this.ctx.response.headers ‌object ‌‌typeof this.ctx.response.proto ‌object ‌‌typeof this.ctx.response.headers.proto ‌undefined tips: egg issues

Azard commented 6 years ago

ctx.response.header not contain hasOwnProperty in egg.js

Use follow code is OK

for (const field in options.headers) {
  this.headers[field.toLowerCase()] = options.headers[field];
}
chenyulun commented 6 years ago

这个文件不是我写的,是你底层应用的node-oauth2-server报的错误

Azard commented 6 years ago

我试试...

thonatos commented 6 years ago

@chenyulun

12 里你示例了,方便的话提供一下复现的代码,

之前遇到过这个问题,但后来没复现过,所以没管了

Azard commented 6 years ago

这个可以在插件里包装一下 response.header ,不过不确定是否有这个必要

thonatos commented 6 years ago

之前说的也许有问题,不误导群众

chenyulun commented 6 years ago

怎么在query传参? response.headers是不可重新的属性 Cannot set property headers of #<Object> which has only a getter

chenyulun commented 6 years ago

报错的是

app.all('/oauth2/token', oauth.token());

你添加到下面两个路由没用啊,上面这个还报错; 请求js

fetch('http://127.0.0.1:7001/user/token',{method: 'POST', mode:'cors',credentials: 'include',headers:{"Content-Type":"application/x-www-form-urlencoded",Authorization: 'Basic bXlfYXBwOm15X3NlY3JldA=='},body:'username=egg-oauth2-server&password=azard&grant_type=password'}) .then(function(res){return res.json()}).then(function(res){console.log(res)})
chenyulun commented 6 years ago

@Azard 我觉得你不要用他的Response,因为只要你用他的Response传入了egg的ctx.response必报错,重新你自己的Response: /node_modules/egg-oauth2-server/lib/server.js 5行

const Response = require('oauth2-server').Response;
  // Store the headers in lower case.
  for (var field in options.headers) {
    if (options.headers.hasOwnProperty(field)) {
      this.headers[field.toLowerCase()] = options.headers[field];
    }
  }

  // Store additional properties of the response object passed in
  for (var property in options) {
    if (options.hasOwnProperty(property) && !this[property]) {
      this[property] = options[property];
    }
  }

而且感觉上面两个函数都是反过来写的; 后面那个函数把this.headers覆盖了 我顺便看了一下egg的ctx.request.headers存在原型链,不会报错

thonatos commented 6 years ago

password

curl --request POST \
  --url http://localhost:7001/oauth2/token \
  --header 'authorization: Basic bXlfYXBwOm15X3NlY3JldA==' \
  --header 'cache-control: no-cache' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data 'username=0015&password=123456&grant_type=password'

# return
{
    "access_token":"76d8b75662061c2a91841597c15343f6fe5b3631",
    "token_type":"Bearer",
    "expires_in":3599,
    "refresh_token":"2ec9774a432cc30cb94f591a472edb82beb57ea4"
}

trace

console.trace('#response', options.headers.hasOwnProperty);

node_moauth-server
Trace: #response function hasOwnProperty() { [native code] }
    at new Response (/Users/thonatos/workspace/arashivision/account-egg/node_modules/oauth2-server/lib/response.js:10:11)
    at Object.<anonymous> (/Users/thonatos/workspace/arashivision/account-egg/node_modules/egg-oauth2-server-impl/lib/server.js:64:24)
    at Generator.next (<anonymous>)
    at Object.dispatch (/Users/thonatos/workspace/arashivision/account-egg/node_modules/koa-router/lib/router.js:334:12)
    at dispatch.next (<anonymous>)
    at onFulfilled (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:65:19)
    at /Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:54:5
    at Promise (<anonymous>)
    at Object.co (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:118:63)
    at next (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:99:29)
    at onFulfilled (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:69:7)
    at /Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:54:5
    at Promise (<anonymous>)
    at Object.co (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:118:63)
    at next (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:99:29)
    at onFulfilled (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:69:7)
    at /Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:54:5
    at Promise (<anonymous>)
    at Object.co (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:118:63)
    at next (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:99:29)
    at onFulfilled (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:69:7)
    at /Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:54:5
    at Promise (<anonymous>)
    at Object.co (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/thonatos/workspace/arashivision/account-egg/node_modules/co/index.js:118:63)

我已经验证很多次没有问题了 如果没有有效的复现路径,这个 issue 我取消订阅了

chenyulun commented 6 years ago
"dependencies": {
    "egg": "^1.7.0",
    "egg-cors": "^1.2.0",
    "egg-mongoose": "^2.1.0",
    "egg-oauth2-server": "^2.0.2",
    "egg-scripts": "^1.1.1",
    "egg-validate": "^1.0.0"
  },
2017-09-25 2 13 20 2017-09-25 2 08 30
Azard commented 6 years ago

@chenyulun 有什么好的解决方案吗,不行就退回到 egg-oauth2-server 1.x 版本

chenyulun commented 6 years ago

@Azard 是你的版本问题吗? 看他的response源代码可以看出他使用了response的下的所有自己的属性;你只要自己提取出来在new,具体修改方案 修改文件为:egg-oauth2-server/lib/server.js 行号63行替换为:

const newResponse = {
        headers: {}
      };
      const res = ctx.response
      for (var property in res) {
        if (res.hasOwnProperty(property) && !newResponse[property]) {
          newResponse[property] = res[property];
        }
      }
      for (var field in res.headers) {
          newResponse.headers[field.toLowerCase()] = res.headers[field];
      }
      const response = new Response(newResponse);

2017-09-25 3 02 13

Azard commented 6 years ago

good https://github.com/Azard/egg-oauth2-server/commit/ef85b911c6856d0a52fa662d6a80a030b02d9c6b

chenyulun commented 6 years ago

请发npm更新包2.0.3

Azard commented 6 years ago

@chenyulun done