Files
twitch-cloud-ebs/static/SugoiAuthProvider.js
T

3 lines
6.4 KiB
JavaScript

var N=Object.create;var q=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var B=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var E=r=>{throw TypeError(r)};var H=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);var J=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of $(e))!G.call(r,i)&&i!==t&&q(r,i,{get:()=>e[i],enumerable:!(n=Z(e,i))||n.enumerable});return r};var K=(r,e,t)=>(t=r!=null?N(B(r)):{},J(e||!r||!r.__esModule?q(t,"default",{value:r,enumerable:!0}):t,r));var W=(r,e,t)=>e.has(r)||E("Cannot "+t);var s=(r,e,t)=>(W(r,e,"read from private field"),t?t.call(r):e.get(r)),x=(r,e,t)=>e.has(r)?E("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),f=(r,e,t,n)=>(W(r,e,"write to private field"),n?n.call(r,t):e.set(r,t),t);var z=H((v,U)=>{(function(r,e){typeof v=="object"&&typeof U<"u"?U.exports=e():typeof define=="function"&&define.amd?define(e):(r=typeof globalThis<"u"?globalThis:r||self,r.fetchRetry=e())})(v,function(){"use strict";var r=function(n,i){if(i=i||{},typeof n!="function")throw new t("fetch must be a function");if(typeof i!="object")throw new t("defaults must be an object");if(i.retries!==void 0&&!e(i.retries))throw new t("retries must be a positive integer");if(i.retryDelay!==void 0&&!e(i.retryDelay)&&typeof i.retryDelay!="function")throw new t("retryDelay must be a positive integer or a function returning a positive integer");if(i.retryOn!==void 0&&!Array.isArray(i.retryOn)&&typeof i.retryOn!="function")throw new t("retryOn property expects an array or function");var M={retries:3,retryDelay:1e3,retryOn:[]};return i=Object.assign(M,i),function(A,c){var _=i.retries,l=i.retryDelay,d=i.retryOn;if(c&&c.retries!==void 0)if(e(c.retries))_=c.retries;else throw new t("retries must be a positive integer");if(c&&c.retryDelay!==void 0)if(e(c.retryDelay)||typeof c.retryDelay=="function")l=c.retryDelay;else throw new t("retryDelay must be a positive integer or a function returning a positive integer");if(c&&c.retryOn)if(Array.isArray(c.retryOn)||typeof c.retryOn=="function")d=c.retryOn;else throw new t("retryOn property expects an array or function");return new Promise(function(b,w){var L=function(h){var P=typeof Request<"u"&&A instanceof Request?A.clone():A;n(P,c).then(function(a){if(Array.isArray(d)&&d.indexOf(a.status)===-1)b(a);else if(typeof d=="function")try{return Promise.resolve(d(h,null,a)).then(function(u){u?p(h,null,a):b(a)}).catch(w)}catch(u){w(u)}else h<_?p(h,null,a):b(a)}).catch(function(a){if(typeof d=="function")try{Promise.resolve(d(h,a,null)).then(function(u){u?p(h,a,null):w(a)}).catch(function(u){w(u)})}catch(u){w(u)}else h<_?p(h,a,null):w(a)})};function p(h,P,a){var u=typeof l=="function"?l(h,P,a):l;setTimeout(function(){L(++h)},u)}L(0)})}};function e(n){return Number.isInteger(n)&&n>=0}function t(n){this.name="ArgumentError",this.message=n}return r})});var j=K(z()),k=(0,j.default)(globalThis.fetch,{retries:10,retryDelay:r=>r*1e3}),T=location.origin+location.pathname,O=new URL("/oauth2/token",import.meta.url);function I(r){return r.obtainment_timestamp=Date.now(),r}function C(r){let e=new URLSearchParams({client_id:r,grant_type:"client_credentials"});return k(O,{method:"POST",body:e.toString(),headers:{"content-type":"application/x-www-form-urlencoded"}}).then(async function(t){if(!t.ok)throw new Error(await t.text());let n=await t.json();return I(n)})}function Q(r,...e){console.debug("requesting authorization code");let t=new URL("https://id.twitch.tv/oauth2/authorize");return t.searchParams.append("response_type","code"),t.searchParams.append("client_id",r),t.searchParams.append("scope",e.join(" ").trim()),location.assign(t+"&redirect_uri="+T),new Promise(()=>{})}function V(r,e){console.debug("exchanging authorization code");let t=new URLSearchParams({client_id:r,code:e,grant_type:"authorization_code"});return k(O,{method:"POST",body:t.toString()+"&redirect_uri="+T,headers:{"content-type":"application/x-www-form-urlencoded"}}).then(async function(n){if(!n.ok)throw new Error(await n.text());let i=await n.json();return I(i)})}async function D(r){return console.debug("validating token"),k("https://id.twitch.tv/oauth2/validate",{headers:{authorization:"OAuth "+r}}).then(async e=>{if(!e.ok)throw new Error(await e.text());let t=await e.json();return t.access_token=r,t.scope=t.scopes,delete t.scopes,t.token_type="bearer",I(t)})}function m(r,e){console.debug("refreshing token");let t=new URLSearchParams({client_id:r,grant_type:"refresh_token",refresh_token:e});return k(O,{method:"POST",body:t.toString(),headers:{"content-type":"application/x-www-form-urlencoded"}}).then(async function(n){if(!n.ok)throw new Error(await n.text());return await n.json()})}async function F(r,...e){let t=await R(r,...e);return t||Q(r,...e)}async function R(r,...e){console.debug("scopes requested:",...e);let t=new URLSearchParams(location.search+"&"+location.hash.substring(1));if(t.has("code")){let n=t.get("code");return history.replaceState(null,"",T),V(r,n)}if(t.has("refresh_token"))return m(r,t.get("refresh_token"));if(t.has("access_token"))return D(t.get("access_token"));if(t.has("error")){let n=t.get("error")+": "+t.get("error_description");throw history.replaceState(null,"",T),new Error(n)}return null}function g(r){return new Proxy(r,{get(e,t,n){return e[t.toString().replace(/[A-Z]/g,i=>`_${i.toLowerCase()}`)]}})}function Y(r,...e){if(!r)return!1;if(!e)return!0;for(let t of e)if(!r.scope.includes(t))return!1;return!0}var o,y,S=class{constructor(e){x(this,o);x(this,y,e=>(f(this,o,e),e));this.clientId=e}async addUser(...e){return f(this,o,await F(this.clientId,...e).then(g).then(s(this,y))),s(this,o)}async addUserForToken(e){return e.refresh_token?(f(this,o,await m(this.clientId,e.refresh_token).then(g).then(s(this,y))),s(this,o)):(f(this,o,await D(e.access_token).then(g).then(s(this,y))),s(this,o))}removeUser(){f(this,o,null)}async getAccessTokenForUser(e,...t){if(!t[0]&&s(this,o))return s(this,o);for(let n of t)if(Y(s(this,o),...n))return s(this,o);return f(this,o,await R(this.clientId,...t[0]||[]).then(g).then(s(this,y))),s(this,o)}async getAnyAccessToken(e){return s(this,o)||C(this.clientId).then(g)}getCurrentScopesForUser(e){return!s(this,o)||s(this,o)instanceof Promise?[]:s(this,o).scope}async refreshAccessTokenForUser(e){return f(this,o,await m(this.clientId,s(this,o).refresh_token).then(s(this,y))),s(this,o)}};o=new WeakMap,y=new WeakMap;export{S as default};
//# sourceMappingURL=SugoiAuthProvider.js.map