1

我正在 webpy 的开发 Web 服务器上使用 python-openid 测试 OpenID 身份验证。通过雅虎!和 myOpenID,我不断收到消息Server denied check_authentication的失败响应。奇怪的是,我也收到了正确的openid.identity.

相同类型的身份验证适用于 Google (@ https://www.google.com/accounts/o8/ud ...)。一方面,这让我相信我做的事情是正确的,但另一方面,这种不一致让我感到困惑。

return_to&trust_root都是 localhost:8080,这可能与它有关。

这是我用来将用户发送到 Yahoo! 的代码 进行身份验证:

  def POST(self):
    post_data = web.input()
    if post_data.has_key('openid_identifier'):
      openid_identifier = post_data.get('openid_identifier')
      c = Consumer(session, openid.store.memstore.MemoryStore())
      auth = c.begin(openid_identifier)
      auth_url = auth.redirectURL('http://localhost:8080', return_to='http://localhost:8080/authenticate')
      raise web.seeother(auth_url)
    return post_data

auth_url在这种情况下设置为(格式化以便于阅读):

https://open.login.yahooapis.com/openid/op/auth?
openid.assoc_handle=cYSO3wJSjQa3ewmRpaQz3YodzqjosP1ta.4TVzumqlLpAFM7oWci6K9bMKG4uuqZ.5m.fY7Wp8BWfQ1eR_soHWpJ6gCsKtxi_7Bqi22T5RUcMIuQBVjpGFSjc_kRY2k-&
openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&
openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&
openid.mode=checkid_setup&
openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.realm=http%3A%2F%2Flocalhost%3A8080&
openid.return_to=http%3A%2F%2Flocalhost%3A8080%2Fauthenticate%3Fjanrain_nonce%3D2010-10-08T02%253A56%253A04ZrxAI

以下是处理程序在返回 URL 中的样子:

  def GET(self):
    data = web.input()
    c = Consumer(session, openid.store.memstore.MemoryStore())
    result = c.complete(dict(data), current_url='http://localhost:8080/authenticate')
    if result.status == SUCCESS:
      openid_identity = data.get('openid.identity')
      ...
    render = web.template.render('templates/', base='layout')
    return render.error(...)

result设置为<openid.consumer.consumer.FailureResponse id=None message='Server denied check_authentication'>,并且data(返回的查询参数)设置如下:

<Storage {'openid.op_endpoint': u'https://open.login.yahooapis.com/openid/op/auth', 
'openid.sig': u'yCHffpHs2Whtw9p1gPzC+ToQJ0k=', 
'openid.ns': u'http://specs.openid.net/auth/2.0', 
'janrain_nonce': u'2010-10-08T02:56:04ZrxAIWh', 
'openid.return_to': u'http://localhost:8080/authenticate?janrain_nonce=2010-10-08T02%3A56%3A04ZrxAIWh', 
'openid.pape.auth_level.nist': u'0', 
'openid.claimed_id': u'https://me.yahoo.com/a/d3eEQZAWydfmtDwaGB2vBEVU4vIMLsez#1ac56', 
'openid.mode': u'id_res', 
'openid.realm': u'http://localhost:8080', 
'openid.response_nonce': u'2010-10-08T02:55:52ZRLNmEd7aWiaGWjHfhqEQs2Fxj3.nXdwciA--', 
'openid.signed': u'assoc_handle,claimed_id,identity,mode,ns,op_endpoint,response_nonce,return_to,signed,pape.auth_level.nist', 
'openid.identity': u'https://me.yahoo.com/a/d3eEQZAWydfmtDwaGB2vBEVU4vIMLsez', 
'openid.assoc_handle': u'cYSO3wJSjQa3ewmRpaQz3YodzqjosP1ta.4TVzumqlLpAFM7oWci6K9bMKG4uuqZ.5m.fY7Wp8BWfQ1eR_soHWpJ6gCsKtxi_7Bqi22T5RUcMIuQBVjpGFSjc_kRY2k-'}>

这肯定看起来不像是对我的失败回应。请注意openid.identity已设置。是的,那是我在 Yahoo! 上的 OpenID 身份。

我不知道从哪里拿这个。有什么建议吗?

4

1 回答 1

3

消费者需要一个数据存储来维护发现和身份验证之间的状态。我使用的商店,openid.store.memstore.MemoryStore()实际上并没有维护请求之间的状态。它只在进程中维护状态——正如您对“内存”所期望的那样(duh)。必须改变的一点是在 GET 和 POST 处理程序中创建消费者。

这是创建消费者的错误方法:

# BAD: MemoryStore() has a short memory -- within the process only
c = Consumer(session, openid.store.memstore.MemoryStore())

这是创建消费者的正确方法:

# GOOD: MySQL has a long memory -- across processes
db = web.database(dbn='mysql', db='somedb', user='someuser', pw='')
conn = db._db_cursor().connection
cstore = sqlstore.MySQLStore(conn, 'openid_associations', 'openid_nonces')
c = Consumer(session, cstore)

我想这有助于记住您的关联句柄和随机数。我一定在这里被困了 10 个小时,所以我希望这有助于下一个人(或女孩)避免这样做。

这将是我赢得的第一个赏金——我自己的。哇!

临别说明:假设您已经在数据库中设置了 OpenID 表,在 MySQL 中应该如下所示:

create table openid_nonces (
  server_url blob not null,
  timestamp integer not null,
  salt char(40) not null,
  primary key (server_url(255), timestamp, salt)
) engine=InnoDB;

create table openid_associations (
  server_url blob not null,
  handle varchar(255) not null,
  secret blob not null,
  issued integer not null,
  lifetime integer not null,
  assoc_type varchar(64) not null,
  primary key (server_url(255), handle)
) engine=InnoDB;

检查文档的 openid.store.sqlstore 部分以获取特定商店的相关 SQL 语句。

于 2010-10-10T06:07:26.143 回答