This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package services | ||
|
||
import play.api.Application | ||
import play.api.Play | ||
import play.api.http.{MimeTypes, HeaderNames} | ||
import play.api.libs.ws.WS | ||
import play.api.libs.ws._ | ||
import play.api.mvc.{Results, Action, Controller} | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class OAuth2(application: Application) { | ||
lazy val mitAuthId = application.configuration.getString("mit.client.id").get | ||
lazy val mitAuthSecret = application.configuration.getString("mit.client.secret").get | ||
|
||
def getAuthorizationUrl(redirectUri: String, scope: String, state: String): String = { | ||
val baseUrl = application.configuration.getString("mit.redirect.url").get | ||
baseUrl.format(mitAuthId, redirectUri, scope, state) | ||
} | ||
|
||
def getToken(code: String): Future[String] = { | ||
val tokenResponse = WS.url("https://oidc.mit.edu/token")(application). | ||
withQueryString("client_id" -> mitAuthId, | ||
"client_secret" -> mitAuthSecret, | ||
"code" -> code, | ||
"grant_type" -> "authorization_code", | ||
"redirect_uri" -> "http://localhost:9000/_oauth-callback"). | ||
withAuth(mitAuthId, mitAuthSecret, WSAuthScheme.BASIC). | ||
withHeaders(HeaderNames.ACCEPT -> MimeTypes.JSON). | ||
post(Results.EmptyContent()) | ||
|
||
tokenResponse.flatMap { response => | ||
(response.json \ "access_token").asOpt[String].fold(Future.failed[String](new IllegalStateException("Sod off!"))) { accessToken => | ||
Future.successful(accessToken) | ||
} | ||
} | ||
} | ||
} | ||
|
||
object OAuth2 extends Controller { | ||
lazy val oauth2 = new OAuth2(Play.current) | ||
|
||
def callback(codeOpt: Option[String] = None, stateOpt: Option[String] = None) = Action.async { implicit request => | ||
println(codeOpt) | ||
println(stateOpt) | ||
println(request.session.get("oauth-state")) | ||
println(request) | ||
(for { | ||
code <- codeOpt | ||
state <- stateOpt | ||
oauthState <- request.session.get("oauth-state") | ||
} yield { | ||
if (state == oauthState) { | ||
oauth2.getToken(code).map { accessToken => | ||
Redirect(services.routes.OAuth2.success()).withSession("oauth-token" -> accessToken) | ||
}.recover { | ||
case ex: IllegalStateException => Unauthorized(ex.getMessage) | ||
} | ||
} | ||
else { | ||
Future.successful(BadRequest("Invalid mit login")) | ||
} | ||
}).getOrElse(Future.successful(BadRequest("No parameters supplied"))) | ||
} | ||
|
||
def success() = Action.async { request => | ||
println("___oath token start___") | ||
println(request.session.get("oauth-token")) | ||
println("___oath token end___") | ||
implicit val app = Play.current | ||
request.session.get("oauth-token").fold(Future.successful(Unauthorized("No way Jose"))) { authToken => | ||
WS.url("https://oidc.mit.edu/userinfo"). | ||
withHeaders(HeaderNames.AUTHORIZATION -> ("Bearer " + s"$authToken")). | ||
get().map { response => | ||
Ok(response.json) | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@(message: String, redirectUrl: String) | ||
|
||
@layout.main("Login to SCOAP3 - TopicHub") { | ||
<h2>Not Yet Authenticated</h2> | ||
<a id="openid" href="@redirectUrl"><em><b>Log in with MITID!</b></em></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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import org.specs2.mutable._ | ||
import org.specs2.runner._ | ||
import org.junit.runner._ | ||
import org.specs2.mock._ | ||
|
||
import play.api.test._ | ||
import play.api.test.Helpers._ | ||
import org.fest.assertions.Assertions.assertThat | ||
|
||
/** | ||
* add your integration spec here. | ||
* An integration test will fire up a whole play application in a real (or headless) browser | ||
*/ | ||
class AuthenticationSpec extends Specification with Mockito { | ||
"Application" should { | ||
"display a login screen" in new WithBrowser { | ||
browser.goTo("http://localhost:" + port + "/login") | ||
browser.pageSource must contain("Log in with MITID") | ||
assertThat(browser.title()).isEqualTo("Login to SCOAP3 - TopicHub") | ||
} | ||
|
||
"redirect when login is clicked" in new WithBrowser { | ||
browser.goTo("http://localhost:" + port + "/login") | ||
browser.$("#openid").click(); | ||
assertThat(browser.title()).isEqualTo("MIT OpenID Connect Pilot - Log In") | ||
} | ||
} | ||
} |