UNPKG

@jbrowse/plugin-authentication

Version:

JBrowse 2 Authentication

53 lines (52 loc) 1.94 kB
import { ConfigurationReference, getConf } from '@jbrowse/core/configuration'; import { InternetAccount } from '@jbrowse/core/pluggableElementTypes/models'; import { getRoot, types } from 'mobx-state-tree'; import { HTTPBasicLoginForm } from './HTTPBasicLoginForm'; import { getResponseError } from '../util'; const stateModelFactory = (configSchema) => { return InternetAccount.named('HTTPBasicInternetAccount') .props({ type: types.literal('HTTPBasicInternetAccount'), configuration: ConfigurationReference(configSchema), }) .views(self => ({ get validateWithHEAD() { return getConf(self, 'validateWithHEAD'); }, })) .actions(self => ({ getTokenFromUser(resolve, reject) { const { session } = getRoot(self); session.queueDialog((doneCallback) => [ HTTPBasicLoginForm, { internetAccountId: self.internetAccountId, handleClose: (token) => { if (token) { resolve(token); } else { reject(new Error('User cancelled entry')); } doneCallback(); }, }, ]); }, async validateToken(token, location) { if (!self.validateWithHEAD) { return token; } const newInit = self.addAuthHeaderToInit({ method: 'HEAD' }, token); const response = await fetch(location.uri, newInit); if (!response.ok) { throw new Error(await getResponseError({ response, reason: 'Error validating token', })); } return token; }, })); }; export default stateModelFactory;