function generateBrowserLoginUrl (base, token, opts = {}) { const url = new URL(`${base}/auth/token/${token}/`) if (opts.isAskFlow) { url.searchParams.append('ask', '1') } if (opts.isPrivate || opts.isAskFlow) { url.searchParams.append('private', '1') } if (opts.clearSession) { url.searchParams.append('clearSession', '1') } return url.toString() }
const wsUrl = parsedURL.toString() const ws = websocket(wsUrl)
get url() { if (this.opts.basicAuth) { const u = new URL(this.opts.url); u.username = this.opts.basicAuth.username || ''; u.password = this.opts.basicAuth.password || ''; return u.toString(); } return this.opts.url; }
const getApplePublicKey = async () => { const url = new URL(ENDPOINT_URL); url.pathname = '/auth/keys'; const data = await request({ url: url.toString(), method: 'GET' }); const key = JSON.parse(data).keys[0]; const pubKey = new NodeRSA(); pubKey.importKey({ n: Buffer.from(key.n, 'base64'), e: Buffer.from(key.e, 'base64') }, 'components-public'); return pubKey.exportKey(['public']); }
const refreshAuthorizationToken = async (refreshToken, options) => { if (!options.clientID) throw new Error('clientID is empty'); if (!options.clientSecret) throw new Error('clientSecret is empty'); const url = new URL(ENDPOINT_URL); url.pathname = '/auth/token'; const form = { client_id: options.clientID, client_secret: options.clientSecret, refresh_token: refreshToken, grant_type: 'refresh_token', }; const body = await request({ url: url.toString(), method: 'POST', form }); return JSON.parse(body); }
const getLatestTweets = (latestId) => { const url = new URL("https://api.twitter.com/1.1/search/tweets.json"); url.searchParams.append("q", "%23meinunterricht"); url.searchParams.append("count", "100"); url.searchParams.append("since_id", latestId); return new Promise((resolve, reject) => { get(url.toString()) .then((response) => { const result = JSON.parse(response.body); const tweets = parseResponse(result); const newId = tweets.length > 0 ? orderBy(tweets, "id", "desc")[0].id : latestId; resolve({newValue: newId, result: tweets}); }) .catch(err => reject(err)); }); }
/** * Render the page of url use selenium-webdriver. * * @param url {string} request url * @return {Promise<string>} html text of page. */ async render(url) { /* put some query parameters to tell the script in page that they are being pre-rendering */ const nUrl = new urlib.URL(url); nUrl.searchParams.set("isPrerender", "true"); nUrl.searchParams.delete("prerender"); await this.driver.get(nUrl.toString()); const body = await this.driver.findElement(selenium.By.css("body")); /* Waiting for data-render-complete attribute append to body */ await this.driver.wait(new selenium.Condition("for render complete", async () => { return await body.getAttribute("data-render-complete") === "true"; }), 1000); return await (await this.driver.findElement(selenium.By.css("html"))).getAttribute("outerHTML"); }
const getAuthorizationUrl = (options = {}) => { if (!options.clientID) throw Error('clientID is empty'); if (!options.redirectUri) throw Error('redirectUri is empty'); const url = new URL(ENDPOINT_URL); url.pathname = '/auth/authorize'; url.searchParams.append('response_type', 'code'); url.searchParams.append('state', options.state || 'state'); url.searchParams.append('client_id', options.clientID); url.searchParams.append('redirect_uri', options.redirectUri); if (options.scope){ url.searchParams.append('scope', 'openid ' + options.scope); } else { url.searchParams.append('scope', 'openid'); } return url.toString(); }
normalizeReg(urlStr) { const url = new URL(urlStr); url.searchParams.set('interface', this.interfaceName); url.searchParams.set('version', this.version); url.searchParams.set('group', this.group); // 写入自定义元数据 // append custom metadata to searchParams let customMeta = {}; const blockList = [ 'interface', 'version', 'group' ]; Object.keys(this.customMeta).forEach(metaKey => { const value = this.customMeta[metaKey]; if (!(typeof value === 'undefined' || blockList.includes(metaKey))) { url.searchParams.set(metaKey, value); const source = { [`${metaKey}`]: value }; customMeta = Object.assign(customMeta, source); } }); const reg = { interfaceName: this.interfaceName, version: this.version, group: this.group, url: url.toString(), }; return Object.assign(reg, customMeta); }
this.opts.url = u.toString();
const getAuthorizationToken = async (code, options) => { if (!options.clientID) throw new Error('clientID is empty'); if (!options.redirectUri) throw new Error('redirectUri is empty'); if (!options.clientSecret) throw new Error('clientSecret is empty'); const url = new URL(ENDPOINT_URL); url.pathname = '/auth/token'; const form = { client_id: options.clientID, client_secret: options.clientSecret, code, grant_type: 'authorization_code', redirect_uri: options.redirectUri, }; const body = await request({ url: url.toString(), method: 'POST', form }); return JSON.parse(body); }
const data = await fetch(url.toString(), { method: 'GET', headers: {
/** Refreshes an Apple authorization token */ const refreshAuthorizationToken = async ( refreshToken: string, options: { clientID: string, clientSecret: string, }, ): Promise<AppleAuthorizationTokenResponseType> => { if (!options.clientID) { throw new Error('clientID is empty'); } if (!options.clientSecret) { throw new Error('clientSecret is empty'); } const url = new URL(ENDPOINT_URL); url.pathname = '/auth/token'; const params = new URLSearchParams(); params.append('client_id', options.clientID); params.append('client_secret', options.clientSecret); params.append('refresh_token', refreshToken); params.append('grant_type', 'refresh_token'); return fetch(url.toString(), { method: 'POST', body: params, }).then((res) => res.json()); }
return url.toString();
return fetch(url.toString(), { method: 'POST', body: params,