-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API-1888 adding authFilter to verify confidence level
- Loading branch information
1 parent
f2b5d3c
commit 65aa619
Showing
13 changed files
with
540 additions
and
26 deletions.
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
48 changes: 48 additions & 0 deletions
48
app/uk/gov/hmrc/openidconnect/userinfo/connectors/AuthConnector.scala
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,48 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.openidconnect.userinfo.connectors | ||
|
||
import play.api.Logger | ||
import uk.gov.hmrc.openidconnect.userinfo.config.WSHttp | ||
import uk.gov.hmrc.play.auth.microservice.connectors.ConfidenceLevel._ | ||
import uk.gov.hmrc.play.config.ServicesConfig | ||
import uk.gov.hmrc.play.http.{HeaderCarrier, HttpGet} | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
|
||
trait AuthConnector extends uk.gov.hmrc.play.auth.microservice.connectors.AuthConnector { | ||
val authBaseUrl: String | ||
val http: HttpGet | ||
|
||
def confidenceLevel()(implicit hc: HeaderCarrier): Future[Option[Int]] = { | ||
http.GET(s"$authBaseUrl/auth/authority") map { | ||
resp => | ||
val cf = (resp.json \ "confidenceLevel").as[Int] | ||
Some(cf) | ||
} recover { | ||
case e: Throwable => | ||
Logger.error("failed to retrieve auth confidenceLevel", e) | ||
None | ||
} | ||
} | ||
} | ||
|
||
object AuthConnector extends AuthConnector with ServicesConfig { | ||
override lazy val authBaseUrl = baseUrl("auth") | ||
lazy val http = WSHttp | ||
} |
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
58 changes: 58 additions & 0 deletions
58
app/uk/gov/hmrc/openidconnect/userinfo/filters/MicroserviceAuthFilter.scala
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,58 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.openidconnect.userinfo.filters | ||
|
||
import play.api.Routes | ||
import play.api.mvc.{Filter, RequestHeader, Result, Results} | ||
import uk.gov.hmrc.openidconnect.userinfo.config.{AuthParamsControllerConfiguration, ControllerConfiguration} | ||
import uk.gov.hmrc.openidconnect.userinfo.services.AuthService | ||
import uk.gov.hmrc.play.auth.controllers.{AuthConfig, AuthParamsControllerConfig} | ||
import uk.gov.hmrc.play.http.HeaderCarrier | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
|
||
trait MicroserviceAuthFilter extends Filter { | ||
def apply(next: (RequestHeader) => Future[Result])(rh: RequestHeader): Future[Result] = { | ||
implicit val hc = HeaderCarrier.fromHeadersAndSession(rh.headers) | ||
|
||
def authConfig(rh: RequestHeader): Option[AuthConfig] = { | ||
rh.tags.get(Routes.ROUTE_CONTROLLER).flatMap { name => | ||
if (controllerNeedsAuth(name)) Some(authParamsConfig.authConfig(name)) | ||
else None | ||
} | ||
} | ||
|
||
authConfig(rh) match { | ||
case Some(authConfig) => authService.isAuthorised().flatMap { | ||
case true => next(rh) | ||
case _ => Future.successful(Results.Unauthorized) | ||
} | ||
case _ => next(rh) | ||
} | ||
} | ||
|
||
val authService: AuthService | ||
val authParamsConfig: AuthParamsControllerConfig | ||
|
||
def controllerNeedsAuth(controllerName: String): Boolean = ControllerConfiguration.paramsForController(controllerName).needsAuth | ||
} | ||
|
||
object MicroserviceAuthFilter extends MicroserviceAuthFilter { | ||
override lazy val authService = AuthService | ||
override lazy val authParamsConfig = AuthParamsControllerConfiguration | ||
} |
39 changes: 39 additions & 0 deletions
39
app/uk/gov/hmrc/openidconnect/userinfo/services/AuthService.scala
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,39 @@ | ||
/* | ||
* Copyright 2016 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.openidconnect.userinfo.services | ||
|
||
import uk.gov.hmrc.openidconnect.userinfo.connectors.AuthConnector | ||
import uk.gov.hmrc.play.auth.microservice.connectors.ConfidenceLevel.L200 | ||
import uk.gov.hmrc.play.http.HeaderCarrier | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
trait AuthService { | ||
val authConnector: AuthConnector | ||
|
||
def isAuthorised()(implicit hc: HeaderCarrier) = { | ||
authConnector.confidenceLevel().map { result => | ||
result match { | ||
case Some(cf) => cf >= L200.level | ||
case None => false | ||
} | ||
} | ||
} | ||
} | ||
|
||
object AuthService extends AuthService { | ||
override val authConnector = AuthConnector | ||
} |
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
Oops, something went wrong.