首页 文章

获取新的访问令牌时,不应替换oauth2中的刷新令牌

提问于
浏览
6

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

如果我正确理解刷新令牌的行为,当访问令牌到期时,我们应该调用我们的auth服务器的令牌 endpoints

'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();

1 回答

  • 5

    您是100%正确,刷新令牌的当前实现具有刷新令牌的滑动到期,因为每次使用grant_type = refresh_token时我们都会发出新的访问令牌和刷新令牌标识符,这对我的情况来说非常完美,因为我想要用户只要他正在使用该应用程序,如果他没有使用该应用程序超过刷新令牌的到期日期,那么当他尝试使用刷新获取新的访问令牌时,他将收到401令牌 .

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

相关问题