Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Created August 17, 2014 08:07
Show Gist options
  • Save krrrr38/d709ca3a8cf92e4294b7 to your computer and use it in GitHub Desktop.
Save krrrr38/d709ca3a8cf92e4294b7 to your computer and use it in GitHub Desktop.
SecureSocial 3.0(milestone 1) with Custom OAuth Provider
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")
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 = {
...
}
}
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"
}
#####################################################################################
#
# SecureSocial 2 Settings
#
#####################################################################################
smtp {
host=smtp.gmail.com
#port=25
ssl=true
user="your_user_name"
password="your_password"
from="from_address"
}
securesocial {
onLoginGoTo=/
ssl=false
cookie {
idleTimeoutInMinutes=30
absoluteTimeOutInMinutes=720
}
hatena {
requestTokenUrl="https://www.hatena.com/oauth/initiate"
accessTokenUrl="https://www.hatena.com/oauth/token"
authorizationUrl="https://www.hatena.ne.jp/oauth/authorize"
consumerKey="your_consumer_key"
consumerSecret="your_consumer_secret"
scope="read_public"
}
userpass {
withUserNameSupport=false
sendWelcomeEmail=true
enableGravatarSupport=true
tokenDuration=60
tokenDeleteInterval=5
signupSkipLogin=false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment