@twurple/auth
Version:
Authenticate with Twitch and stop caring about refreshing tokens.
103 lines (102 loc) • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StaticAuthProvider = void 0;
const tslib_1 = require("tslib");
const shared_utils_1 = require("@d-fischer/shared-utils");
const common_1 = require("@twurple/common");
const helpers_1 = require("../helpers");
/**
* An auth provider that always returns the same initially given credentials.
*
* You are advised to roll your own auth provider that can handle scope upgrades,
* or to plan ahead and supply only access tokens that account for all scopes
* you will ever need.
*/
let StaticAuthProvider = class StaticAuthProvider {
/**
* Creates a new auth provider with static credentials.
*
* @param clientId The client ID of your application.
* @param accessToken The access token to provide.
*
* You need to obtain one using one of the [Twitch OAuth flows](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/).
* @param scopes The scopes the supplied token has.
*
* If this argument is given, the scopes need to be correct, or weird things might happen. If it's not (i.e. it's `undefined`), we fetch the correct scopes for you.
*
* If you can't exactly say which scopes your token has, don't use this parameter/set it to `undefined`.
*/
constructor(clientId, accessToken, scopes) {
this._clientId = clientId || '';
this._accessToken =
typeof accessToken === 'string'
? {
accessToken,
refreshToken: null,
scope: scopes !== null && scopes !== void 0 ? scopes : [],
expiresIn: null,
obtainmentTimestamp: Date.now(),
}
: accessToken;
this._scopes = scopes;
}
/**
* The client ID.
*/
get clientId() {
return this._clientId;
}
/**
* Gets the static access token.
*
* If the current access token does not have the requested scopes, this method throws.
* This makes supplying an access token with the correct scopes from the beginning necessary.
*
* @param user Ignored.
* @param scopeSets The requested scopes.
*/
async getAccessTokenForUser(user, ...scopeSets) {
return await this._getAccessToken(scopeSets);
}
/**
* Gets the static access token.
*
* If the current access token does not have the requested scopes, this method throws.
* This makes supplying an access token with the correct scopes from the beginning necessary.
*
* @param intent Ignored.
* @param scopeSets The requested scopes.
*/
async getAccessTokenForIntent(intent, ...scopeSets) {
return await this._getAccessToken(scopeSets);
}
/**
* Gets the static access token.
*/
async getAnyAccessToken() {
return await this._getAccessToken();
}
/**
* The scopes that are currently available using the access token.
*/
getCurrentScopesForUser() {
var _a;
return (_a = this._scopes) !== null && _a !== void 0 ? _a : [];
}
async _getAccessToken(requestedScopeSets) {
const [scopes, userId] = await (0, helpers_1.loadAndCompareTokenInfo)(this._clientId, this._accessToken.accessToken, this._userId, this._scopes, requestedScopeSets);
this._scopes = scopes;
this._userId = userId;
return { ...this._accessToken, userId };
}
};
exports.StaticAuthProvider = StaticAuthProvider;
tslib_1.__decorate([
(0, shared_utils_1.Enumerable)(false)
], StaticAuthProvider.prototype, "_clientId", void 0);
tslib_1.__decorate([
(0, shared_utils_1.Enumerable)(false)
], StaticAuthProvider.prototype, "_accessToken", void 0);
exports.StaticAuthProvider = StaticAuthProvider = tslib_1.__decorate([
(0, common_1.rtfm)('auth', 'StaticAuthProvider', 'clientId')
], StaticAuthProvider);