fix(desktop): implement bearer token auth for windows webview2
This commit is contained in:
@@ -61,13 +61,10 @@ jobs:
|
|||||||
rustc --version
|
rustc --version
|
||||||
shell: cmd
|
shell: cmd
|
||||||
- name: Cache Rust target and registry
|
- name: Cache Rust target and registry
|
||||||
uses: actions/cache@v3
|
uses: Swatinem/rust-cache@v2
|
||||||
with:
|
with:
|
||||||
path: |
|
workspaces: |
|
||||||
~/.cargo/registry
|
web/src-tauri
|
||||||
~/.cargo/git
|
|
||||||
web/src-tauri/target
|
|
||||||
key: ${{ runner.os }}-cargo-${{ hashFiles('web/src-tauri/Cargo.lock') }}
|
|
||||||
- name: Build frontend
|
- name: Build frontend
|
||||||
run: cd web && npm ci && npm run build
|
run: cd web && npm ci && npm run build
|
||||||
shell: cmd
|
shell: cmd
|
||||||
|
|||||||
+40
-4
@@ -8,12 +8,37 @@ if (isTauri) {
|
|||||||
const TARGET = 'https://dumpster.dustin.coffee';
|
const TARGET = 'https://dumpster.dustin.coffee';
|
||||||
const WS_TARGET = 'wss://dumpster.dustin.coffee';
|
const WS_TARGET = 'wss://dumpster.dustin.coffee';
|
||||||
|
|
||||||
|
let sessionToken = localStorage.getItem('dumpster_session_token') || '';
|
||||||
|
|
||||||
const originalFetch = window.fetch;
|
const originalFetch = window.fetch;
|
||||||
window.fetch = async (input, init) => {
|
window.fetch = async (input, init) => {
|
||||||
if (typeof input === 'string' && input.startsWith('/')) {
|
let url = typeof input === 'string' ? input : input.toString();
|
||||||
input = TARGET + input;
|
if (url.startsWith('/')) {
|
||||||
|
url = TARGET + url;
|
||||||
}
|
}
|
||||||
return originalFetch(input, init);
|
|
||||||
|
let newInit = init ? { ...init } : {};
|
||||||
|
if (sessionToken) {
|
||||||
|
newInit.headers = {
|
||||||
|
...newInit.headers,
|
||||||
|
'Authorization': 'Bearer ' + sessionToken
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await originalFetch(url, newInit);
|
||||||
|
|
||||||
|
const token = response.headers.get('X-Session-Token');
|
||||||
|
if (token) {
|
||||||
|
sessionToken = token;
|
||||||
|
localStorage.setItem('dumpster_session_token', token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.endsWith('/auth/logout') && response.ok) {
|
||||||
|
sessionToken = '';
|
||||||
|
localStorage.removeItem('dumpster_session_token');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
};
|
};
|
||||||
|
|
||||||
const OriginalWebSocket = window.WebSocket;
|
const OriginalWebSocket = window.WebSocket;
|
||||||
@@ -27,7 +52,18 @@ if (isTauri) {
|
|||||||
} else if (urlStr.startsWith('/')) {
|
} else if (urlStr.startsWith('/')) {
|
||||||
urlStr = WS_TARGET + urlStr;
|
urlStr = WS_TARGET + urlStr;
|
||||||
}
|
}
|
||||||
return new OriginalWebSocket(urlStr, protocols);
|
|
||||||
|
const ws = new OriginalWebSocket(urlStr, protocols);
|
||||||
|
|
||||||
|
if (sessionToken && urlStr.includes('/ws') && !urlStr.includes('/ws/bot')) {
|
||||||
|
const originalOnOpen = ws.onopen;
|
||||||
|
ws.onopen = function(ev) {
|
||||||
|
ws.send(JSON.stringify({ token: sessionToken }));
|
||||||
|
if (originalOnOpen) originalOnOpen.call(ws, ev);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return ws;
|
||||||
} as unknown as typeof WebSocket;
|
} as unknown as typeof WebSocket;
|
||||||
window.WebSocket.prototype = OriginalWebSocket.prototype;
|
window.WebSocket.prototype = OriginalWebSocket.prototype;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user