Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluator Server Update #62

Merged
merged 6 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
- docker
scala:
- 2.11.11
- 2.12.3
- 2.12.10
jdk:
- oraclejdk8
cache:
Expand All @@ -30,7 +30,7 @@ after_success:
sbt ++$TRAVIS_SCALA_VERSION dockerBuildAndPush;
sbt ++$TRAVIS_SCALA_VERSION smoketests/test;
fi
- if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" -a "$TRAVIS_SCALA_VERSION" = "2.12.3" ]; then
- if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" -a "$TRAVIS_SCALA_VERSION" = "2.12.10" ]; then
sbt ++$TRAVIS_SCALA_VERSION publishSignedAll;
echo "Deploying to Heroku";
docker login --username=noel.m@47deg.com --password=$heroku_token registry.heroku.com;
Expand Down
14 changes: 8 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
lazy val root = (project in file("."))
.settings(mainClass in Universal := Some("org.scalaexercises.evaluator.EvaluatorServer"))
.settings(stage := (stage in Universal in `evaluator-server`).value)
.settings(noPublishSettings: _*)
.aggregate(`evaluator-server`, `evaluator-client`)

lazy val `evaluator-shared` = (project in file("shared"))
.enablePlugins(AutomateHeaderPlugin)
.settings(name := "evaluator-shared")

lazy val `evaluator-client` = (project in file("client"))
.dependsOn(`evaluator-shared`)
.enablePlugins(AutomateHeaderPlugin)
.settings(
name := "evaluator-client",
clientDependencies
)

lazy val `evaluator-server` = (project in file("server"))
.dependsOn(`evaluator-shared`)
.enablePlugins(JavaAppPackaging)
.enablePlugins(AutomateHeaderPlugin)
.enablePlugins(sbtdocker.DockerPlugin)
Expand All @@ -40,6 +36,12 @@ lazy val `smoketests` = (project in file("smoketests"))
)
.settings(buildInfoSettings: _*)

lazy val root = (project in file("."))
.settings(mainClass in Universal := Some("org.scalaexercises.evaluator.EvaluatorServer"))
.settings(stage := (stage in Universal in `evaluator-server`).value)
.settings(noPublishSettings: _*)
.aggregate(`evaluator-server`, `evaluator-client`, `evaluator-shared`, `smoketests`)

addCommandAlias(
"publishSignedAll",
";evaluator-sharedJS/publishSigned;evaluator-sharedJVM/publishSigned;evaluator-clientJS/publishSigned;evaluator-clientJVM/publishSigned"
Expand Down
25 changes: 14 additions & 11 deletions project/ProjectPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object ProjectPlugin extends AutoPlugin {
lazy val roshttp = "2.2.4"
lazy val slf4jSimple = "1.7.28"
lazy val jwtCore = "4.0.0"
lazy val coursier = "2.0.0-RC3-4"
}

lazy val dockerSettings = Seq(
Expand Down Expand Up @@ -81,9 +82,12 @@ object ProjectPlugin extends AutoPlugin {
%%("http4s-circe", V.http4s),
%("config"),
%%("jwt-core", V.jwtCore),
%%("coursier", V.coursier),
%%("coursier-cache", V.coursier),
"io.get-coursier" %% "coursier-cats-interop" % V.coursier,
%%("scalatest", V.scalatest) % "test"
),
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "2.0.0-RC3-2")
) //,
//addSbtPlugin("io.get-coursier" % "sbt-coursier" % V.coursier)
kiroco12 marked this conversation as resolved.
Show resolved Hide resolved
)

lazy val buildInfoSettings = Seq(
Expand Down Expand Up @@ -135,21 +139,20 @@ object ProjectPlugin extends AutoPlugin {
organizationEmail = "hello@47deg.com"
),
orgLicenseSetting := ApacheLicense,
scalaVersion := "2.12.10",
scalaVersion := "2.11.11",
scalaOrganization := "org.scala-lang",
javacOptions ++= Seq("-encoding", "UTF-8", "-Xlint:-options"),
fork in Test := false,
parallelExecution in Test := false,
cancelable in Global := true,
headerLicense := Some(
HeaderLicense.Custom(
s"""|/*
| * scala-exercises - ${name.value}
| * Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
| */
|
|""".stripMargin
)),
headerMappings := headerMappings.value + (HeaderFileType.scala -> HeaderCommentStyle.CStyleBlockComment)
s"""|
| scala-exercises - ${name.value}
| Copyright (C) 2015-2019 47 Degrees, LLC. <http://www.47deg.com>
|
|""".stripMargin
))
//headerMappings := headerMappings.value + (HeaderFileType.scala -> HeaderCommentStyle.CStyleBlockComment)
) ++ shellPromptSettings
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.7
sbt.version=1.2.8
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.4.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.5.0")
addSbtPlugin("com.47deg" % "sbt-org-policies" % "0.12.0-M1")
addSbtPlugin("com.47deg" % "sbt-org-policies" % "0.12.0-M2")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "3.0.1")
55 changes: 31 additions & 24 deletions server/src/main/scala/org/scalaexercises/evaluator/auth.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/*
* scala-exercises - evaluator-server
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*
* scala-exercises - evaluator-server
* Copyright (C) 2015-2019 47 Degrees, LLC. <http://www.47deg.com>
*
*/

package org.scalaexercises.evaluator

import org.http4s._, org.http4s.dsl._, org.http4s.server._
import cats.effect.Sync
import com.typesafe.config._
import org.http4s._
import org.http4s.syntax.kleisli.http4sKleisliResponseSyntax
import org.http4s.util._
import scala.util.{Failure, Success, Try}
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim, JwtHeader, JwtOptions}

import org.log4s.getLogger
import pdi.jwt.{Jwt, JwtAlgorithm}

import scalaz.concurrent.Task
import scala.util.{Failure, Success}

object auth {

Expand All @@ -37,15 +39,14 @@ object auth {

type HeaderT = `X-Scala-Eval-Api-Token`

def name: CaseInsensitiveString = "x-scala-eval-api-token".ci
def name: CaseInsensitiveString = CaseInsensitiveString("x-scala-eval-api-token")

override def parse(s: String): ParseResult[`X-Scala-Eval-Api-Token`] =
ParseResult.success(`X-Scala-Eval-Api-Token`(s))

def matchHeader(header: Header): Option[HeaderT] = {
def matchHeader(header: Header): Option[HeaderT] =
if (header.name == name) Some(`X-Scala-Eval-Api-Token`(header.value))
else None
}

}

Expand All @@ -55,20 +56,26 @@ object auth {
writer.append(token)
}

def apply(service: HttpService): HttpService = Service.lift { req =>
req.headers.get(`X-Scala-Eval-Api-Token`) match {
case Some(header) =>
Jwt.decodeRaw(header.value, secretKey, Seq(JwtAlgorithm.HS256)) match {
case Success(tokenIdentity) =>
logger.info(s"Auth success with identity : $tokenIdentity")
service(req)
case Failure(ex) =>
logger.warn(s"Auth failed : $ex")
Task.now(Response(Status.Unauthorized))
def apply[F[_]: Sync](service: HttpApp[F]): HttpApp[F] =
HttpRoutes
.of[F] {
case req if req.headers.nonEmpty => {
req.headers.get(`X-Scala-Eval-Api-Token`) match {
case Some(header) =>
Jwt.decodeRaw(header.token, secretKey, Seq(JwtAlgorithm.HS256)) match {
case Success(tokenIdentity) => {
logger.info(s"Auth success with identity : $tokenIdentity")
service(req)
}
case Failure(ex) => {
logger.warn(s"Auth failed : $ex")
Sync[F].pure(Response(Status.Unauthorized))
}
}
}
}
case None => Task.now(Response(Status.Unauthorized))
}

}
case _ => Sync[F].pure(Response(Status.Unauthorized))
}
.orNotFound

}
37 changes: 10 additions & 27 deletions server/src/main/scala/org/scalaexercises/evaluator/codecs.scala
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
/*
* scala-exercises - evaluator-server
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*
* scala-exercises - evaluator-server
* Copyright (C) 2015-2019 47 Degrees, LLC. <http://www.47deg.com>
*
*/

package org.scalaexercises.evaluator

import org.http4s._, org.http4s.dsl._
import io.circe.{Decoder, Encoder, Json, Printer}
import org.http4s.headers.`Content-Type`
import io.circe.jawn.CirceSupportParser.facade
import cats.effect.Sync
import io.circe.{Decoder, Encoder}
import org.http4s._
import org.http4s.circe._

/** Provides Json serialization codecs for the http4s services */
trait Http4sCodecInstances {

implicit val jsonDecoder: EntityDecoder[Json] = jawn.jawnDecoder(facade)
implicit def entityDecoderOf[F[_]: Sync, A: Decoder]: EntityDecoder[F, A] = jsonOf[F, A]

implicit def jsonDecoderOf[A](implicit decoder: Decoder[A]): EntityDecoder[A] =
jsonDecoder.flatMapR { json =>
decoder
.decodeJson(json)
.fold(
failure =>
DecodeResult.failure(
InvalidMessageBodyFailure(s"Could not decode JSON: $json", Some(failure))),
DecodeResult.success(_)
)
}

implicit val jsonEntityEncoder: EntityEncoder[Json] = EntityEncoder[String]
.contramap[Json] { json =>
Printer.noSpaces.pretty(json)
}
.withContentType(`Content-Type`(MediaType.`application/json`))

implicit def jsonEncoderOf[A](implicit encoder: Encoder[A]): EntityEncoder[A] =
jsonEntityEncoder.contramap[A](encoder.apply)
implicit def entityEncoderOf[F[_]: Sync, A: Encoder]: EntityEncoder[F, A] = jsonEncoderOf[F, A]

}

Expand Down
Loading