Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter Login with Scala
package login
import net.liftweb.http.LiftRules
import net.liftweb.http.Req
import net.liftweb.http.GetRequest
import twitter4j.TwitterException
import twitter4j.TwitterFactory
import net.liftweb.http.S
import net.liftweb.common.Box
import net.liftweb.http.LiftResponse
import net.liftweb.util.Props
import net.liftweb.common.Full
import net.liftweb.common.Empty
import net.liftweb.http.SessionVar
import twitter4j.Twitter
import twitter4j.auth.RequestToken
import it.cataalog.model.User
import net.liftweb.common.Loggable
/**
* Handles user login via Twitter.
*/
object TwitterDispatcher extends Loggable {
/**
* Dispatch requests for twitter. We are interested in
* twitter/authenticate, twitter/callback and twitter/logout
*/
def matcher: LiftRules.DispatchPF = {
case req @ Req("twitter" :: "authenticate" :: Nil, _, GetRequest) =>
() => signUpRedirect(req)
case req @ Req("twitter" :: "callback" :: Nil, _, GetRequest) =>
() => processCallBack(req)
case req @ Req("twitter" :: "logout" :: Nil, _, GetRequest) =>
() => logout(req)
}
/**
* Calls Twitter API to authenticate the user.
* Post authentication Twitter would send the token back on callbackURL
* @See https://github.com/yusuke/sign-in-with-twitter/blob/master/src/main/java/twitter4j/examples/signin/SigninServlet.java
*/
def signUpRedirect(req: Req): Box[LiftResponse] = {
val twitter = (new TwitterFactory).getInstance
twitter.setOAuthConsumer(Props.get("twitter4j.oauth.consumerKey").openOr(""),
Props.get("twitter4j.oauth.consumerSecret").openOr(""))
sessionTwitter.set(Full(twitter))
// redirect to authentication URL
try {
val callbackURL = req.hostAndPath + "/twitter/callback" // + S.queryString.openOr("")
val requestToken = twitter.getOAuthRequestToken(callbackURL)
sessionRequestToken.set(Full(requestToken))
S.redirectTo(requestToken.getAuthenticationURL())
} catch {
case te: TwitterException => {
if (401 == te.getStatusCode()) { // Invalid credentials exception
te.printStackTrace
logger.info("Unable to get the access token.")
} else {
te.printStackTrace()
}
S.error("Unable to login with twitter.")
S.redirectTo("/")
}
}
}
/**
* Call back from Twitter post authentication.
* Login the authenticated user or 'create and login' new user.
*/
def processCallBack(req: Req): Box[LiftResponse] = {
(sessionTwitter.is, sessionRequestToken.is, req.param("oauth_verifier")) match {
case (Full(twitter), Full(requestToken), Full(verifier)) => {
try {
twitter.getOAuthAccessToken(requestToken, verifier)
sessionRequestToken.remove
val tuser = twitter.verifyCredentials
val userName = tuser.getScreenName
val imageUrl = tuser.getProfileImageURL.toString
logger.debug("Getting the twitter user as " + tuser)
User.findByUsername(userName) match {
case Empty => {
// Create user in local database
// Login user
// redirect to home
S.redirectTo("/")
}
case Full(user) => {
// sessionNode.set(Full(user))
}
case _ =>
}
S.redirectTo("/")
} catch {
case e: TwitterException => {
throw new Exception(e)
}
}
}
case _ => {
S.error("Authentication error")
S.redirectTo("/")
}
}
}
def logout(req: Req): Box[LiftResponse] = {
sessionTwitter.remove
sessionRequestToken.remove
S.redirectTo("/")
}
}
object sessionTwitter extends SessionVar[Box[Twitter]](Empty)
object sessionRequestToken extends SessionVar[Box[RequestToken]](Empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.