minauth
Version:
A TypeScript library for building authentication systems on top of the Mina blockchain and other zero-knowledge proofs solutions.
55 lines • 2 kB
JavaScript
// TODO use logger
import axios from 'axios';
import { Strategy } from 'passport-strategy';
import { MinAuthPluginInputSchema } from '../common/proof.js';
/**
* Forward proof verification to a remote verifier.
*/
const verifyProof = (verifierUrl, data, log) => {
log.info('Calling for proof verification with:', data);
return axios.post(verifierUrl, data).then((resp) => {
if (resp.status == 200) {
log.info('Received response:', resp);
const { output } = resp.data;
return { __tag: 'success', output };
}
const { error } = resp.data;
return { __tag: 'failed', error };
}, (error) => {
return { __tag: 'failed', error: String(error) };
});
};
/**
* Minauth's integration with passport.js.
* This implementation uses the plugin server to verify the proof.
*/
class MinAuthStrategy extends Strategy {
constructor(config) {
super();
this.name = 'MinAuthStrategy';
this.log = config.logger;
this.verifyProof = (data) => verifyProof(config.verifierUrl, data, config.logger);
}
async authenticate(req) {
this.log.info('authenticating (strategy) with req:', req.body);
const loginData = MinAuthPluginInputSchema.parse(req.body);
// forward the proof verification to the plugin server
const result = await this.verifyProof(loginData);
if (result.__tag == 'success') {
const { output } = result;
this.log.debug('proof verification output:', output);
const authResp = {
plugin: loginData.plugin,
output
};
this.success(authResp);
}
else {
const { error } = result;
this.log.info(`unable to authenticate using minAuth: ${error}`);
this.fail({ message: 'Proof validation was not succesful' }, 401);
}
}
}
export default MinAuthStrategy;
//# sourceMappingURL=minauthstrategy.js.map