| 1 | const cache = (window.__rustyTeamsSkype = window.__rustyTeamsSkype || {}); |
| 2 | if (!cache.token || cache.expires <= Date.now() + 60000) { |
| 3 | const spaces = await acquire('https://api.spaces.skype.com', 'authorization.readwrite'); |
| 4 | if (!spaces.token) return {status: 0, body: 'no token for authsvc'}; |
| 5 | let response; |
| 6 | try { |
| 7 | response = await fetch('https://teams.cloud.microsoft/api/authsvc/v1.0/authz', { |
| 8 | method: 'POST', |
| 9 | headers: {Authorization: 'Bearer ' + spaces.token}, |
| 10 | }); |
| 11 | } catch (error) { |
| 12 | return {status: 0, body: 'authz_unreachable'}; |
| 13 | } |
| 14 | if (!response.ok) return {status: 0, body: 'authz_http_' + response.status}; |
| 15 | try { |
| 16 | const answer = await response.json(); |
| 17 | cache.token = answer.tokens.skypeToken; |
| 18 | cache.expires = Date.now() + Number(answer.tokens.expiresIn || 0) * 1000; |
| 19 | cache.chatService = answer.regionGtms.chatService; |
| 20 | } catch (error) { |
| 21 | return {status: 0, body: 'authz_shape'}; |
| 22 | } |
| 23 | } |
| 24 | const headers = {...args.headers, Authorization: 'Bearer ' + token, 'x-skypetoken': cache.token}; |
| 25 | if (cache.chatService) headers['x-csendpoint'] = cache.chatService; |
| 26 | try { |
| 27 | const response = await fetch(args.url, { |
| 28 | method: args.method, |
| 29 | headers, |
| 30 | body: args.body === null ? undefined : JSON.stringify(args.body), |
| 31 | }); |
| 32 | const text = await response.text(); |
| 33 | let body = text; |
| 34 | try { body = text ? JSON.parse(text) : null; } catch (error) {} |
| 35 | return {status: response.status, retryAfter: response.headers.get('Retry-After'), body}; |
| 36 | } catch (error) { |
| 37 | return {status: 0, body: String(error)}; |
| 38 | } |