package com.softwaremill.codebrag.rest

import com.softwaremill.codebrag.service.github.GitHubAuthService
import org.scalatra.SeeOther
import com.softwaremill.codebrag.dao.UserDAO
import com.softwaremill.codebrag.service.user.Authenticator
import com.softwaremill.codebrag.domain.{User, Authentication}
import java.util.UUID


class GithubAuthorizationServlet(val authenticator:Authenticator, ghAuthService:GitHubAuthService, userDao:UserDAO) extends JsonServletWithAuthentication {

  var tmpLogin:String = _
  var tmpPassword:String = _

  get("/auth_callback") {
    val code = params.get("code").get
    val accessToken = ghAuthService.getAccessToken(code)
    val user = ghAuthService.loadUserData(accessToken)
    val auth: Authentication = Authentication.github(user.login, accessToken.access_token)
    userDao.findByEmail(user.email) match {
      case Some(u) => userDao.changeAuthentication(u.id, auth)
      case None => userDao.add(User(auth, user.name, user.email, UUID.randomUUID().toString))
    }
    tmpLogin = user.login
    tmpPassword = accessToken.access_token
    authenticate()
    SeeOther("/")
  }

  override protected def login: String = tmpLogin

  override protected def password: String = tmpPassword

  override protected def rememberMe: Boolean = true
}




package com.softwaremill.codebrag.rest

import com.softwaremill.codebrag.service.github.GitHubAuthService
import org.scalatra.SeeOther
import com.softwaremill.codebrag.dao.UserDAO
import com.softwaremill.codebrag.service.user.Authenticator


class GithubAuthorizationServlet(val authenticator:Authenticator, ghAuthService:GitHubAuthService, userDao:UserDAO) extends JsonServletWithAuthentication {

  var tmpLogin:String = _
  var tmpPassword:String = _

  get("/auth_callback") {
    val code = params.get("code").get
    val accessToken = ghAuthService.getAccessToken(code)
    val user = ghAuthService.loadUserData(accessToken)
    userDao.findByEmail(user.email) match {
      case Some(u) =>
      case None =>
    }
    tmpLogin = user.login
    tmpPassword = accessToken.access_token
    authenticate()
    SeeOther("/")
  }

  override protected def login: String = tmpLogin

  override protected def password: String = tmpPassword
}




--- 
+++ 
@@ -4,6 +4,8 @@
 import org.scalatra.SeeOther
 import com.softwaremill.codebrag.dao.UserDAO
 import com.softwaremill.codebrag.service.user.Authenticator
+import com.softwaremill.codebrag.domain.{User, Authentication}
+import java.util.UUID
 
 
 class GithubAuthorizationServlet(val authenticator:Authenticator, ghAuthService:GitHubAuthService, userDao:UserDAO) extends JsonServletWithAuthentication {
@@ -15,9 +17,10 @@
     val code = params.get("code").get
     val accessToken = ghAuthService.getAccessToken(code)
     val user = ghAuthService.loadUserData(accessToken)
+    val auth: Authentication = Authentication.github(user.login, accessToken.access_token)
     userDao.findByEmail(user.email) match {
-      case Some(u) =>
-      case None =>
+      case Some(u) => userDao.changeAuthentication(u.id, auth)
+      case None => userDao.add(User(auth, user.name, user.email, UUID.randomUUID().toString))
     }
     tmpLogin = user.login
     tmpPassword = accessToken.access_token
@@ -28,6 +31,8 @@
   override protected def login: String = tmpLogin
 
   override protected def password: String = tmpPassword
+
+  override protected def rememberMe: Boolean = true
 }
 
 
Update authentication and remember user signed in with GitHub

Update existing user with new credentials or create a new user. Enable remembering of user who signed in with GitHub.
