Skip to content

Commit

Permalink
Supported optional parameters for login
Browse files Browse the repository at this point in the history
see: t2v#175
  • Loading branch information
toshi committed Jul 3, 2016
1 parent 8a17f74 commit 9f9d12e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions social-sample/app/views/index.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h2>GitHub</h2>
<img src="@{u.avatarUrl}">
</p>
}.getOrElse {
<a href="@routes.GitHubAuthController.link("user")">link GitHub</a>
<a href="@{routes.GitHubAuthController.link() + "?scope=user"}>link GitHub</a>
}
</div>

Expand All @@ -69,7 +69,7 @@ <h2>Facebook</h2>
<img src="@{u.coverUrl}">
</p>
}.getOrElse {
<a href="@routes.FacebookAuthController.link("email")">link Facebook</a>
<a href="@{routes.FacebookAuthController.link() + "?scope=email"}">link Facebook</a>
}
</div>

Expand All @@ -94,7 +94,7 @@ <h2>Slack</h2>
@u.accessToken
</p>
}.getOrElse {
<a href="@routes.SlackAuthController.link("read")">link Slack</a>
<a href="@{routes.SlackAuthController.link() + "?scope=read"}">link Slack</a>
}
</div>

Expand All @@ -105,10 +105,10 @@ <h2>Slack</h2>
<a href="@routes.TwitterAuthController.login">twitter login</a>
</p>
<p>
<a href="@routes.GitHubAuthController.login("user")">github login</a>
<a href="@{routes.GitHubAuthController.login() + "?scope=user"}">github login</a>
</p>
<p>
<a href="@routes.FacebookAuthController.login("email")">facebook login</a>
<a href="@{routes.FacebookAuthController.login() + "?scope=email"}">facebook login</a>
</p>
}
</div>
Expand Down
10 changes: 5 additions & 5 deletions social-sample/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ GET /login/twitter controllers.TwitterAuthController.login
GET /link/twitter controllers.TwitterAuthController.link
GET /authorize/twitter controllers.TwitterAuthController.authorize

GET /login/github controllers.GitHubAuthController.login(scope: String)
GET /link/github controllers.GitHubAuthController.link(scope: String)
GET /login/github controllers.GitHubAuthController.login()
GET /link/github controllers.GitHubAuthController.link()
GET /authorize/github controllers.GitHubAuthController.authorize

GET /login/facebook controllers.FacebookAuthController.login(scope: String)
GET /link/facebook controllers.FacebookAuthController.link(scope: String)
GET /login/facebook controllers.FacebookAuthController.login()
GET /link/facebook controllers.FacebookAuthController.link()
GET /authorize/facebook controllers.FacebookAuthController.authorize

GET /link/slack controllers.SlackAuthController.link(scope: String)
GET /link/slack controllers.SlackAuthController.link()
GET /authorize/slack controllers.SlackAuthController.authorize
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jp.t2v.lab.play2.auth.social.core

import play.api.libs.ws.WSResponse
import play.api.mvc.{ AnyContent, Request }

import scala.concurrent.{ ExecutionContext, Future }

Expand All @@ -20,7 +21,7 @@ trait OAuth2Authenticator extends OAuthAuthenticator {

def retrieveAccessToken(code: String)(implicit ctx: ExecutionContext): Future[AccessToken]

def getAuthorizationUrl(scope: String, state: String): String
def getAuthorizationUrl(request: Request[AnyContent], state: String): String

def parseAccessTokenResponse(response: WSResponse): String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ trait OAuth2Controller extends Controller with OAuthController { self: OptionalA

protected val OAuth2StateKey = "play.auth.social.oauth2.state"

// TODO scope is optional in some services
// TODO some services have more optional parameter
def login(scope: String) = AsyncStack(ExecutionContextKey -> OAuthExecutionContext) { implicit request =>
def login = AsyncStack(ExecutionContextKey -> OAuthExecutionContext) { implicit request =>
implicit val ec = StackActionExecutionContext
loggedIn match {
case Some(u) =>
Expand All @@ -28,21 +26,19 @@ trait OAuth2Controller extends Controller with OAuthController { self: OptionalA
// should be more random ?
val state = UUID.randomUUID().toString
Future.successful(
Redirect(authenticator.getAuthorizationUrl(scope, state)).withSession(
Redirect(authenticator.getAuthorizationUrl(request, state)).withSession(
request.session + (OAuth2StateKey -> state)
)
)
}
}

// TODO scope is optional in some services
// TODO some services have more optional parameter
def link(scope: String) = StackAction(ExecutionContextKey -> OAuthExecutionContext) { implicit request =>
def link = StackAction(ExecutionContextKey -> OAuthExecutionContext) { implicit request =>
loggedIn match {
case Some(u) =>
// TODO should it be more random ?
val state = UUID.randomUUID().toString
Redirect(authenticator.getAuthorizationUrl(scope, state)).withSession(
Redirect(authenticator.getAuthorizationUrl(request, state)).withSession(
request.session + (OAuth2StateKey -> state)
)
case None =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import play.api.Logger
import play.api.Play.current
import play.api.http.{ HeaderNames, MimeTypes }
import play.api.libs.ws.{ WS, WSResponse }
import play.api.mvc.Results
import play.api.mvc.{ AnyContent, Request, Results }

import scala.concurrent.{ ExecutionContext, Future }
import scala.util.control.NonFatal
Expand Down Expand Up @@ -43,7 +43,8 @@ class FacebookAuthenticator extends OAuth2Authenticator {
}
}

def getAuthorizationUrl(scope: String, state: String): String = {
def getAuthorizationUrl(request: Request[AnyContent], state: String): String = {
val scope = request.getQueryString("scope").getOrElse("")
val encodedClientId = URLEncoder.encode(clientId, "utf-8")
val encodedRedirectUri = URLEncoder.encode(callbackUrl, "utf-8")
val encodedScope = URLEncoder.encode(scope, "utf-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import play.api.Logger
import play.api.Play.current
import play.api.http.{ HeaderNames, MimeTypes }
import play.api.libs.ws.{ WS, WSResponse }
import play.api.mvc.Results
import play.api.mvc.{ AnyContent, Request, Results }

import scala.concurrent.{ ExecutionContext, Future }
import scala.util.control.NonFatal
Expand Down Expand Up @@ -42,7 +42,8 @@ class GitHubAuthenticator extends OAuth2Authenticator {
}
}

def getAuthorizationUrl(scope: String, state: String): String = {
def getAuthorizationUrl(request: Request[AnyContent], state: String): String = {
val scope = request.getQueryString("scope").getOrElse("")
val encodedClientId = URLEncoder.encode(clientId, "utf-8")
val encodedRedirectUri = URLEncoder.encode(callbackUrl, "utf-8")
val encodedScope = URLEncoder.encode(scope, "utf-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import play.api.Logger
import play.api.http.{ HeaderNames, MimeTypes }
import play.api.libs.ws.{ WS, WSResponse }
import play.api.Play.current
import play.api.mvc.Results
import play.api.mvc.{ AnyContent, Request, Results }

import scala.concurrent.{ ExecutionContext, Future }
import scala.util.control.NonFatal
Expand All @@ -28,7 +28,8 @@ class SlackAuthenticator extends OAuth2Authenticator {

override val callbackUrl: String = current.configuration.getString("slack.callbackURL").getOrElse("slack.callbackURL is missing")

def getAuthorizationUrl(scope: String, state: String): String = {
def getAuthorizationUrl(request: Request[AnyContent], state: String): String = {
val scope = request.getQueryString("scope").getOrElse("")
val encodedClientId = URLEncoder.encode(clientId, "utf-8")
val encodedRedirectUri = URLEncoder.encode(callbackUrl, "utf-8")
val encodedScope = URLEncoder.encode(scope, "utf-8")
Expand Down

0 comments on commit 9f9d12e

Please sign in to comment.