@@ -8,6 +8,7 @@ const GIRA_GRAPHQL_WS_ENDPOINT = "wss://c2g091p01.emel.pt/ws/graphql";
88const GIRA_AUTH_ENDPOINT = "https://api-auth.emel.pt/auth" ;
99const GIRA_TOKEN_REFRESH_ENDPOINT = "https://api-auth.emel.pt/token/refresh" ;
1010const GIRA_USER_ENDPOINT = "https://api-auth.emel.pt/user" ;
11+ const FIREBASE_TOKEN_URL = "https://luk.moe/girabot_tokens/exchange" ;
1112
1213const NUMBER_OF_RETRIES = 3 ;
1314const DEFAULT_PROXY = "https://corsproxy.afonsosousah.workers.dev/" ;
@@ -19,10 +20,11 @@ async function makePostRequest(url, body, accessToken = null) {
1920 const response = await fetch ( proxyURL ?? DEFAULT_PROXY , {
2021 method : "POST" ,
2122 headers : {
22- "User-Agent" : "Gira/3.4.0 (Android 34)" ,
23+ "User-Agent" : "Gira/3.4.3 (Android 34)" ,
2324 "X-Proxy-URL" : url ,
2425 "Content-Type" : "application/json" ,
2526 "X-Authorization" : `Bearer ${ accessToken } ` ,
27+ "X-Firebase-Token" : encryptFirebaseToken ( user . firebaseToken , user . accessToken ) ,
2628 } ,
2729 body : body ,
2830 } ) ;
@@ -31,9 +33,19 @@ async function makePostRequest(url, body, accessToken = null) {
3133
3234 // refresh token
3335 accessToken = await tokenRefresh ( ) ;
36+ // se o token tiver expirado
37+ if ( ! getCookiie ( "firebaseToken" ) ) {
38+ const firebaseToken = await fetchFirebaseToken ( ) ;
39+ const { exp } = getJWTPayload ( user . firebaseToken ) ;
40+ if ( firebaseToken ) {
41+ // Store firebaseToken cookie (for quick refreshes)
42+ createCookie ( "firebaseToken" , firebaseToken , new Date ( exp * 1000 ) ) ; // 30 days
43+ user . firebaseToken = firebaseToken ;
44+ } else delete user . firebaseToken ;
45+ }
3446
35- // check if token refresh was successful
36- if ( typeof accessToken !== "undefined" ) {
47+ // check if token refresh was successful and there's a firebase token
48+ if ( typeof accessToken !== "undefined" && user . firebaseToken ) {
3749 // try to make request again
3850 return await retryPostRequest ( url , body , accessToken , "Erro da API (401)" ) ; // be sure to use latest available token
3951 }
@@ -158,6 +170,20 @@ async function makePostRequest(url, body, accessToken = null) {
158170 }
159171}
160172
173+ // source: trust me bro
174+ function encryptFirebaseToken ( firebaseToken , authToken ) {
175+ let { sub, jti } = getJWTPayload ( authToken ) ;
176+
177+ const key = sub . replaceAll ( "-" , "" ) ;
178+ const iv = jti . slice ( 0 , 16 ) ;
179+
180+ let cipher = crypto . createCipheriv ( "aes-256-cbc" , key , iv ) ;
181+
182+ let encrypted = cipher . update ( firebaseToken , "utf8" , "base64" ) ;
183+
184+ return encrypted + cipher . final ( "base64" ) ;
185+ }
186+
161187async function makeGetRequest ( url , accessToken = null ) {
162188 // Proxy is not needed for these GET requests
163189 const response = await fetch ( url , {
0 commit comments