Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
434a16f
Implementation of Authorization provider
Ayybeeshafi Jan 24, 2019
60daff2
Implementation of Authorization for Delphi Management refs #105
Ayybeeshafi Jan 30, 2019
c0a56e1
Implementation of authenticate on InstanceRegistryController refs #105
Ayybeeshafi Jan 31, 2019
4b7f532
Authentication of Delphi-Management refs #105
Ayybeeshafi Jan 31, 2019
5690662
Authentication of Delphi Management
Ayybeeshafi Feb 3, 2019
dc7afc1
Code commenting and formmating refs #105
Ayybeeshafi Feb 3, 2019
b3f8dc4
Merge branch 'develop' into feature/Authorization
janniclas Feb 4, 2019
4d9ecbe
Merge branch 'develop' into feature/Authorization
janniclas Feb 4, 2019
6250f0e
Fix of ambigious implicit values
Ayybeeshafi Feb 4, 2019
04d7fbf
Merge branch 'feature/Authorization' of https://github.com/delphi-hub…
Ayybeeshafi Feb 4, 2019
db5753e
Basic Authentication of Components with IR is now working refs #105
Ayybeeshafi Feb 7, 2019
544113e
Adapting Delphi Management to new API. Adding Instance now works refs…
Ayybeeshafi Feb 7, 2019
d5de64e
Adating Delphi Management methods according to the new API refs #113
Ayybeeshafi Feb 8, 2019
d37dde2
Adatpting to new API. refs #113
Ayybeeshafi Feb 8, 2019
bf97647
updated api endpoint for getting the whole instance network
janniclas Feb 10, 2019
742faed
removed unneccessary user name passwort config
janniclas Feb 10, 2019
15003f6
corrected new api path for post instance
janniclas Feb 10, 2019
8919499
updated the handle action endpoint to new api
janniclas Feb 11, 2019
f668a3b
Now token expires after 30 minutes and Strings are loaded from Config…
Ayybeeshafi Feb 11, 2019
8c786ea
Scheduling of Generation of Tokens refs #105
Ayybeeshafi Feb 12, 2019
a02df69
Generation of token at each method call
Ayybeeshafi Feb 12, 2019
84ae01f
auto refreshing expired token
janniclas Feb 13, 2019
88d758e
fixed style issue regarding return
janniclas Feb 14, 2019
7215ab3
scala style fixes
janniclas Feb 14, 2019
2ed6a60
Merge branch 'develop' into feature/Authorization
janniclas Feb 14, 2019
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
46 changes: 46 additions & 0 deletions app/authorization/AuthProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// 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 authorization


import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim}
import play.api.Configuration

object AuthProvider {

var Token = "" // scalastyle:ignore

/** This method generates JWT token for registering Delphi-Management at the Instance-Registry
*
* @param validFor
* @return
*/

def generateJwt(validFor: Long = 1)(implicit configuration: Configuration): String = {
val jwtSecretKey = configuration.get[String]("play.http.secret.JWTkey")
if (Token == "" || !Jwt.isValid(Token, jwtSecretKey, Seq(JwtAlgorithm.HS256))) {
val claim = JwtClaim()
.issuedNow
.expiresIn(validFor * 300)
.startsNow
. +("user_id", configuration.get[String]("play.http.instance"))
. +("user_type", "Admin")

Token = Jwt.encode(claim, jwtSecretKey, JwtAlgorithm.HS256)
}
Token
}
}
74 changes: 51 additions & 23 deletions app/controllers/InstanceRegistryController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import akka.actor.{ActorRef, ActorSystem}
import javax.inject.Inject
import play.api.{Configuration, Logger}
import play.api.libs.concurrent.CustomExecutionContext
import play.api.libs.json._
import play.api.libs.ws.WSClient
import akka.stream.Materializer
import play.api.libs.streams.ActorFlow
import actors.{ClientSocketActor, PublishSocketMessageActor}
import play.api.mvc._

import scala.concurrent.ExecutionContext
import authorization.AuthProvider
import play.api.libs.json.Json



trait MyExecutionContext extends ExecutionContext
Expand Down Expand Up @@ -63,9 +64,15 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
val instanceRegistryUri = config.get[String]("app.instanceRegistryUri")
val instanceRegistryBasePath = config.get[String]("app.instanceRegistryBasePath")

/**This method maps list of instances with specific componentType.
*
* @param componentType
* @return
*/
def instances(componentType: String): Action[AnyContent] = Action.async {

ws.url(instanceRegistryUri + "/instances").addQueryStringParameters("ComponentType" -> componentType).get().map { response =>
ws.url(instanceRegistryUri).addQueryStringParameters("ComponentType" -> componentType)
.withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
.get().map { response =>
// TODO: possible handling of parsing the data can be done here

Ok(response.body)
Expand All @@ -80,8 +87,15 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
}
}

/**Called to fetch network graph of current registry. Contains a list of all instances and all links
* currently registered.
*
* @return
*/

def getNetwork(): Action[AnyContent] = Action.async {
ws.url(instanceRegistryUri + "/network").get().map { response =>
ws.url(instanceRegistryUri + "/instances/network").withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
.get().map { response =>
// TODO: possible handling of parsing the data can be done here
Logger.debug(response.body)
if (response.status == 200) {
Expand All @@ -92,10 +106,20 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
}(myExecutionContext)
}

def numberOfInstances(componentType: String) : Action[AnyContent] = Action.async {
/**
* Fetches the number of instances for the specified ComponentType. The ComponentType is an optional parameter which is passed as an query
* argument named 'ComponentType'
*
* @param componentType
* @return
*/

def numberOfInstances(componentType: String): Action[AnyContent] = Action.async {
// TODO: handle what should happen if the instance registry is not reachable.
// TODO: create constants for the urls
ws.url(instanceRegistryUri + "/numberOfInstances").addQueryStringParameters("ComponentType" -> componentType).get().map { response =>
ws.url(instanceRegistryUri + "/count").addQueryStringParameters("ComponentType" -> componentType)
.withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
.get().map { response =>
// TODO: possible handling of parsing the data can be done here
if (response.status == 200) {
Ok(response.body)
Expand All @@ -106,31 +130,36 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
}

/**
* This function is for handling all(start, stop, play, pause, resume) POST request.
* To control the instance State
* @param componentId
*/
* This function is for handling all(start, stop, play, pause, resume) POST request.
* To control the instance State (E.g. /instances/42/stop )
*
* @param componentId
*/


def handleRequest(action: String, instanceID: String): Action[AnyContent] = Action.async { request =>
ws.url(instanceRegistryUri + action)
.addQueryStringParameters("Id" -> instanceID)
ws.url(instanceRegistryUri + "/instances/" + instanceID + action)
.withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
.post("")
.map { response =>
new Status(response.status)
}(myExecutionContext)
}

/**
* This function is for handling an POST request for adding an instance to the Scala web server
*
* @param componentType
* @param name
*/
def postInstance(compType: String, name: String): Action[AnyContent] = Action.async { request =>
ws.url(instanceRegistryUri + "/deploy")
.addQueryStringParameters("ComponentType" -> compType, "InstanceName" -> name)
.post("")
* This function is for handling an POST request for adding an instance to the Scala web server
* (E.g. .../instances/deploy
*
* @param componentType
* @param name
*/

def postInstance(compType: String, name: String): Action[AnyContent] = Action.async
{
request =>
ws.url(instanceRegistryUri + "/instances/deploy")
.withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
.post(Json.obj("ComponentType" -> compType, "InstanceName" -> name))
.map { response =>
response.status match {
// scalastyle:off magic.number
Expand All @@ -142,5 +171,4 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
}
}(myExecutionContext)
}

}
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ libraryDependencies ++= Seq(
"com.google.guava" % "guava" % "25.1-jre",
"org.apache.commons" % "commons-compress" % "1.16"
)

libraryDependencies += "com.pauldijou" %% "jwt-core" % "1.0.0"
Loading