Cronjob with Twitter - cron

I want to send tweets from my users' twitter accounts using cronjob that signed up or connected to my site using twitter. But the problem is
Oauth keys are valid only while session is active, so storing oauth keys info of user's in database is useless (Am i right about this ? Not really sure, please correct me if i am wrong.)
So when the session ends, oauth key gets useless, and user needs to get new oauth keys.
And my question is : is there a way to do what i had written above ? Sending tweets from my users's twitter accounts.
I am newbie about this Twitter api thing, and also newbie on PHP. So i would like to get the "logic" of the way doing this or maybe some examples about it.

Twitter's OAuth 1.0a access tokens do not currently expire. You can save them in a database and they will keep working until the user revokes access.

I'd recommend "twitteroauth" library for OAuth on Twitter: https://github.com/abraham/twitteroauth. Here is an example code with the twitteroauth library.
<?php
// Load twitteroauth library.
require_once __DIR__ . '/twitteroauth/twitteroauth.php';
// Create a TwitterOAuth object.
$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
// Post a message.
$oauth->post('statuses/update', array('status' => "Hello, world!!"));
See: https://github.com/abraham/twitteroauth/blob/master/DOCUMENTATION

Related

Handling Social Media Integrations in a MEAN stack App after a user is Logged in

A user can create an account in my App only with his work email.
Example: john#xyzcompany.com
After he creates an account, he can link multiple social media accounts to his profile.
Example: john#gmail.com, john2#gmail.com
I'm using MEAN stack to develop the App.
When a user logs in to my app, I'm creating a JWT token to authorize all his future requests to the server.
When it comes to Social Media accounts Integrations, After successful authentication I'm able to receive the accessTokens from these Social Media to the backend callback URL. I need to link the tokens to the right user. Is there anyway I can pass my JWT token along with the callback URL to identify which user has made the request, because I cannot identify the user based on the email in his Social Media Account?
I was able to solve this using socket.io. But I feel it is unnecessary to use sockets for simple authentication.
Is there any other approach to solve it? I have researched online, few of them suggested using passport. I don't fully understand how passport works, I just feel it is just a middleware to authenticate API requests from users, which I'm doing anyway using a custom function.
Could someone explain whether it is possible to pass JWT in callback URLs using passport strategies?
What is the right approach to handle such authentications in a MEAN stack app? I'm stuck with this issue since the past week, really looking forward for a good solution.
I have encountered this type of situation in one of the large scale applications I have been working for and the approach we used to handle it was to store the jwtToken in redis and then retrieve it back with the help of user cookies. Let me explain it in more detail -
Send a new Cookie for the user when the user opens the login page. The cookie should contain some unique id or code against which we will save the JWT token,. Eg: res.cookie('jwtIdentifier', newid())
Now, when the user logs in, generate the JWT token and save it to your redis against the cookie jwtIdentifier's value. Eg: redisHelper.set(req.cookies.jwtIdentifier, JWTTOKEN)
At last, when the login is successful and the user is redirected back to your app, you can retrieve your JWT token again for the corresponding user using the cookie. Eg: redisHelper.get(req.cookies.jwtIdentifier) This will give you back the JWT token that you can then use across your application for that specific user.
Hope it's clear, let me know if you have any questions with this implementation ;)
You might try using client side facebook authentication as described here
https://theinfogrid.com/tech/developers/angular/facebook-login-angular-rest-api/
in this case in angular app you have facebook token alongside your app token and you can send them to your backend to identify the current user.
on backend part you eill call facebook to get profile data from accessToken and then store user profile id and depending on your business you might need also to store the access token

Prevent a malicious user to fill my database by sign up a lot of accounts?

I am developping a REST API with node.JS. I saw a lot of tutorials to secure my API, especially with JWT Library, but I think about an issue to this process :
I need to ask to my users to signup sending to my server an id and a password that I can store in my database to generate a token for the following actions. But what if a malicious user wanted to add a lot of user to my database by sending to the URI of my server a lot of time informations to sign up a lot of time ?
Is there a way to prevent an "attack" like this ?
Is Auth0 can help me ?
What you are looking for is something to limit or throttle requests made to your API endpoints. A good solution is to set up a middleware such as express-rate-limit.

How to do authentication for an API build with express?

There are quite a few examples and tutorials on authentication and node out there, as well as several questions on stackoverflow. I'm still struggeling with this subject however when trying to implement authentication for an API which communicates with a SPA. I tried using mean.js as an example as well as to use JWT and passport.js. But even after some days of research and trial and error it is still unclear to me how to achieve the following scenario:
A user registers himself with a username (or email) and a password (and gets an email to verify his account)
The user logs himself in with the password and username.
The user recieves a token as a response
The token is used in all following requests which require authentication (and is invalidated after a given amount of time).
If the user logs out, the token is invalidated and he gets a new one the next time he logs in.
At a later point of time I also would like to implement Facebook and Google Login (that's why I would like to use passport.js).
I'm glad for any help and also open to suggestions for a better authentication flow.
Looking into Firebase (https://www.firebase.com/). It simplifies the process of handling Auth and all the perils of trying to handle Auth yourself.
Firebase also supports all of the major Auth providers out of the box (Facebook, Google, Twitter, etc).
I ended up using the node-example of satellizer:
https://github.com/sahat/satellizer/blob/master/examples/server/node/server.js#L36
You store password and username to the database on signup.
For logging in one can you simply compare the (hashed) password with the proved one and send back a token you create via jwt.encode (jsonwebtoken).
After that you append the token to your requests (in the header is probably the best way) and check its validity via jwt.decode(token, TOKEN_SECRET)

Facebook login flow with to nodejs

I am working on a REST API backend service for an app that uses both email and facebook login. Lets just pretend this app is a todo list app and a user can sign in and add notes which they could later view on may different devices.
The "Login with email" is pretty simple, the app would make a request to:
URL: /v1/login
Params: email, password
Then the serivce returns an access token if all this information is correct so we know the identity of the user creating, updating or deleting a note/list item.
Now for the facebook side. I've seen several differnet answers all over StackOverflow and Facebook's documentation. Some people say, just pass in the id and login the user with the matching id. This would mean calling the login function from the Facebook SDK and just keeping that id to send in a request.
URL: /v1/login/facebook
Params: id
That would work but seems highly unsecure. Anyone could get someone else's Facebook id and just send a request to the server. Facebook's documentation mentions also using the account email. We'll what if the user ever changes their email on Facebook, they could never login to this app again. Another hint of security would be passing in the token from Facebook every time. Since those tokens can change or expire, there really wouldn't be a way login past the expiration date.
The final method I know of would be passing in the app secret:
URL: /v1/login/facebook
Params: id, secret
This seems by far the most secure, if you are using https to connect to the server. The downside to this method is, what if the Facebook secret token is ever reset? I mean you could always make a call to the server to request and check if token was reset and if so pass back the new one. But then if anyone had access to the endpoint to check out of date tokens, it could give them the new secret token.
I know we are dealing with the internet here and there will always be security flaws and there isn't a perfect way to do this. But what is the best way?
Try to send facebook_token for that user.
URL: /v1/login/facebook
Params: facebook_token
and service side make a service call to facebook graph api to get information about that user using facebook_token.
get the facebook id from that response and use it to provide data to that user.

Post to twitter, improve security

I have a web site and every client has his own account. When clients create new articles, titles should go to their own twitter accounts.
The question is how to make it in background? I can ask user for twitter login/password - and save this data in my database. But the problem is with security: password will be stored unencrypted.
Does twitter have better way to organize this flow?
thank you
p.s.
to make it clear - background sending is must - as some clients use API to access my services
OAuth is the solution to your problem. OAuth is supported by Twitter.
By using OAuth, each of your users may authorize you to post tweets on their behalf. You will get a token for each user. You must store this token in your database, but you will not need the end users username or password.

Resources