Signing emails with DKIM in Node.js - javascript

I'm writing a Nodejs app that needs to be able to send email. So far, I've used Postfix in conjunction with a Nodejs module called Nodemailer to send my email through Amazon SES.
Postfix has been handling the DKIM signing, but now I wish to get rid of postfix and just use Nodemailer to send emails through Amazon SES.
My only problem now is finding a way to sign emails within Nodejs. I've thought of running a opendkim command using "exec" in node but haven't been able to figure that out. From searching, there looks to be no modules for this either.
Can anyone help me on this?

Latest version of Nodemailer supports DKIM signing out of the box, also tested with SES.
var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: "AWSACCESSKEY",
AWSSecretKey: "AWS/Secret/key"
});
// all messages sent with *transport* are signed with the following options
transport.useDKIM({
domainName: "example.com",
keySelector: "dkimselector",
privateKey: fs.readFileSync("private_key.pem")
});
transport.sendMail(...);

you can find at https://gist.github.com/2198497 an implementation I developped to dkim-sign mails sent through SES. It's heavily inspired by the php implementation by Ahmad Amarullah found here : http://code.google.com/p/php-mail-domain-signer/. I'm well aware the code is far from clean, but it should help you get started. The mails sent through it are considered correct by gmail and yahoo. Don't hesitate if you have questions / can't get it to work.

Related

NodeJS REST call with certificate. How could I know which file to use?

I'm trying to open a connexion to an IBM Cognos TM1 server through NodeJS/Axios and to do so I need to pass, during the first REST API Call everything which is needed to authenticate :
an username and password which I have
certificate information as SSL is set to true (I don't want to ignore self signed certificates)
I've found examples on how to pass certificate information with Axios but at this time I didn't succeed in getting it to work :-(
I've put in a dedicated folder, the whole ssl folder a TM1 client software is using:
E:\Development\nodejs\tm1query\assets\ssl\applixca.pem
E:\Development\nodejs\tm1query\assets\ssl\ibmtm1.arm
E:\Development\nodejs\tm1query\assets\ssl\ibmtm1.crl
E:\Development\nodejs\tm1query\assets\ssl\ibmtm1.kdb
E:\Development\nodejs\tm1query\assets\ssl\ibmtm1.rdb
E:\Development\nodejs\tm1query\assets\ssl\ibmtm1.sth
E:\Development\nodejs\tm1query\assets\ssl\importsslcert.exe
E:\Development\nodejs\tm1query\assets\ssl\tm1ca_v2.der
E:\Development\nodejs\tm1query\assets\ssl\tm1ca_v2.pem
E:\Development\nodejs\tm1query\assets\ssl\tm1store
E:\Development\nodejs\tm1query\assets\ssl\uninstallSSL.bat
E:\Development\nodejs\tm1query\assets\ssl\applixca.der
My problem is, which file do I need to use when creating the agent which will hold the connexion ?
Thanks in advance for helping or pointing me to information which can help me to go further in my understanding of how certificates works.
Regards,
Bob

facebook graph api node.js invalid appsecret_proof

this is my first post so please go easy on me!
I am a beginning developer working with javascript and node.js. I am trying to make a basic request from a node js file to facebook's graph API. I have signed up for their developer service using my facebook account, and I have installed the node package for FB found here (https://www.npmjs.com/package/fb). It looks official enough.
Everything seems to be working, except I am getting a response to my GET request with a message saying my appsecret_proof is invalid.
Here is the code I am using (be advised the sensitive info is just keyboard mashing).
let https = require("https");
var FB = require('fb');
FB.options({
version: 'v2.11',
appId: 484592542348233,
appSecret: '389fa3ha3fukzf83a3r8a3f3aa3a3'
});
FB.setAccessToken('f8af89a3f98a3f89a3f87af8afnafmdasfasedfaskjefzev8zv9z390fz39fznabacbkcbalanaa3fla398fa3lfa3flka3flina3fk3anflka3fnalifn3laifnka3fnaelfafi3eifafnaifla3nfia3nfa3ifla');
console.log(FB.options());
FB.api('/me',
'GET',
{
"fields": "id,name"
},
function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res);
console.log(res.id);
console.log(res.name);
}
);
The error I am getting reads:
{ message: 'Invalid appsecret_proof provided in the API argument',
type: 'GraphMethodException',
code: 100,
fbtrace_id: 'H3pDC0OPZdK' }
I have reset my appSecret and accessToken on the developer page and tried them immediately after resetting them. I get the same error, so I don't think that stale credentials are the issue. My
console.log(FB.options())
returns an appropriate looking object that also contains a long hash for appSecretProof as expected. I have also tried this code with a number of version numbers in the options (v2.4, v2.5, v2.11, and without any version key). Facebook's documentation on this strikes me as somewhat unclear. I think I should be using v2.5 of the SDK (which the node package is meant to mimic) and making requests to v2.11 of the graph API, but ??? In any case, that wouldn't seem to explain the issue I'm having. I get a perfectly good response that says my appSecretProof is invalid when I don't specify any version number at all.
The node package for fb should be generating this appSecretProof for me, and it looks like it is doing that. My other info and syntax all seem correct according to the package documentation. What am I missing here? Thank you all so much in advance.
looks like you have required the appsecret_proof for 2 factor authorization in the advance setting in your app.
Access tokens are portable. It's possible to take an access token generated on a client by Facebook's SDK, send it to a server and then make calls from that server on behalf of the client. An access token can also be stolen by malicious software on a person's computer or a man in the middle attack. Then that access token can be used from an entirely different system that's not the client and not your server, generating spam or stealing data.
You can prevent this by adding the appsecret_proof parameter to every API call from a server and enabling the setting to require proof on all calls. This prevents bad guys from making API calls with your access tokens from their servers. If you're using the official PHP SDK, the appsecret_proof parameter is automatically added.
Please refer the below url to generate the valid appsecret_proof,and add it to each api call
https://developers.facebook.com/docs/graph-api/securing-requests
I had to deal with the same issue while working with passport-facebook-token,
I finally released that the problem had nothing to have with the logic of my codebase or the app configuration.
I had this error just because I was adding intentionally an authorization Header to the request. so if you are using postman or some other http client just make sure that the request does not contain any authorization Header.

Best way to check if Twilio authentication is successful with JS SDK

What is the best way to check if Twilio auht_token, account_sid are correct and sms can be sent, number checked? Some call which doesn't cost extra credits?
E.g. I see https://www.twilio.com/docs/api/rest/usage-records on RESTfull documentation but can't find how to get the same thing with JS SDK. Can't see dedicated endpoint for config checking so looking for anything else.
Environment: NodeJS 8.9
Twilio developer evangelist here.
Most API calls to the Twilio REST API don't cost, particularly those where you retrieve a resource or list resources. Since you mentioned SMS you could, for example, list your latest messages like this:
const client = require('twilio')(accountSid, authToken);
client.messages.list({ limit: 10 })
.then(function(messages) {
console.log("Everything is good!");
})
.catch(function(err) {
console.error("Something went wrong: ", err)
})
Take a look through the API reference and pick one that takes your fancy.
Using JS SDK might be insecure here. Because of that I think they didn't include a method in the JS API which may present the user the account_sid and the auth_token, which may be exploited. I assume you can use a server bridge between your client JS and Twilio API. Like this:
Client makes a JS AJAX request to http://my.domain.tld/checkstatus
Server connects to the Twilio API with C#, PHP, NodeJS or whatever tech it uses
Twilio returns that the credentials and tokens are still valid or expired
Server prepares the client response as true/false or 0/1
Client reads the status and continues or redirects somewhere else.
Edit There's a GET method here which you can also use with JS AJAX call:
https://www.twilio.com/docs/api/rest/usage-records#list-get
which is requested by this format:
/2010-04-01/Accounts/{AccountSid}/Usage/Records/{Subresource}

How can I embed Twilio's sms service in an html file?

All I found was with php and node.js (which is based on js, so it should be fine), but I got across this library:
<script type="text/javascript"
src="//media.twiliocdn.com/sdk/js/client/v1.4/twilio.min.js"></script>
What is the relavant JS code to make a "send sms" request?
This is the php I found:
<?php
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
$client = new Client($sid, $token);
$client->messages->create(
'+15558675309', // number to send to
array(
'from' => '+15017250604', // your Twilio number
'body' => "There’s something strange in my neighborhood. I don’t know who to call. Send help!"
)
);
Thanks.
Twilio developer evangelist here.
We don't recommend that you use the Twilio REST API for sending SMS messages within a public HTML page. If you do so, you will expose your account credentials publicly and a malicious attacker could steal them and send messages or phone calls on your behalf, using up your credit and potentially spamming people.
The JavaScript library you found there is for you to use to make phone calls from within the browser using WebRTC. This is built to not leak your credentials as you need to generate a token server side that can be used to authenticate users.
I recommend you check out the SMS quick start guides in a language of your choice to see how you can write server side code to send messages.

Attaching image to email through Parse's Sendgrid module using JavaScript

I was wondering if anyone knew how to send an email with an image attached to it from Parse's Sendgrid module (by this, I mean Facebook's Parse BaaS, not SendGrid's Parse API). So far, I can send out emails, but not with an image attached to it. I tried two different things. One is sending the email as Base64, but I read that is not supported by A LOT of email providers, therefore I was discouraged in using this method since compatibility is kind of an issue here (not critical though). My second approach was to try to mimic sendgrid's process of using a cid, but maybe I was doing something wrong and it did not work
var sendGridInstance = require('sendgrid');
sendGridInstance.initialize(sendGridUser, sendGridKey);
sendGridInstance.sendEmail({
to:endCustomerEmail,
from: 'test#test.com',
subject: 'Test subject',
html: 'My HTML goes here..',
replyto: 'donotreply#test.com'
The code above works whenever it is called in CloudCode, and indeed sends an email. But the cid thing does not work.
Has anyone successfully sent an email w/image using the sendgrid module with Parse? If so, could you please tell me what am I missing?
All help is much appreaciated!
Thank you!
Cheers!
This module doesn't support binary files - it calls SendGrid API with application/x-www-form-urlencoded request and you can't urlencode contents of binary file.
Take a look at https://github.com/m1gu3l/parse-sendgrid-mailer instead - it calls API with multipart/form-data request which is better suited for this case.

Categories