Created
August 17, 2014 08:07
-
-
Save krrrr38/d709ca3a8cf92e4294b7 to your computer and use it in GitHub Desktop.
SecureSocial 3.0(milestone 1) with Custom OAuth Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := """securesocial-hatena""" | |
version := "1.0-SNAPSHOT" | |
lazy val root = (project in file(".")).enablePlugins(PlayScala) | |
scalaVersion := "2.11.1" | |
libraryDependencies += "ws.securesocial" %% "securesocial" % "master-SNAPSHOT" | |
resolvers += Resolver.sonatypeRepo("snapshots") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Constructor | |
import providers.HatenaProvider | |
import securesocial.core.RuntimeEnvironment | |
import securesocial.core.providers._ | |
import service.{DemoUser, MyEventListener, InMemoryUserService} | |
import scala.collection.immutable.ListMap | |
object Global extends play.api.GlobalSettings { | |
/** | |
* The runtime environment for this sample app. | |
*/ | |
object MyRuntimeEnvironment extends RuntimeEnvironment.Default[DemoUser] { | |
override lazy val userService: InMemoryUserService = new InMemoryUserService() | |
override lazy val eventListeners = List(new MyEventListener()) | |
override lazy val providers = ListMap( | |
// oauth 1 client providers | |
include(new HatenaProvider(routes, cacheService, oauth1ClientFor(HatenaProvider.Hatena))), | |
// username password | |
include(new UsernamePasswordProvider[DemoUser](userService, avatarService, viewTemplates, passwordHashers)) | |
) | |
} | |
override def getControllerInstance[A](controllerClass: Class[A]): A = { | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package providers | |
import securesocial.core._ | |
import securesocial.core.services.{CacheService, RoutesService} | |
import scala.concurrent.{ExecutionContext, Future} | |
/** | |
* A Hatena Provider | |
*/ | |
class HatenaProvider( | |
routesService: RoutesService, | |
cacheService: CacheService, | |
client: OAuth1Client | |
) extends OAuth1Provider( | |
routesService, | |
cacheService, | |
client | |
) { | |
override val id = HatenaProvider.Hatena | |
override def fillProfile(info: OAuth1Info): Future[BasicProfile] = { | |
import HatenaProvider._ | |
import ExecutionContext.Implicits.global | |
client.retrieveProfile(UserInfoApi, info).map { me => | |
val userId = (me \ UserName).as[String] | |
val fullName = (me \ DisplayName).asOpt[String] | |
val profileUrl = (me \ ProfileImageUrl).asOpt[String] | |
BasicProfile(id, userId, None, None, fullName, None, profileUrl, authMethod, Some(info)) | |
} recover { | |
case e => | |
logger.error("[securesocial] error retrieving profile information from Hatena", e) | |
throw new AuthenticationException() | |
} | |
} | |
} | |
object HatenaProvider { | |
val Hatena = "hatena" | |
val UserInfoApi = "http://n.hatena.com/applications/my.json" | |
val UserName = "user_name" | |
val DisplayName = "display_name" | |
val ProfileImageUrl = "profile_image_url" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment