Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove token from local storage and store it as global variable #520

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ describe('request the token after OAuth', () => {
// Verify that the token request was made
cy.get('@requestToken').should('have.property', 'response');
cy.get('@requestToken').its('response.statusCode').should('eq', 200);

cy.getAllLocalStorage().then((result) => {
expect(result['http://localhost:1234']).to.have.property('token');
})
});

it('should show an error when the token request fails', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { showToast } from "../toast";
import { Config } from "../config";
import { Localization } from "@evolvedbinary/prosemirror-lwdita-localization";
import urijs from "urijs";
import { Global } from '../global'

/**
* Fetches the raw content of a document from a GitHub repository.
Expand Down Expand Up @@ -158,7 +159,9 @@ export const getUserInfo = async (config: Config, localization: Localization, to
* @returns A promise that resolves when the document has been published.
*/
export const createPrFromContribution = async (config: Config, localization: Localization, ghrepo: string, source: string, branch: string, changedDocument: string, title: string, desc: string): Promise<string> => {
const authenticatedUserInfo = await getUserInfo(config, localization, localStorage.getItem('token') as string);
const token = (global as Global).token;

const authenticatedUserInfo = await getUserInfo(config, localization, token);

const owner = ghrepo.split('/')[0];
const repo = ghrepo.split('/')[1];
Expand All @@ -173,8 +176,7 @@ export const createPrFromContribution = async (config: Config, localization: Loc
content
};
const body = `${desc}` + config.git.commitMessageSuffix;
// get the token from the local storage
const token = localStorage.getItem('token');

// make a post request to /api/github/integration
const response = await fetch(config.server.api.baseUrl + config.server.api.endpoint.integration, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { exchangeOAuthCodeForAccessToken } from './github.plugin';
import { showToast } from '../toast';
import { Config } from '../config';
import { Localization } from '@evolvedbinary/prosemirror-lwdita-localization';
import { Global } from '../global'

/**
* Interface for the URL parameters
Expand Down Expand Up @@ -217,7 +218,7 @@ export function processRequest(config: Config, localization: Localization): unde
}

exchangeOAuthCodeForAccessToken(config, localization, returnParams.code).then(({token, installation}) => {
localStorage.setItem('token', token);
(global as Global).token = token;
if(!installation) {
// redirect to the OAuth error page and show the error message with instructions to install the app
redirectToGitHubAppInstall(config, returnParams);
Expand Down
31 changes: 31 additions & 0 deletions packages/prosemirror-lwdita/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable no-var */
/*!
Copyright (C) 2020 Evolved Binary

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


// This file is used to declare global variables that are used in the application.
// This is a workaround to allow the use of global variables in TypeScript.
// See https://javascript.plainenglish.io/typescript-and-global-variables-in-node-js-59c4bf40cb31
export interface Global {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we also need this? I read the article that you pointed to but they don't do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface is for setting the type

const token = (global as Global).token;

when using the global var on it's own it's does not types.

token: string;
}

// The use of `var` is intentional here, as it is the only way to declare a global variable in TypeScript.
declare global {
var token: string;
}
export { };
11 changes: 2 additions & 9 deletions packages/prosemirror-lwdita/tests/github.plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import fetchMock from 'fetch-mock';
import { shortXdita, shortXditaProsemirroJson } from './test-utils';
import { MockConfig } from './mock.config';
import { createLocalization } from '@evolvedbinary/prosemirror-lwdita-localization';
import { Global } from '../src/global'

const config = new MockConfig();
const localization = createLocalization();
Expand Down Expand Up @@ -116,15 +117,6 @@ describe('getUserInfo', () => {
});

describe('createPrFromContribution', () => {
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const window = global as any;
Object.defineProperty(window, 'localStorage', {
value: {
getItem: () => 'mock-token',
},
});
});
afterEach(() => {
fetchMock.restore();
});
Expand All @@ -151,6 +143,7 @@ describe('createPrFromContribution', () => {
login: petalBotUser,
},
});
(global as Global).token = token;
await createPrFromContribution(config, localization, ghrepo, source, branch, changedDocument, title, description);
const lastCall = fetchMock.lastCall(config.server.api.baseUrl + config.server.api.endpoint.integration) as fetchMock.MockCall;
if (!lastCall) {
Expand Down