11//Imports
22import octokit from "@octokit/graphql"
33import OctokitRest from "@octokit/rest"
4+ import axios from "axios"
45import compression from "compression"
6+ import crypto from "crypto"
57import express from "express"
68import ratelimit from "express-rate-limit"
79import cache from "memory-cache"
8- import util from "util"
910import url from "url"
10- import axios from "axios "
11+ import util from "util "
1112import mocks from "../../../tests/mocks/index.mjs"
1213import metrics from "../metrics/index.mjs"
1314import presets from "../metrics/presets.mjs"
1415import setup from "../metrics/setup.mjs"
15- import crypto from "crypto"
1616
1717/**App */
1818export default async function ( { sandbox = false } = { } ) {
@@ -68,8 +68,9 @@ export default async function({sandbox = false} = {}) {
6868 console . debug ( `metrics/app/session/${ login } > authenticated with session ${ session . substring ( 0 , 6 ) } , using custom octokit` )
6969 return { login, graphql : octokit . graphql . defaults ( { headers : { authorization : `token ${ token } ` } } ) , rest : new OctokitRest . Octokit ( { auth : token } ) }
7070 }
71- else if ( session )
71+ else if ( session ) {
7272 console . debug ( `metrics/app/session > unknown session ${ session . substring ( 0 , 6 ) } , using default octokit` )
73+ }
7374 return null
7475 }
7576
@@ -146,7 +147,7 @@ export default async function({sandbox = false} = {}) {
146147 //Modes and extras
147148 app . get ( "/.modes" , limiter , ( req , res ) => res . status ( 200 ) . json ( conf . settings . modes ) )
148149 app . get ( "/.extras" , limiter , async ( req , res ) => {
149- if ( ( authenticated . has ( req . headers [ "x-metrics-session" ] ) ) && ( conf . settings . extras ?. logged ) ) {
150+ if ( ( authenticated . has ( req . headers [ "x-metrics-session" ] ) ) && ( conf . settings . extras ?. logged ) ) {
150151 if ( conf . settings . extras ?. features !== true )
151152 return res . status ( 200 ) . json ( [ ...conf . settings . extras . features , ...conf . settings . extras . logged ] )
152153 }
@@ -177,9 +178,9 @@ export default async function({sandbox = false} = {}) {
177178 try {
178179 const custom = uapi ( req . headers [ "x-metrics-session" ] )
179180 if ( custom ) {
180- const { data :{ resources} } = await custom . rest . rateLimit . get ( )
181+ const { data : { resources} } = await custom . rest . rateLimit . get ( )
181182 if ( resources )
182- return res . status ( 200 ) . json ( { rest :resources . core , graphql :resources . graphql , search :resources . search , login :custom . login } )
183+ return res . status ( 200 ) . json ( { rest : resources . core , graphql : resources . graphql , search : resources . search , login : custom . login } )
183184 }
184185 }
185186 catch { } //eslint-disable-line no-empty
@@ -220,39 +221,42 @@ export default async function({sandbox = false} = {}) {
220221 console . debug ( `metrics/app/oauth > request ${ state } ` )
221222 //OAuth through GitHub
222223 return res . redirect ( `https://github.com/login/oauth/authorize?${ new url . URLSearchParams ( {
223- client_id :conf . settings . oauth . id ,
224+ client_id : conf . settings . oauth . id ,
224225 state,
225- redirect_uri :`${ conf . settings . oauth . url } /.oauth/authorize` ,
226- allow_signup :false ,
227- scope :scopes ,
226+ redirect_uri : `${ conf . settings . oauth . url } /.oauth/authorize` ,
227+ allow_signup : false ,
228+ scope : scopes ,
228229 } ) } `)
229230 } )
230231 app . get ( "/.oauth/authorize" , async ( req , res ) => {
231232 //Check state
232233 const { code, state} = req . query
233- if ( ( ! state ) || ( ! states . has ( state ) ) ) {
234+ if ( ( ! state ) || ( ! states . has ( state ) ) ) {
234235 console . debug ( "metrics/app/oauth > 400 (invalid state)" )
235236 return res . status ( 400 ) . send ( "Bad request: invalid state" )
236237 }
237238 //OAuth
238239 try {
239240 //Authorize user
240241 console . debug ( "metrics/app/oauth > authorization" )
241- const { data} = await axios . post ( "https://github.com/login/oauth/access_token" , `${ new url . URLSearchParams ( {
242- client_id :conf . settings . oauth . id ,
243- client_secret :conf . settings . oauth . secret ,
244- code,
245- } ) } `)
242+ const { data} = await axios . post (
243+ "https://github.com/login/oauth/access_token" ,
244+ `${ new url . URLSearchParams ( {
245+ client_id : conf . settings . oauth . id ,
246+ client_secret : conf . settings . oauth . secret ,
247+ code,
248+ } ) } `,
249+ )
246250 const token = new url . URLSearchParams ( data ) . get ( "access_token" )
247251 //Validate user
248- const { data :{ login} } = await axios . get ( "https://api.github.com/user" , { headers :{ Authorization :`token ${ token } ` } } )
252+ const { data : { login} } = await axios . get ( "https://api.github.com/user" , { headers : { Authorization : `token ${ token } ` } } )
249253 console . debug ( `metrics/app/oauth > authorization success for ${ login } ` )
250254 const session = crypto . randomBytes ( 128 ) . toString ( "hex" )
251255 authenticated . set ( session , { login, token} )
252256 console . debug ( `metrics/app/oauth > created session ${ session . substring ( 0 , 6 ) } ` )
253257 //Redirect user back
254258 const { from} = states . get ( state )
255- return res . redirect ( `/.oauth/redirect?${ new url . URLSearchParams ( { to :from , session} ) } ` )
259+ return res . redirect ( `/.oauth/redirect?${ new url . URLSearchParams ( { to : from , session} ) } ` )
256260 }
257261 catch {
258262 console . debug ( "metrics/app/oauth > authorization failed" )
@@ -267,7 +271,7 @@ export default async function({sandbox = false} = {}) {
267271 if ( authenticated . has ( session ) ) {
268272 const { token} = authenticated . get ( session )
269273 try {
270- console . log ( await axios . delete ( `https://api.github.com/applications/${ conf . settings . oauth . id } /grant` , { auth :{ username :conf . settings . oauth . id , password :conf . settings . oauth . secret } , headers :{ Accept :"application/vnd.github+json" } , data :{ access_token :token } } ) )
274+ console . log ( await axios . delete ( `https://api.github.com/applications/${ conf . settings . oauth . id } /grant` , { auth : { username : conf . settings . oauth . id , password : conf . settings . oauth . secret } , headers : { Accept : "application/vnd.github+json" } , data : { access_token : token } } ) )
271275 authenticated . delete ( session )
272276 console . debug ( `metrics/app/oauth > deleted session ${ session . substring ( 0 , 6 ) } ` )
273277 return res . redirect ( "/.oauth" )
@@ -279,8 +283,9 @@ export default async function({sandbox = false} = {}) {
279283 app . get ( "/.oauth/redirect" , limiter , ( req , res ) => res . sendFile ( `${ conf . paths . statics } /oauth/redirect.html` ) )
280284 app . get ( "/.oauth/enabled" , limiter , ( req , res ) => res . json ( true ) )
281285 }
282- else
286+ else {
283287 app . get ( "/.oauth/enabled" , limiter , ( req , res ) => res . json ( false ) )
288+ }
284289
285290 //Pending requests
286291 const pending = new Map ( )
@@ -405,7 +410,7 @@ export default async function({sandbox = false} = {}) {
405410 const repository = req . params . repository ?. replace ( / [ \n \r ] / g, "" )
406411 let solve = null
407412 //Check username
408- if ( ( login . startsWith ( "." ) ) || ( login . includes ( "/" ) ) )
413+ if ( ( login . startsWith ( "." ) ) || ( login . includes ( "/" ) ) )
409414 return next ( )
410415 if ( ! / ^ [ - \w ] + $ / i. test ( login ) ) {
411416 console . debug ( `metrics/app/${ login } > 400 (invalid username)` )
@@ -452,9 +457,9 @@ export default async function({sandbox = false} = {}) {
452457 console . debug ( `metrics/app/${ login } > ${ util . inspect ( q , { depth : Infinity , maxStringLength : 256 } ) } ` )
453458 const octokit = { ...api , ...uapi ( req . headers [ "x-metrics-session" ] ) }
454459 let uconf = conf
455- if ( ( octokit . login ) && ( conf . settings . extras ?. logged ) && ( uconf . settings . extras ?. features !== true ) ) {
460+ if ( ( octokit . login ) && ( conf . settings . extras ?. logged ) && ( uconf . settings . extras ?. features !== true ) ) {
456461 console . debug ( `metrics/app/${ login } > session is authenticated, adding additional permissions ${ conf . settings . extras . logged } ` )
457- uconf = { ...conf , settings :{ ...conf . settings , extras :{ ...conf . settings . extras } } }
462+ uconf = { ...conf , settings : { ...conf . settings , extras : { ...conf . settings . extras } } }
458463 uconf . settings . extras . features = uconf . settings . extras . features ?? [ ]
459464 uconf . settings . extras . features . push ( ...conf . settings . extras . logged )
460465 }
@@ -468,7 +473,7 @@ export default async function({sandbox = false} = {}) {
468473 const { rendered, mime} = await metrics ( { login, q} , {
469474 ...octokit ,
470475 plugins,
471- conf :uconf ,
476+ conf : uconf ,
472477 die : q [ "plugins.errors.fatal" ] ?? false ,
473478 verify : q . verify ?? false ,
474479 convert : convert !== "auto" ? convert : null ,
0 commit comments