Playã«ã¯ãOpenID 2.0 ã使ãããã®ã©ã¤ãã©ãªãç¨æããã¦ã¾ãã
play.api.libs.openid ã¨ãããã¤ã§ããã
http://www.playframework-ja.org/documentation/2.0.4/ScalaOpenID
ããã使ã£ã¦OpenIDèªè¨¼ãã¦ã¿ãã®ã§ãå¿ããåã«ã¡ã¢ã
Playã®ãã¼ã¸ã§ã³ã¯ã2.1.1ã§ãã
OpenIDèªè¨¼ãã¦ã¿ã
Googleã¢ã«ã¦ã³ãã§OpenIDèªè¨¼ãã¦ã¿ããã¨ã«ã
ãGoogleã§ãã°ã¤ã³ãããã¿ãããªãªã³ã¯ãç¨æãã¦ããã®ãªã³ã¯ãã¯ãªãã¯ãããã¨ãã®ã¢ã¯ã·ã§ã³ãä¸ã®ãã¤ã§ãã
object Auth extends Controller { def authenticate = Action { implicit request => val f: Future[String] = OpenID.redirectURL( "www.google.com/accounts/o8/id", routes.Auth.openIDCallback.absoluteURL() ) f onFailure { case _ => Redirect(routes.Application.login) } AsyncResult(f map(Redirect(_))) } def openIDCallback = TODO }
OpenID.redirectURLã¡ã½ããã«ãOP(OpenID Provider) Identifierã¨èªè¨¼ãæåããå ´åã®ã³ã¼ã«ããã¯URLã渡ãã¾ããã³ã¼ã«ããã¯URLã¯Playã®Routeããåå¾ãã¦ã¾ãã
OP Identifierã¯åOPã®Webãµã¤ãã§ç¢ºèªã§ããã¨æãã¾ããä»åã®OPã¯Googleã
OpenID.redirectURLã¡ã½ããã¯ãOP IdentifierããOPã®èªè¨¼ç»é¢ã®URLãæ¢ç´¢ãã¦ããã¾ãããã®æ¢ç´¢ã§ã¯HTTPéä¿¡ãçºçããã®ã§ãFuture[String]ãè¿ãã¦ãã¾ãã
Futureã«å
ã¾ããStringã¯OPã®èªè¨¼ç»é¢ã®URLã§ãããä¸ã®authenticateã¢ã¯ã·ã§ã³ã®ã¬ã¹ãã³ã¹ã¯ãã®URLã¸ã®ãªãã¤ã¬ã¯ãã¨ãªã£ã¦ã¾ãã
ãªãã¤ã¬ã¯ãå ã®OPã®èªè¨¼ç»é¢ã§ãGoogleã¢ã«ã¦ã³ãã®èªè¨¼ãå®äºããã¨ã第2å¼æ°ã§æå®ããã³ã¼ã«ããã¯URLã¸ãªãã¤ã¬ã¯ãããã¾ãã
ããã§OpenIDã使ã£ãèªè¨¼ã¯ã¨ããããã§ãã¾ããã
ã»ãã·ã§ã³ã使ã
次ã¯ãã¦ã¼ã¶æ
å ±ãã»ãã·ã§ã³ã«æ ¼ç´ãã¦ã¿ã¾ãã
以ä¸ã®openIDCallbakãOPã®ç»é¢ã§ã®èªè¨¼å¾ã«ãªãã¤ã¬ã¯ããããã¢ã¯ã·ã§ã³ã
object Auth extends Controller { def authenticate = Action { implicit request => val f: Future[String] = OpenID.redirectURL( "www.google.com/accounts/o8/id", routes.Auth.openIDCallback.absoluteURL() ) f onFailure { case _ => Redirect(routes.Application.login) } AsyncResult(f map(Redirect(_))) } def openIDCallback = Action { implicit request => val f: Future[UserInfo] = OpenID.verifiedId f onFailure { case _ => Redirect(routes.Application.login) } AsyncResult( f map( info => Redirect(routes.Application.index).withSession("openid" -> info.id) ) ) } }
OpenID.verifiedIdã¡ã½ããã¯ãimplicitãã©ã¡ã¼ã¿ã¨ãã¦requestãªãã¸ã§ã¯ããåãåãã¾ãã
ãªãã¤ã¬ã¯ãã§æ¸¡ã£ã¦ãããã©ã¡ã¼ã¿ãæ£ãããOPã«å¯¾ãã¦åãåããããããçµæã¯Future[UserInfo]ã§ãã
UserInfoã«ã¯ãã¦ã¼ã¶ãèå¥ããããã®IDãæã£ã¦ã¾ãã
ããã¤ãã»ãã·ã§ã³ã«æ ¼ç´ãã¦ãã°ã¤ã³ç¶æ
ã«ã§ãã¾ãã
ãã°ã¤ã³ç¶æ ããã§ãã¯ããå¦çãå ±éåãã
ä¸ã®ä¾ã§IDãã»ãã·ã§ã³ã«æ ¼ç´ããç¶æ
ã§ãApplicationã³ã³ããã¼ã©ã®indexã¢ã¯ã·ã§ã³ã¸ãªãã¤ã¬ã¯ãããã¾ããã
indexã¢ã¯ã·ã§ã³ã§ã¯ãã»ãã·ã§ã³æ
å ±ã確èªãã¦ããã°ã¤ã³ãã¦ãããã©ããã§å¦çãåå²ããããããã¨ããã®ãã°ã¤ã³å¤å®ã¯å
±éåãããã
ããããå
±éå¦çã¯ãã¢ã¯ã·ã§ã³ã®åæã£ã¦ã®ã使ãã°ããããã
http://www.playframework-ja.org/documentation/2.0.4/ScalaActionsComposition
object Application extends Controller { def openid(request: RequestHeader): Option[String] = request.session.get("openid") def onUnauthorized(request: RequestHeader): Result = Results.Redirect(routes.Application.login) def withAuth(f: => String => Request[AnyContent] => Result) = { Security.Authenticated(openid, onUnauthorized) { user => Action(request => f(user)(request)) } } def index = withAuth { openid => implicit request => // ã»ã»ã» } }
ã¾ããwithAuthã£ã¦ããã¡ã½ãããç¨æããã®ã¡ã½ããã¯ã«ãªã¼åãããé¢æ°ãåãåã£ã¦Actionãè¿ãããã«ãã¾ãã
ããã¦ããªãã¤ã¬ã¯ãå
ã¨ãªãindexã¢ã¯ã·ã§ã³ã§ã¯ãActionã使ã代ããã«ãwithAuthã使ã£ã¦Actionãªãã¸ã§ã¯ããä½ãã¾ãã
withAuthã®å®è£
ã§ããããã°ã¤ã³ãã¦ãããã©ããã§ãåå²ããããã«ãã¾ãã
ããããã£ã¦ããããã«ãã¼çãªãã®ãPlayã«ã¯ç¨æããã¦ã¾ããã
play.api.mvc.Security.Authenticatedã§ãã
http://www.playframework.com/documentation/api/2.1.1/scala/index.html#play.api.mvc.Security$
第ä¸å¼æ°ã«èªè¨¼æ
å ±ãåå¾ãã¦Optionãè¿ãé¢æ°ãæå®ãã¾ããOptionãNoneã ã£ãå ´åã第äºå¼æ°ã«æå®ããé¢æ°ãå®è¡ãããOptionãSomeã ã£ãå ´åã第ä¸å¼æ°ã«æå®ããé¢æ°ãå®è¡ããã¾ãã
第ä¸å¼æ°ã®é¢æ°å
ã§ãã¢ã¯ã·ã§ã³ãä½ã£ã¦ã¾ãã
ãããªæãã®withAuthãç¨æãã¦ããã¦ãããããã¬ã¤ãã«å®è£
ãã¦ããã¨è²ããªã³ã³ããã¼ã©ã§ä½¿ãããã§ããã
Playã§ãServletã®ãã£ã«ã¿ã¼ã¿ãããªãã¨ãããæã¯ã¢ã¯ã·ã§ã³ã®åæã使ããã§ããã®ããªã»ã»ã»ã
ã»ãã·ã§ã³æ å ±ãæ¶ã
ã»ãã·ã§ã³æ å ±ãæ¶ãããå ´åã¯ãwithNewSessionã使ãã°ããã¿ããã§ãã
object Auth extends Controller { def delete = Action { implicit request => Redirect(routes.Application.login).withNewSession } }
ããã¾ã
ããã¥ã¡ã³ãã§ã¢ã¯ã·ã§ã³ã®åæã£ã¦è¦ãã¨ãã¯ä½ã®ãã¨ãã¨æã£ããã©ãåã¢ã¯ã·ã§ã³ã®å ±éå¦çãæ¸ãããã§ããã¼ã
Scalaéå¼ãã¬ã·ã (PROGRAMMERâS RECiPE)
- ä½è : 竹添ç´æ¨¹,島æ¬å¤å¯å
- åºç社/ã¡ã¼ã«ã¼: ç¿æ³³ç¤¾
- çºå£²æ¥: 2012/07/03
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 6人 ã¯ãªãã¯: 54å
- ãã®ååãå«ãããã° (13件) ãè¦ã