333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
/**
 * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * @file devAuthToken
 * @author baiduAip
 */
const devScope = require('../const/devScope');
 
/**
 * 提前获取access_token的时间 默认24个小时
 *
 * @type {number}
 */
let DEFAULT_FETCH_AHEAD_DURATION = 24 * 60 * 60 * 1000;
 
 /**
 * devAuthToken类
 * 百度开发者token信息包装类
 *
 * @constructor
 * @param {string} token access_token
 * @param {number} expireTime 多久之后过期
 * @param {string} scope 权限
 */
class DevAuthToken {
    constructor(token, expireTime, scope) {
        this.token = token;
        this.expireTime = expireTime;
        this.scope = scope;
        this.authDate = new Date();
        this.hasScopeFlag = false;
        this.initScope();
    }
    initScope() {
        // 用户自建token,默认为有权限
        if (this.scope == null) {
            this.hasScopeFlag = true;
            return;
        }
        let scopeArray = this.scope.split(' ');
        scopeArray.forEach(function (item) {
            if (item === devScope) {
                this.hasScopeFlag = true;
            }
        }.bind(this));
    }
    hasScope(scope) {
        return this.hasScopeFlag;
    }
    isExpired() {
        let now = new Date();
        // 根据服务器返回的access_token过期时间,提前重新获取token
        if (now.getTime(this.expireTime) -
            this.authDate.getTime() > this.expireTime * 1000 -
                DEFAULT_FETCH_AHEAD_DURATION) {
            return true;
        }
        return false;
    }
}
 
/**
 * 设置提前获取access_token的时间
 */
DevAuthToken.setExpireAhead = function (duration) {
    DEFAULT_FETCH_AHEAD_DURATION = duration;
}
 
DevAuthToken.DEFAULT_EXPIRE_DURATION = 2592000 * 1000;
 
module.exports = DevAuthToken;