Skip to content

Commit

Permalink
add a permission check for preview and admin (using the `preview_…
Browse files Browse the repository at this point in the history
…access` and `admin_tool_access` permissions added in guardian/permissions#184)
  • Loading branch information
twrichards committed Apr 29, 2024
1 parent 226bc94 commit 3701dbc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion admin/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import dfp._
import common.dfp._
import common._
import conf.switches.SwitchboardLifecycle
import conf.CachedHealthCheckLifeCycle
import controllers.{AdminControllers, HealthCheck}
import _root_.dfp.DfpDataCacheLifecycle
import com.amazonaws.regions.Regions
Expand Down Expand Up @@ -106,6 +105,7 @@ trait AppComponents extends FrontendComponents with AdminControllers with AdminS
// in [admin].
"/interactive-librarian/",
),
requiredEditorialPermissionName = "admin_tool_access",
)

lazy val healthCheck = wire[HealthCheck]
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ val common = library("common")
jSoup,
json4s,
panDomainAuth,
editorialPermissions,
quartzScheduler,
redisClient,
rome,
Expand Down
26 changes: 23 additions & 3 deletions common/app/http/GuardianAuthWithExemptions.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package http

import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.AmazonS3
import com.gu.pandomainauth.action.AuthActions
import com.gu.pandomainauth.model.AuthenticatedUser
import com.gu.pandomainauth.{PanDomain, PanDomainAuthSettingsRefresher}
import com.gu.permissions.{PermissionDefinition, PermissionsConfig, PermissionsProvider}
import common.Environment.stage
import conf.Configuration.aws.mandatoryCredentials
import model.ApplicationContext
import org.apache.pekko.stream.Materializer
import play.api.Mode
import play.api.libs.ws.WSClient
import play.api.mvc.{BaseController, _}
import play.api.mvc._

import java.net.URL
import scala.concurrent.Future
Expand All @@ -22,6 +25,7 @@ class GuardianAuthWithExemptions(
s3Client: AmazonS3,
system: String,
extraDoNotAuthenticatePathPrefixes: Seq[String],
requiredEditorialPermissionName: String,
)(implicit
val mat: Materializer,
context: ApplicationContext,
Expand All @@ -30,6 +34,19 @@ class GuardianAuthWithExemptions(

private val outer = this

private val permissions: PermissionsProvider = PermissionsProvider(
PermissionsConfig(
stage = if (stage == "PROD") "PROD" else "CODE",
region = Regions.EU_WEST_1.getName,
awsCredentials = mandatoryCredentials,
),
)

private val requiredPermission = PermissionDefinition(
name = requiredEditorialPermissionName,
app = "frontend",
)

private def toolsDomainSuffix =
stage match {
case "PROD" => "gutools.co.uk"
Expand Down Expand Up @@ -81,9 +98,12 @@ class GuardianAuthWithExemptions(
if (doNotAuthenticate(request)) {
nextFilter(request)
} else {
// TODO: in future PR add a permission check here based on user, likely via a function passed in to GuardianAuthWithExemptions
AuthAction.authenticateRequest(request) { user =>
nextFilter(request)
if (permissions.hasPermission(requiredPermission, user.email)) {
nextFilter(request)
} else {
??? //FIXME serve 4XX (with explanation)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions preview/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ trait AppComponents
s3Client,
system = "preview",
extraDoNotAuthenticatePathPrefixes = healthCheck.healthChecks.map(_.path),
requiredEditorialPermissionName = "preview_access",
)

override lazy val capiHttpClient: HttpClient = new CapiHttpClient(wsClient) {
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ object Dependencies {
val mockito = "org.mockito" % "mockito-all" % "1.10.19" % Test
val paClient = "com.gu" %% "pa-client" % "7.0.7"
val panDomainAuth = "com.gu" %% "pan-domain-auth-play_3-0" % "3.1.0"
val editorialPermissions = "com.gu" %% "editorial-permissions-client" % "2.15"
val quartzScheduler = "org.quartz-scheduler" % "quartz" % "2.3.2"
val redisClient = "net.debasishg" %% "redisclient" % "3.42"
val rome = "rome" % "rome" % romeVersion
Expand Down

0 comments on commit 3701dbc

Please sign in to comment.