@@ -27,11 +27,10 @@ import akka.stream.Materializer
2727import play .api .libs .streams .ActorFlow
2828import actors .{ClientSocketActor , PublishSocketMessageActor }
2929import play .api .mvc ._
30- import scala .concurrent .ExecutionContext
30+ import scala .concurrent .{ Future , ExecutionContext }
3131import authorization .AuthProvider
3232import authorization .AuthAction
33- import play .api .libs .json .Json
34-
33+ import play .api .libs .json .{Json , JsValue }
3534
3635
3736trait MyExecutionContext extends ExecutionContext
@@ -205,23 +204,30 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
205204
206205 def authentication (): Action [AnyContent ] = Action .async {
207206 request =>
208- val json = request.body.asJson.get
209-
210- val username = (json \ " username" ).as[String ]
211- val password = (json \ " password" ).as[String ]
212-
213- ws.url(instanceRegistryUri + " /users" + " /authenticate" )
214- .withAuth(username, password, WSAuthScheme .BASIC )
215- .withHttpHeaders((" Delphi-Authorization" , s " ${AuthProvider .generateJwt()}" ))
216- .post(" " )
217- .map { response =>
218- if (response.status == 200 ) {
219- Ok (Json .obj(" token" -> response.body, " refreshToken" -> " " ))
220- } else {
221- new Status (response.status)
207+ // val json = request.body.asJson.get
208+
209+
210+ val jsonBody : Option [JsValue ] = request.body.asJson
211+
212+ jsonBody.map { json =>
213+
214+ val username = (json \ " username" ).as[String ]
215+ val password = (json \ " password" ).as[String ]
216+
217+ ws.url(instanceRegistryUri + " /users" + " /authenticate" )
218+ .withAuth(username, password, WSAuthScheme .BASIC )
219+ .withHttpHeaders((" Delphi-Authorization" , s " ${AuthProvider .generateJwt()}" ))
220+ .post(" " )
221+ .map { response =>
222+ if (response.status == 200 ) {
223+ Ok (Json .obj(" token" -> response.body, " refreshToken" -> " " ))
224+ } else {
225+ new Status (response.status)
226+ }
222227 }
223- }(myExecutionContext)
224- }
228+ }.getOrElse{ Future (BadRequest (" Invalid body" ))}
229+
230+ }
225231
226232 /**
227233 * This function is used to add a label to specific instance.
0 commit comments