-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create 0-config authentication (IT JUST WORKS 🔥🔥🔥)
- Loading branch information
Kipras Melnikovas
committed
Aug 23, 2020
1 parent
ef17f7e
commit fd43b7c
Showing
21 changed files
with
327 additions
and
49 deletions.
There are no files selected for viewing
Submodule gitbeaker
updated
10 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// import "./options-storage"; | ||
import "./gitlab-session-cookie-sync"; | ||
// import "./set-global-var"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Retrieve any previously set cookie and send to content script | ||
* | ||
* https://github.com/mdn/webextensions-examples/blob/master/cookie-bg-picker/background_scripts/background.js | ||
*/ | ||
|
||
async function getActiveTab(): Promise<browser.tabs.Tab> { | ||
return await browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => tabs[0]); | ||
} | ||
|
||
interface MessagePayload { | ||
gitlabSessionToken: string | undefined; | ||
success: boolean; | ||
reason?: string; | ||
} | ||
|
||
async function sendMessage( | ||
tabId: number, | ||
messagePayload: MessagePayload, | ||
options?: browser.tabs._SendMessageOptions | undefined | ||
): Promise<void> { | ||
await browser.tabs.sendMessage(tabId, messagePayload, options); | ||
} | ||
|
||
async function gitlabSessionCookieSync(): Promise<void> { | ||
try { | ||
const tab = await getActiveTab(); | ||
|
||
if (tab.id === undefined) { | ||
return; | ||
} | ||
|
||
if (!tab?.url) { | ||
await sendMessage(tab.id, { | ||
gitlabSessionToken: undefined, | ||
success: false, | ||
reason: "Cannot update cookies (tab's **URL** not found)", | ||
}); | ||
|
||
return; | ||
} | ||
|
||
/** get any previously set cookie for the current tab */ | ||
const cookie: browser.cookies.Cookie | null = await browser.cookies.get({ | ||
url: tab.url, | ||
name: "_gitlab_session", | ||
}); | ||
|
||
if (!cookie) { | ||
await sendMessage(tab.id, { | ||
gitlabSessionToken: undefined, | ||
success: false, | ||
reason: "Cannot update cookies (cookie was falsy)", | ||
}); | ||
|
||
return; | ||
} | ||
|
||
await sendMessage(tab.id, { gitlabSessionToken: cookie.value, success: true }); | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
} | ||
|
||
/** update when the tab is updated */ | ||
browser.tabs.onUpdated.addListener(gitlabSessionCookieSync); | ||
/** update when the tab is activated */ | ||
browser.tabs.onActivated.addListener(gitlabSessionCookieSync); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import "./inject-native-auth-into-api"; | ||
import "./refined-gitlab"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { updateApiVariable } from "../utils/api"; | ||
import { getConfig } from "../utils/config"; | ||
import { getCSRFData } from "../utils/getCSRFData"; | ||
|
||
function injectNativeAuthIntoApi(request, _sender, _sendResponse): void { | ||
const { authKind } = getConfig(); | ||
|
||
if (authKind !== "native") { | ||
return; | ||
} | ||
|
||
const { gitlabSessionToken } = request; | ||
|
||
if (gitlabSessionToken) { | ||
// console.log("`gitlabSessionToken` present - creating new api"); | ||
|
||
const { key: gitlabCSRFTokenKey, value: gitlabCSRFTokenValue } = getCSRFData(); | ||
|
||
updateApiVariable({ | ||
kind: "native", | ||
options: { | ||
nativeAuth: { | ||
gitlabSessionCookieValue: gitlabSessionToken, | ||
gitlabCSRFTokenKey, | ||
gitlabCSRFTokenValue, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
browser.runtime.onMessage.addListener(injectNativeAuthIntoApi); |
12 changes: 7 additions & 5 deletions
12
source/refined-gitlab.ts → source/scripts-content/refined-gitlab.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,54 @@ | ||
// // import { Gitlab } from "@gitbeaker/core"; /** all imports utterly broken */ | ||
import { Gitlab } from "../../gitbeaker/packages/gitbeaker-browser/src"; | ||
// eslint-disable-next-line import/no-cycle | ||
import { getConfig } from "./config"; | ||
import { NativeAuth as GitbeakerNativeAuth } from "../../gitbeaker/packages/gitbeaker-core/src/infrastructure/BaseService"; | ||
// import { setGlobalVar } from "./setGlobalVar"; | ||
|
||
/** | ||
* https://github.com/jdalrymple/gitbeaker | ||
*/ | ||
|
||
export const api = new Gitlab({ | ||
token: getConfig().apiToken, | ||
host: getConfig().hostUrl, | ||
}); | ||
export interface NativeAuth { | ||
kind: "native"; | ||
options: { | ||
nativeAuth: GitbeakerNativeAuth; | ||
}; | ||
} | ||
|
||
export interface APITokenAuth { | ||
kind: "apiToken"; | ||
options: { | ||
oauthToken: string; | ||
host: string; | ||
}; | ||
} | ||
|
||
export type Auth = NativeAuth | APITokenAuth; | ||
|
||
export type AuthKind = Auth["kind"]; | ||
|
||
// eslint-disable-next-line import/no-mutable-exports | ||
let api: ReturnType<typeof createApi>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
export const createApi = (auth: Auth) => { | ||
console.log("createApi, auth =", auth); | ||
|
||
return new Gitlab({ ...auth.options }); | ||
}; | ||
|
||
export const updateApiVariable = (auth: Auth) => { | ||
console.log("updateApiVariable", api); | ||
|
||
api = createApi(auth); | ||
|
||
/** broken */ | ||
// (window as any).api = api; | ||
|
||
// // eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// // @ts-ignore | ||
// (window as any).api = cloneInto(api, window, { cloneFunctions: true }); | ||
|
||
return api; | ||
}; | ||
|
||
export { api }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { needsToWaitForApi } from "./needsToWaitForApi"; | ||
|
||
export const apiLoaded = new Promise((resolve) => { | ||
if (!needsToWaitForApi()) { | ||
resolve(); | ||
} | ||
|
||
browser.runtime.onMessage.addListener((request, _sender, _sendResponse) => { | ||
const { gitlabSessionToken } = request; | ||
|
||
if (gitlabSessionToken) { | ||
resolve(); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.