MCPcopy Index your code
hub / github.com/conwnet/github1s / ConnectToGitLab

Function ConnectToGitLab

src/gitlab-auth.ts:32–58  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30
31// https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-flow
32export const ConnectToGitLab = async () => {
33 const STATE = createRandomString(32);
34 const opener = window.open(createAuthorizeUrl(STATE), '_blank', OPEN_WINDOW_FEATURES);
35
36 return new Promise((resolve) => {
37 const handleAuthMessage = (event: MessageEvent) => {
38 // Note that though the browser block opening window and popup a tip,
39 // the user can be still open it from the tip. In this case, the `opener`
40 // is null, and we should still process the authorizing message
41 const isValidOpener = !!(opener && event.source === opener);
42 const isValidOrigin = event.origin === AUTH_PAGE_ORIGIN;
43 const isValidResponse = event.data ? event.data.type === 'authorizing' : false;
44 if (!isValidOpener || !isValidOrigin || !isValidResponse) {
45 return;
46 }
47 window.removeEventListener('message', handleAuthMessage);
48 resolve(event.data?.payload);
49 };
50
51 window.addEventListener('message', handleAuthMessage);
52 // if there isn't any message from opener window in 300s, remove the message handler
53 timeout(300 * 1000).then(() => {
54 window.removeEventListener('message', handleAuthMessage);
55 resolve({ error: 'authorizing_timeout', error_description: 'Authorizing timeout' });
56 });
57 });
58};

Callers

nothing calls this directly

Calls 4

timeoutFunction · 0.90
createRandomStringFunction · 0.85
createAuthorizeUrlFunction · 0.85
openMethod · 0.65

Tested by

no test coverage detected