@jbrowse/plugin-authentication
Version:
JBrowse 2 Authentication
73 lines (72 loc) • 3.1 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = stateModelFactory;
const jsx_runtime_1 = require("react/jsx-runtime");
const configuration_1 = require("@jbrowse/core/configuration");
const mobx_state_tree_1 = require("mobx-state-tree");
const OAuthModel_1 = require("../OAuthModel");
const GoogleDriveFilehandle_1 = require("./GoogleDriveFilehandle");
const GoogleDriveIcon_1 = __importDefault(require("./GoogleDriveIcon"));
const util_1 = require("./util");
const model_1 = __importDefault(require("../OAuthModel/model"));
function getUri(str) {
const urlId = /[-\w]{25,}/.exec(str);
return `https://www.googleapis.com/drive/v3/files/${urlId}`;
}
function stateModelFactory(configSchema) {
return (0, model_1.default)(OAuthModel_1.configSchema)
.named('GoogleDriveOAuthInternetAccount')
.props({
type: mobx_state_tree_1.types.literal('GoogleDriveOAuthInternetAccount'),
configuration: (0, configuration_1.ConfigurationReference)(configSchema),
})
.views(() => ({
get toggleContents() {
return (0, jsx_runtime_1.jsx)(GoogleDriveIcon_1.default, {});
},
get selectorLabel() {
return 'Enter Google Drive share link';
},
}))
.actions(self => ({
getFetcher(location) {
return async (input, init) => {
const driveUrl = new URL(getUri(String(input)));
const searchParams = new URLSearchParams();
if (init === null || init === void 0 ? void 0 : init.metadataOnly) {
searchParams.append('fields', 'size');
}
else {
searchParams.append('alt', 'media');
}
driveUrl.search = searchParams.toString();
const authToken = await self.getToken(location);
const response = await fetch(driveUrl, self.addAuthHeaderToInit({ ...init, method: 'GET', credentials: 'same-origin' }, authToken));
if (!response.ok) {
throw new Error(await (0, util_1.getDescriptiveErrorMessage)(response));
}
return response;
};
},
openLocation(location) {
return new GoogleDriveFilehandle_1.GoogleDriveFile(location.uri, {
fetch: this.getFetcher(location),
});
},
async validateToken(token, location) {
const response = await fetch(getUri(location.uri), {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
});
if (!response.ok) {
throw new Error(await (0, util_1.getDescriptiveErrorMessage)(response, 'Token could not be validated'));
}
return token;
},
}));
}
;