@jbrowse/plugin-authentication
Version:
JBrowse 2 Authentication
61 lines (60 loc) • 2.54 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { ConfigurationReference } from '@jbrowse/core/configuration';
import { types } from 'mobx-state-tree';
import { configSchema as OAuthConfigSchema } from '../OAuthModel';
import DropboxIcon from './DropboxIcon';
import { getDescriptiveErrorMessage } from './util';
import baseModel from '../OAuthModel/model';
const stateModelFactory = (configSchema) => {
return baseModel(OAuthConfigSchema)
.named('DropboxOAuthInternetAccount')
.props({
type: types.literal('DropboxOAuthInternetAccount'),
configuration: ConfigurationReference(configSchema),
})
.views(() => ({
get toggleContents() {
return _jsx(DropboxIcon, {});
},
get selectorLabel() {
return 'Enter Dropbox share link';
},
}))
.actions(self => ({
getFetcher(location) {
return async (input, init) => {
const authToken = await self.getToken(location);
const newInit = self.addAuthHeaderToInit({ ...init, method: 'POST' }, authToken);
newInit.headers.append('Dropbox-API-Arg', JSON.stringify({ url: input }));
const response = await fetch('https://content.dropboxapi.com/2/sharing/get_shared_link_file', newInit);
if (!response.ok) {
throw new Error(await getDescriptiveErrorMessage(response));
}
return response;
};
},
async validateToken(token, location) {
const response = await fetch('https://api.dropboxapi.com/2/sharing/get_shared_link_metadata', {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: location.uri,
}),
});
if (!response.ok) {
const refreshToken = self.retrieveRefreshToken();
if (refreshToken) {
self.removeRefreshToken();
const newToken = await self.exchangeRefreshForAccessToken(refreshToken);
return this.validateToken(newToken, location);
}
throw new Error(await getDescriptiveErrorMessage(response, 'Token could not be validated'));
}
return token;
},
}));
};
export default stateModelFactory;