|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 HAT Data Exchange Ltd |
| 3 | + * SPDX-License-Identifier: AGPL-3.0 |
| 4 | + * |
| 5 | + * This file is part of the Hub of All Things project (HAT). |
| 6 | + * |
| 7 | + * HAT is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License |
| 9 | + * as published by the Free Software Foundation, version 3 of |
| 10 | + * the License. |
| 11 | + * |
| 12 | + * HAT is distributed in the hope that it will be useful, but |
| 13 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | + * the GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General |
| 18 | + * Public License along with this program. If not, see |
| 19 | + * <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + * Written by Andrius Aucinas <andrius.aucinas@hatdex.org> |
| 22 | + * 7 / 2018 |
| 23 | + */ |
| 24 | + |
| 25 | +package org.hatdex.hat.api.controllers |
| 26 | + |
| 27 | +import com.mohiva.play.silhouette.api.Silhouette |
| 28 | +import javax.inject.Inject |
| 29 | +import org.hatdex.hat.api.json.ApplicationJsonProtocol |
| 30 | +import org.hatdex.hat.api.models.applications.HatApplication |
| 31 | +import org.hatdex.hat.api.models.{ ApplicationManage, ErrorMessage, Owner } |
| 32 | +import org.hatdex.hat.api.service.RemoteExecutionContext |
| 33 | +import org.hatdex.hat.api.service.applications.ApplicationsService |
| 34 | +import org.hatdex.hat.authentication.{ ContainsApplicationRole, HatApiAuthEnvironment, HatApiController, WithRole } |
| 35 | +import play.api.Logger |
| 36 | +import play.api.http.HttpEntity |
| 37 | +import play.api.libs.json.Json |
| 38 | +import play.api.libs.ws.WSClient |
| 39 | +import play.api.mvc.{ Action, AnyContent, ControllerComponents } |
| 40 | + |
| 41 | +import scala.concurrent.Future |
| 42 | + |
| 43 | +class ApplicationRequestProxy @Inject() ( |
| 44 | + components: ControllerComponents, |
| 45 | + silhouette: Silhouette[HatApiAuthEnvironment], |
| 46 | + wsClient: WSClient)( |
| 47 | + implicit |
| 48 | + val ec: RemoteExecutionContext, |
| 49 | + applicationsService: ApplicationsService) |
| 50 | + extends HatApiController(components, silhouette) with ApplicationJsonProtocol { |
| 51 | + |
| 52 | + import org.hatdex.hat.api.json.HatJsonFormats.errorMessage |
| 53 | + |
| 54 | + val logger = Logger(this.getClass) |
| 55 | + |
| 56 | + def proxyRequest(id: String, path: String, method: String = "GET"): Action[AnyContent] = SecuredAction(ContainsApplicationRole(Owner(), ApplicationManage(id)) || WithRole(Owner())).async { implicit request => |
| 57 | + logger.info(s"Proxy $method request for $id to $path with parameters: ${request.queryString}") |
| 58 | + applicationsService.applicationStatus(id).flatMap { maybeStatus ⇒ |
| 59 | + maybeStatus map { |
| 60 | + case HatApplication(app, _, true, _, _, _) ⇒ |
| 61 | + |
| 62 | + applicationsService.applicationToken(request.identity, app) |
| 63 | + .flatMap { token ⇒ |
| 64 | + val baseRequest = wsClient.url(s"${app.kind.url}/$path") |
| 65 | + .withHttpHeaders("x-auth-token" → token.accessToken) |
| 66 | + .addQueryStringParameters(request.queryString.map(p ⇒ (p._1, p._2.head)).toSeq: _*) |
| 67 | + .withMethod(method) |
| 68 | + |
| 69 | + request.body.asJson.fold(baseRequest)(b ⇒ baseRequest.withBody(b)) |
| 70 | + .stream() |
| 71 | + .map(r ⇒ new Status(r.status).sendEntity(HttpEntity.Strict(r.bodyAsBytes, Some("application/json")))) |
| 72 | + } |
| 73 | + |
| 74 | + case _ ⇒ Future.successful(BadRequest(Json.toJson(ErrorMessage( |
| 75 | + "Application not active", |
| 76 | + s"Application $id does not appear to be activated by the user")))) |
| 77 | + } getOrElse { |
| 78 | + Future.successful(NotFound(Json.toJson(ErrorMessage( |
| 79 | + "Application not Found", |
| 80 | + s"Application $id does not appear to be a valid application registered with the DEX")))) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments