Skip to content

Commit 5c05008

Browse files
committed
Route for refresh token and generateRefreshJWT refs #128
1 parent 837dba5 commit 5c05008

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

app/authorization/AuthProvider.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,15 @@ import play.api.Configuration
5959
Jwt.isValid(token, jwtSecretKey, Seq(JwtAlgorithm.HS256)) // Decode the token using the secret key
6060
}
6161

62+
//This method is exactly the same as the generateJWT(). It is just missing the userType. I believe the
63+
//above method can also serve the same purpose. I havent tested these methods with /users/refreshToken thats why I cant say for sure.
64+
def generateRefreshJWT(validFor: Long= 1)(implicit configuration: Configuration): String = {
65+
val jwtSecretKey = configuration.get[String]("play.http.secret.JWTkey")
66+
val refreshClaim = JwtClaim()
67+
.issuedNow
68+
.expiresIn((validFor * 300))
69+
.startsNow
70+
. +("user_id", configuration.get[String]("play.http.instance"))
71+
Jwt.encode(refreshClaim, jwtSecretKey, JwtAlgorithm.HS256)
72+
}
6273
}

app/controllers/ApiRouter.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ class ApiRouter @Inject()(irController: InstanceRegistryController, sysControlle
4646
case POST(p"/reconnectInstance" ? q"from=$from"& q"to=$to") => irController.reconnect(from.toInt, to.toInt)
4747
case POST(p"/authenticate") => irController.authentication()
4848
case POST(p"/labelInstance" ? q"instanceID=$instanceID"& q"label=$label") => irController.labelInstance(instanceID, label)
49+
case POST(p"/refreshToken") => irController.refreshToken()
4950
}
5051
}

app/controllers/InstanceRegistryController.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
9494
*/
9595

9696
def getNetwork(): Action[AnyContent] = authAction.async {
97+
println(AuthProvider.generateRefreshJWT())
9798
ws.url(instanceRegistryUri + "/instances/network").withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
9899
.get().map { response =>
99100
// TODO: possible handling of parsing the data can be done here
@@ -256,14 +257,19 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
256257
{
257258
request =>
258259
ws.url(instanceRegistryUri + "/users" + "/refreshToken")
259-
.withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
260+
.withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateRefreshJWT()}"))
260261
.post("")
261262
.map { response =>
262263
response.status match {
263-
case 200 =>
264-
Ok(response.body)
265-
case 400 =>
264+
// scalastyle:off magic.number
265+
case 202 =>
266+
Ok((response.json \ "token" \ "refreshToken").as[String])
267+
//Ok(Json.obj("token" -> "", "refreshToken" -> ""))
268+
case 401 =>
266269
Unauthorized
270+
// scalastyle:on magic.number
271+
case x: Any =>
272+
new Status(x)
267273
}
268274
}(myExecutionContext)
269275
}

0 commit comments

Comments
 (0)