Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 1 addition & 4 deletions admin/src/main/scala/com/neu/api/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ trait Api extends Directives with CoreActors with Core {

private val managerPathPrefix: Option[String] =
sys.env.get("PATH_PREFIX").map(_.trim).filter(_.nonEmpty)
private final val timeOutStatus = "Status: 408"
private final val authenticationFailedStatus = "Status: 401"
private final val serverErrorStatus = "Status: 503"

implicit def exceptionHandler: ExceptionHandler =
ExceptionHandler {
Expand All @@ -57,7 +54,7 @@ trait Api extends Directives with CoreActors with Core {
)
case e: Exception =>
val (status, message) =
handleError(timeOutStatus, authenticationFailedStatus, serverErrorStatus, e)
handleError(e)
complete(
HttpResponse(
status = status,
Expand Down
11 changes: 8 additions & 3 deletions admin/src/main/scala/com/neu/client/RestClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,24 @@ class RestClient()(using
}

def handleError(
timeOutStatus: String,
authenticationFailedStatus: String,
serverErrorStatus: String,
e: Throwable
): (StatusCode, String) = {
val PERMISSION_DENIED = "Permission denied"
val sw = new StringWriter

val timeOutStatus = "Status: 408"
val authenticationFailedStatus = "Status: 401"
val authorizationFailedStatus = "Status: 403"
val serverErrorStatus = "Status: 503"

e.printStackTrace(new PrintWriter(sw))
logger.warn(sw.toString)
if (e.getMessage.contains(timeOutStatus)) {
(StatusCodes.RequestTimeout, "Session expired!")
} else if (e.getMessage.contains(authenticationFailedStatus)) {
(StatusCodes.Unauthorized, "Authentication failed!")
} else if (e.getMessage.contains(authorizationFailedStatus)) {
(StatusCodes.Forbidden, "No role assigned!")
} else if (e.getMessage.contains(serverErrorStatus)) {
(StatusCodes.ServiceUnavailable, "Server is not available!")
} else if (e.getMessage.contains(PERMISSION_DENIED)) {
Expand Down
22 changes: 14 additions & 8 deletions admin/src/main/scala/com/neu/service/BaseService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ open class BaseService extends Directives with LazyLogging {
}

protected def onUnauthorized(e: HttpResponseException, res: String): StandardRoute =
if (res.contains("\"code\":14")) {
complete((StatusCodes.BadRequest, res))
} else if (res.contains("\"code\":47")) {
complete((StatusCodes.Unauthorized, blocked))
} else if (res.contains("\"code\":48")) {
complete((StatusCodes.Unauthorized, passwordExpired))
} else if (res.contains("\"code\":50")) {
complete(StatusCodes.Unauthorized, authSSODisabledError)
if (e.getMessage.contains("Status: 401")) {
if (res.contains("\"code\":14")) {
complete((StatusCodes.BadRequest, res))
} else if (res.contains("\"code\":47")) {
complete((StatusCodes.Unauthorized, blocked))
} else if (res.contains("\"code\":48")) {
complete((StatusCodes.Unauthorized, passwordExpired))
} else if (res.contains("\"code\":50")) {
complete(StatusCodes.Unauthorized, authSSODisabledError)
} else {
complete((StatusCodes.Unauthorized, authError))
}
} else if (e.getMessage.contains("Status: 403")) {
complete((StatusCodes.Forbidden, "No role assigned!"))
} else {
complete((StatusCodes.Unauthorized, authError))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class OpenIdAuthService()(implicit
ip: String,
host: Option[String],
nonce: String
): Future[Route] = Future {
): Future[Route] = {
logger.info(s"openId-g: state is ${state.get}.")
logger.info(s"openId-g: code is ${code.getOrElse("no code")}")
logger.info(s"openId-g: host is ${host.getOrElse("no host")}")
Expand All @@ -134,18 +134,35 @@ class OpenIdAuthService()(implicit
logger.info("openId-g: OpenId Login. ")

response.status match {
case StatusCodes.OK =>
case StatusCodes.OK =>
val authTokenFuture: Future[String] = Unmarshal(response.entity).to[String]
val authToken = Await.result(authTokenFuture, RestClient.waitingLimit.seconds)
val userToken: UserTokenNew = AuthenticationManager.parseToken(authToken)
logger.info("openId-g: added authToken")
AuthenticationManager.putSsoToken(nonce, userToken)
redirect(ROOT_PATH, StatusCodes.Found)
case _ =>
logger.warn("openId-g: invalid response. redirect /")
deleteCookie("temp") {
redirect(ROOT_PATH, StatusCodes.MovedPermanently)
Future.successful(redirect(ROOT_PATH, StatusCodes.Found))
case StatusCodes.TemporaryRedirect =>
response.headers.find(_.is("location")) match {
case Some(location) =>
logger.info(s"openId-g: temporary redirect to ${location.value}")
val locationParts = location.value.split("#", 2)
val baseUri = Uri(locationParts(0))
val redirectUri =
if (locationParts.length > 1) baseUri.withFragment(locationParts(1))
else baseUri
Future.successful(
redirect(redirectUri, StatusCodes.SeeOther)
)
case None =>
Future.successful(complete(StatusCodes.BadGateway))
}
case _ =>
logger.warn("openId-g: invalid response. redirect /")
Future.successful(
deleteCookie("temp") {
redirect(ROOT_PATH, StatusCodes.MovedPermanently)
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,28 @@ class SamlAuthService()(implicit
private def handleAuthResponse(response: HttpResponse, nonce: String): Future[Route] = {
logger.info("saml-p: processing auth response.")
response.status match {
case StatusCodes.OK =>
logger.info(s"saml-p: success, storing token under nonce. redirecting to $rootPath")
case StatusCodes.OK =>
Unmarshal(response.entity).to[String].map { authToken =>
val userToken: UserTokenNew = AuthenticationManager.parseToken(authToken)
AuthenticationManager.putSsoToken(nonce, userToken)
redirect(rootPath, StatusCodes.Found)
}
case _ =>
case StatusCodes.TemporaryRedirect =>
response.headers.find(_.is("location")) match {
case Some(location) =>
logger.info(s"saml-p: temporary redirect to ${location.value}")
val locationParts = location.value.split("#", 2)
val baseUri = Uri(locationParts(0))
val redirectUri =
if (locationParts.length > 1) baseUri.withFragment(locationParts(1))
else baseUri
Future.successful(
redirect(redirectUri, StatusCodes.SeeOther)
)
case None =>
Future.successful(complete(StatusCodes.BadGateway))
}
case _ =>
logger.warn(s"saml-p: ${response.status}. SAML login error. redirect to $rootPath ")
// Clear the temp cookie so the browser does not retain a stale nonce
Future.successful(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class DashboardService()(implicit executionContext: ExecutionContext)
private final val DENY = "deny"
private final val DASHBOARD = "dashboard"

private final val timeOutStatus = "Status: 408"
private final val authenticationFailedStatus = "Status: 401"
private final val serverErrorStatus = "Status: 503"

def getMultiClusterSummary(tokenId: String, clusterId: Option[String]): Route = complete {
try {
val baseUrl =
Expand Down Expand Up @@ -118,12 +114,7 @@ class DashboardService()(implicit executionContext: ExecutionContext)
)
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand Down Expand Up @@ -422,12 +413,7 @@ class DashboardService()(implicit executionContext: ExecutionContext)
dashboardScoreDTO
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand Down Expand Up @@ -546,12 +532,7 @@ class DashboardService()(implicit executionContext: ExecutionContext)
dashboardNotificationDTO
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand Down
70 changes: 11 additions & 59 deletions admin/src/main/scala/com/neu/service/device/DeviceService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ import scala.util.control.NonFatal

class DeviceService extends Directives with DefaultJsonFormats with LazyLogging {

final val timeOutStatus = "Status: 408"
final val authenticationFailedStatus = "Status: 401"
final val serverErrorStatus = "Status: 503"
private final val benchHostPath = "bench/host"
private var logFile = "/tmp/debug.gz"
private final val benchHostPath = "bench/host"
private var logFile = "/tmp/debug.gz"

def getEnforcers(tokenId: String, id: Option[String]): Route = complete {
if (id.isEmpty) {
Expand Down Expand Up @@ -411,12 +408,7 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
}
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand All @@ -442,12 +434,7 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
)
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand All @@ -472,12 +459,7 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
)
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand All @@ -501,12 +483,7 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
)
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand Down Expand Up @@ -537,12 +514,7 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
}
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand Down Expand Up @@ -617,28 +589,13 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
)
}
case Right(false) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
new RuntimeException("Status: 401")
)
RestClient.handleError(new RuntimeException("Status: 401"))
case Left(error) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
error
)
RestClient.handleError(error)
}
catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand All @@ -661,12 +618,7 @@ class DeviceService extends Directives with DefaultJsonFormats with LazyLogging
}
} catch {
case NonFatal(e) =>
RestClient.handleError(
timeOutStatus,
authenticationFailedStatus,
serverErrorStatus,
e
)
RestClient.handleError(e)
}
}

Expand Down
Loading
Loading