@midware/mauth
Version:
A simple auth middleware for Node.js
82 lines • 3.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const google_auth_library_1 = require("google-auth-library");
const unauthorizedError_1 = __importDefault(require("./util/unauthorizedError"));
class Google {
constructor(updatePicture) {
this.updatePicture = updatePicture;
}
async verify(identification, identifications, headers) {
const error = new unauthorizedError_1.default('GAccount error.');
// console.log('verifyGoogle');
if (!(await this.compare(identification, identifications, headers))) {
// console.log('KeyService.compare FALSE');
throw error;
}
}
async checkPicture(identification, headers, item, payload) {
if (payload && payload.picture && payload.picture !== headers.picture) {
// console.log(payload.picture);
if (item) {
if (Array.isArray(item)) {
for (const received of item) {
received.picture = payload.picture;
}
}
else
item.picture = payload.picture;
}
else
this.updatePicture(identification, payload.picture);
}
}
async checkToken(identification, headers, item) {
return new Promise(async (resolve) => {
// console.log('checkToken');
// console.log(identification);
// console.log(headers);
// console.log(item);
if (headers && headers.tokenid) {
const client = new google_auth_library_1.OAuth2Client(process.env.GOOGLE_CLIENT_ID);
const ticket = await client.verifyIdToken({
idToken: headers.tokenid,
audience: process.env.GOOGLE_CLIENT_ID,
// Specify the CLIENT_ID of the app that accesses the backend
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
});
const payload = ticket.getPayload();
// console.log(process.env.GOOGLE_CLIENT_ID);
// console.log(headers.tokenid);
// console.log(headers);
// console.log(payload);
// console.log(identification);
if (!(payload &&
payload['email'] === identification.identification &&
payload['email_verified']))
resolve(false);
// console.log(payload);
// console.log(item);
this.checkPicture(identification, headers, item, payload);
resolve(true);
// const userid = payload['sub'];
// If request specified a G Suite domain:
//const domain = payload['hd'];
}
resolve(false);
});
}
async compare(rIdentification, identifications, headers) {
for (const identification of identifications) {
if (identification.identification === rIdentification.identification)
if (await this.checkToken(rIdentification, headers))
return true;
}
return false;
}
}
exports.default = Google;
//# sourceMappingURL=google.js.map