Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.readFile(service_account_json, "utf-8", async (err, data) => {
var parsed_json = JSON.parse(data);
const iat = Math.floor(new Date().getTime() / 1000);
const exp = iat + 3600; // 3600 seconds = 1 hour
var payload = {
iss: parsed_json.client_email,
aud: "https://www.googleapis.com/oauth2/v4/token",
exp: exp,
iat: iat,
target_audience: "https://yourapp.appspot.com/"
}
const signature = jws.sign({
header: {alg: 'RS256', typ: 'JWT', kid: parsed_json.private_key_id},
payload: payload,
privateKey: parsed_json.private_key
});
console.log("***************** Google Id Token *****************");
console.log(signature);
console.log("Exchanging JWT for Google ID Token");
data = {
'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' : signature
}
var config = {
headers : {"Content-type": "application/x-www-form-urlencoded"}
function generateJWT(payload, secret, alg) {
var body = {
header: { alg: alg || 'HS256' }, // Default to hash
secret: secret,
payload: payload };
return jwt.sign(body);
}
function token(user) {
let now = (Date.now())/1000
return jws.sign({
header: { alg: 'HS256' },
payload: {
exp: now+60,
iat: now,
nbf: now-2,
name: user
},
secret: secret,
})
}
exports.generateClientAssertion = function(clientID, token_endpoint, privatePEMKey, thumbprint, callback) {
var header = { 'x5t': thumbprint, 'alg': 'RS256', 'typ': 'JWT' };
var payload = {
sub: clientID,
iss: clientID,
jti: Date.now() + aadutils.uid(16),
nbf: Math.floor(Date.now()/1000),
exp: Math.floor(Date.now()/1000) + CONSTANTS.CLIENT_ASSERTION_JWT_LIFETIME,
aud: token_endpoint,
};
var clientAssertion;
var exception = null;
try {
clientAssertion = jws.sign({
header: header,
payload: payload,
privateKey: privatePEMKey
});
} catch (ex) {
exception = ex;
}
callback(exception, clientAssertion);
};
function sign (payload, secretOrPrivateKey, options) {
options = options || {}
var header = {typ: 'JWT', alg: options.algorithm || 'HS256'}
return jws.sign({header: header, payload: payload, secret: secretOrPrivateKey})
}
getKuskiData(kuski, encryptedPassword).then(kuskiData => {
if (kuskiData) {
if (encryptedPassword === kuskiData.dataValues.Password) {
const token = jws.sign({
header: { alg: config.jwtAlgo },
payload: {
username: kuskiData.dataValues.Kuski,
userid: kuskiData.dataValues.KuskiIndex,
},
secret: config.jwtSecret,
});
resolve({
success: true,
token,
username: kuskiData.dataValues.Kuski,
userid: kuskiData.dataValues.KuskiIndex,
});
} else {
resolve({
success: false,
sign: function(msg, privKeyPEM, hex, skipValidation) {
if (!skipValidation) {
validate(msg);
}
msg.jwsHeader = { alg: 'ES256', kid: hex };
msg.jws = jws.sign({
header: msg.jwsHeader,
payload: msg.signedData,
privateKey: privKeyPEM
});
if (!skipValidation) {
validateJws(msg);
}
msg.hash = getHash(msg).toString(encoding);
return msg.jws;
},
GoogleToken.prototype._signJWT = function(opts, callback) {
try {
var signedJWT = jws.sign(opts);
return callback(null, signedJWT);
} catch (err) {
callback(err, null);
}
};