【问题标题】:Refresh tokens in oauth2 should not be replaced when getting a new access token获取新访问令牌时不应替换 oauth2 中的刷新令牌
【发布时间】:2015-02-27 18:57:38
【问题描述】:

标题中的陈述是否正确?我的问题是基于 Taiseer Joudeh 所做的工作(感谢您在这个主题上所做的工作,顺便说一句)http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/

如果我正确理解了刷新令牌的行为,当访问令牌过期时,我们应该使用类似的方式调用我们的身份验证服务器的令牌端点

'grant_type=refresh_token&refresh_token=' + token

我们将取回一个新的访问令牌。该请求将有一个刷新令牌作为有效负载的一部分,但该刷新令牌不应该与我们刚刚使用的相同吗?或者至少,它应该具有相同的到期时间?如果不是,那么真的,刷新生命周期是无限的。

这是我希望通过的 frisby.js 测试,但是使用 Taiseer 的 Web Api 2 实现,最终的期望失败了。我们得到一个新的刷新令牌,该令牌具有新的过期时间。

'use strict';

var frisby = require('frisby');
var config = require('../test-config.json');

var args = config[process.env.test || 'local'];
var host = args.host,
    clientId = args.clientId,
    usr = args.user1,
    pwd = args.password1;

frisby.create('Try and fail to get a protected resource')
    .get(host + '/api/test')
    .expectStatus(401)
    .expectHeaderContains('WWW-Authenticate', 'bearer')
    .toss();

frisby.create('Log in and get a protected resource')
    .post(host + '/token', {
        grant_type: 'password',
        username: usr,
        password: pwd,
        client_id: clientId
    })
    .expectJSONTypes({
        access_token: String,
        token_type: String,
        expires_in: Number,
        userName: String,
        refresh_token: String,
        'as:client_id': String,
        '.issued': String,
        '.expires': String
    })
    .expectJSON({
        token_type: 'bearer',
        userName: 'test2@test.com'
    })
    .afterJSON(function (json) {
        frisby.create('and now get protected resource with attached bearer token')
            .get(host + '/api/test', {
                headers: { 'Authorization': 'Bearer ' + json.access_token }
            })
            .expectStatus(200)
            .toss();
        frisby.create('and try to get a new access token with our refresh token')
            .post(host + '/token', {
                grant_type: 'refresh_token',
                refresh_token: json.refresh_token,
                client_id: clientId
            })
            .afterJSON(function (json2) {
                //we should receive a new access token
                expect(json.access_token).not.toEqual(json2.access_token);
                //but shouldn't the refresh token remain the same until *it* expires?
                expect(json.refresh_token).toEqual(json2.refresh_token);
            })
            .toss();
    })
    .toss();

【问题讨论】:

    标签: oauth refresh token asp.net-web-api2


    【解决方案1】:

    您是 100% 正确的,刷新令牌的当前实现具有刷新令牌的滑动到期时间,因为每次使用 grant_type=refresh_token 我们都会发出新的访问令牌和刷新令牌标识符,这非常适合我的情况,因为我希望用户在使用该应用程序时永远登录,如果他没有使用该应用程序的时间超过刷新令牌的到期日期,那么当他尝试获取新的访问令牌时他将收到 401使用刷新令牌。

    要更改此行为,您只需发出单个刷新令牌标识符并在用户使用刷新令牌请求新访问令牌时返回相同的标识符。您可以通过自定义此方法和 this 中的业务逻辑来做到这一点。

    【讨论】:

    • 谢谢,泰泽尔。这些东西并不容易,尤其是当 Katana 框架为我们做了这么多“魔法”时。仍然可能比我们自己写更好。我已将正在进行的工作推送到我的存储库 - 刷新提供程序在这里:github.com/finleysg/stats-api/blob/master/StatsApi/Providers/…。毫无疑问,仍然天真和不正确,但我的 frisby 测试通过了。您是否担心注销?我在帐户控制器中有一个简单的实现,它在注销操作时删除用户的所有刷新令牌。
    • @Taiseer Joudeh 如果用户在刷新令牌过期日期之后没有使用该应用程序,那么当他尝试使用刷新令牌获取新访问令牌时,他将收到 401。要更改此行为,您只需发出单个刷新令牌标识符并在用户使用刷新令牌请求新访问令牌时返回相同的标识符。我该怎么做?我还希望用户永远登录。可以你在 spring security oauth2 中展示了一些例子。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2017-04-14
    • 2012-06-29
    • 1970-01-01
    • 2013-03-01
    • 2015-07-19
    • 2017-03-22
    • 2021-07-08
    相关资源
    最近更新 更多