Skip to content

Commit 9449e22

Browse files
Merge pull request #121 from delphi-hub/authTest
Removed user secret from GET endpoints
2 parents b2675db + d0168aa commit 9449e22

File tree

11 files changed

+37
-36
lines changed

11 files changed

+37
-36
lines changed

OpenAPISpecification.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,6 @@ definitions:
959959
userName:
960960
type: string
961961
example: Ben
962-
secret:
963-
type: string
964-
example: 0DE19F6FAAFB7CF372172CEA658800999A75DB9E79AF5F378F274E47DF810CEE
965962
userType:
966963
type: string
967964
enum:

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/RequestHandler.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -977,14 +977,14 @@ class RequestHandler(configuration: Configuration, authDao: AuthDAO, instanceDao
977977
* @param user The user to add
978978
* @return Id assigned to that user
979979
*/
980-
def handleAddUser(user: DelphiUser): Try[String] = {
980+
def handleAddUser(user: DelphiUser): Try[Long] = {
981981

982982
val noIdUser = DelphiUser(id = None, userName = user.userName, secret = user.secret, userType = user.userType)
983983

984984
authDao.addUser(noIdUser) match {
985-
case Success(username) =>
985+
case Success(userId) =>
986986
log.info(s"Successfully handled create user request")
987-
Success(username)
987+
Success(userId)
988988
case Failure(x) => Failure(x)
989989
}
990990
}

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/authorization/AuthProvider.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class AuthProvider(authDAO: AuthDAO) extends AppLogging {
5353
private def getSecretForUser(userName: String): Option[String] ={
5454
val user = authDAO.getUserWithUsername(userName)
5555
if(user.isDefined){
56-
Some(user.get.secret)
56+
Some(user.get.secret.get)
5757
} else {
5858
None
5959
}

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/connection/Server.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,9 @@ class Server(handler: RequestHandler) extends HttpApp
11911191
try {
11921192
val paramInstance: DelphiUser = UserString.parseJson.convertTo[DelphiUser](authDelphiUserFormat)
11931193
handler.handleAddUser(paramInstance) match {
1194-
case Success(username) =>
1194+
case Success(userId) =>
11951195
complete {
1196-
username
1196+
userId.toString
11971197
}
11981198
case Failure(ex) =>
11991199
log.error(ex, "Failed to handle create user request.")
@@ -1223,6 +1223,8 @@ class Server(handler: RequestHandler) extends HttpApp
12231223
def allUsers(): server.Route = Route.seal{
12241224
authenticateOAuth2[AccessToken]("Secure Site", handler.authProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
12251225
get {
1226+
log.info("kutta")
1227+
log.info(handler.getAllUsers().toString())
12261228
complete {
12271229
handler.getAllUsers().toList
12281230
}

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/daos/AuthDAO.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait AuthDAO {
2525
* Add user
2626
* @return
2727
*/
28-
def addUser(delphiUser : DelphiUser) : Try[String]
28+
def addUser(delphiUser : DelphiUser) : Try[Long]
2929

3030
/**
3131
* Remove user with username

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/daos/DatabaseAuthDAO.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class DatabaseAuthDAO (configuration : Configuration) extends AuthDAO with AppLo
5353
{
5454
if(hasUserWithUsername(userName)) {
5555
val result = Await.result(dbAuth.run(users.filter(_.userName === userName).result.headOption), Duration.Inf)
56-
Some(dataToObjectAuthenticate(result.get._1, result.get._2, result.get._3, result.get._4))
56+
Some(dataToObjectAuthenticateWithSecret(result.get._1, result.get._2, Some(result.get._3), result.get._4))
5757
} else {
5858
None
5959
}
6060
}
6161

62-
override def addUser(delphiUser : DelphiUser) : Try[String] = {
62+
override def addUser(delphiUser : DelphiUser) : Try[Long] = {
6363
if(hasUserWithUsername(delphiUser.userName)){
6464
Failure(new RuntimeException(s"username ${delphiUser.userName} is already exist."))
6565
} else {
@@ -68,11 +68,11 @@ class DatabaseAuthDAO (configuration : Configuration) extends AuthDAO with AppLo
6868
val secret = delphiUser.secret
6969
val userType = delphiUser.userType.toString
7070

71-
val addFuture: Future[Long] = dbAuth.run((users returning users.map(_.id)) += (id, userName, hashString(secret), userType))
71+
val addFuture: Future[Long] = dbAuth.run((users returning users.map(_.id)) += (id, userName, hashString(secret.get), userType))
7272
val userId = Await.result(addFuture, Duration.Inf)
7373

7474
log.info(s"Added user ${delphiUser.userName} with id $userId to database.")
75-
Success(userName)
75+
Success(userId)
7676
}
7777

7878
}
@@ -146,7 +146,7 @@ class DatabaseAuthDAO (configuration : Configuration) extends AuthDAO with AppLo
146146
log.info("Shutdown complete.")
147147
}
148148

149-
private def dataToObjectAuthenticate(id:Long, userName: String, secret: String, userType: String): DelphiUser = {
149+
private def dataToObjectAuthenticateWithSecret(id:Long, userName: String, secret: Option[String], userType: String): DelphiUser = {
150150
DelphiUser.apply(Option(id), userName, secret, getDelphiUserTypeFromString(userType))
151151
}
152152

@@ -169,7 +169,7 @@ class DatabaseAuthDAO (configuration : Configuration) extends AuthDAO with AppLo
169169

170170
private def dataToObjectUser(options : Option[(Long, String, String, String)]): DelphiUser ={
171171
val optionValue = options.get
172-
DelphiUser(Some(optionValue._1), optionValue._2, optionValue._3, getUserTypeFromString(optionValue._4))
172+
DelphiUser(Some(optionValue._1), optionValue._2, None, getUserTypeFromString(optionValue._4))
173173
}
174174

175175
private def getUserTypeFromString(userType: String): DelphiUserType ={

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/daos/DynamicAuthDAO.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ class DynamicAuthDAO (configuration : Configuration) extends AuthDAO with AppLog
4040
if(hasUserWithUsername(userName)) {
4141
val query = users filter {i => i.userName == userName}
4242
val user = query.iterator.next()
43-
Some(dataToObjectAuthenticate(user.id.get, user.userName, user.secret, user.userType.toString))
43+
Some(dataToObjectAuthenticateWithSecret(user.id.get, user.userName, user.secret.get, user.userType.toString))
4444
} else {
4545
None
4646
}
4747

4848
}
4949

50-
override def addUser(delphiUser : DelphiUser) : Try[String] = {
50+
override def addUser(delphiUser : DelphiUser) : Try[Long] = {
5151
if(hasUserWithUsername(delphiUser.userName)){
5252
Failure(new RuntimeException(s"username ${delphiUser.userName} is already exist."))
5353
} else{
5454
val id = nextId()
55-
val newUser = DelphiUser(Some(id), delphiUser.userName, hashString(delphiUser.secret), delphiUser.userType)
55+
val newUser = DelphiUser(Some(id), delphiUser.userName, Some(hashString(delphiUser.secret.get)), delphiUser.userType)
5656
users.add(newUser)
5757

5858
log.info(s"Added user ${newUser.userName} with id ${newUser.id.get} to database.")
59-
Success(newUser.userName)
59+
Success(id)
6060
}
6161

6262
}
@@ -76,14 +76,14 @@ class DynamicAuthDAO (configuration : Configuration) extends AuthDAO with AppLog
7676
if(hasUserWithId(id)) {
7777
val query = users filter {i => i.id.get == id}
7878
val result = query.iterator.next()
79-
Some(result)
79+
Some(dataToObjectUser(result))
8080
} else {
8181
None
8282
}
8383
}
8484

8585
override def getAllUser(): List[DelphiUser] = {
86-
List() ++ users
86+
List() ++ users map dataToObjectUser
8787
}
8888

8989
override def hasUserWithUsername(username: String) : Boolean = {
@@ -109,10 +109,14 @@ class DynamicAuthDAO (configuration : Configuration) extends AuthDAO with AppLog
109109
log.info("Shutdown complete dynamic Auth DAO.")
110110
}
111111

112-
private def dataToObjectAuthenticate(id:Long, userName: String, secret: String, userType: String): DelphiUser = {
113-
DelphiUser.apply(Option(id), userName, secret, getDelphiUserTypeFromString(userType))
112+
private def dataToObjectUser(delphiUser : DelphiUser): DelphiUser ={
113+
114+
DelphiUser(delphiUser.id, delphiUser.userName, None, delphiUser.userType)
114115
}
115116

117+
private def dataToObjectAuthenticateWithSecret(id:Long, userName: String, secret: String, userType: String): DelphiUser = {
118+
DelphiUser.apply(Option(id), userName, Some(secret), getDelphiUserTypeFromString(userType))
119+
}
116120

117121
private[daos] def clearData() : Unit = {
118122
users.clear()

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/io/swagger/client/model/DelphiUser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ trait UserJsonSupport extends SprayJsonSupport with DefaultJsonProtocol{
4848
implicit val authDelphiUserFormat: JsonFormat[DelphiUser] = jsonFormat4(DelphiUser)
4949
}
5050

51-
final case class DelphiUser(id: Option[Long], userName: String, secret: String, userType: DelphiUserType)
51+
final case class DelphiUser(id: Option[Long], userName: String, secret: Option[String], userType: DelphiUserType)
5252

5353
object DelphiUserEnums {
5454

src/test/scala/de/upb/cs/swt/delphi/instanceregistry/connection/ServerTest.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim}
3434
import spray.json._
3535

3636
import scala.util.{Failure, Success, Try}
37-
import java.nio.charset.StandardCharsets
38-
import java.security.MessageDigest
3937

4038
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.DelphiUserEnums.DelphiUserType
4139

@@ -65,7 +63,7 @@ class ServerTest
6563
private val invalidJsonInstance = validJsonInstance.replace(""""name":"ValidInstance",""", """"name":Invalid", """)
6664

6765
private val validJsonDelphiUser = DelphiUser(id = None,
68-
userName = "validUser" , secret = "validUser",
66+
userName = "validUser" , secret = Some("validUser"),
6967
userType = DelphiUserType.Admin).toJson(authDelphiUserFormat
7068
).toString
7169
private val invalidTypedJsonDelphiUser = validJsonDelphiUser.replace(""""userType":"User",""", """"userType":"Component",""")
@@ -81,8 +79,8 @@ class ServerTest
8179
override def beforeAll(): Unit = {
8280
requestHandler.initialize()
8381
authDao.initialize()
84-
authDao.addUser(DelphiUser(None, "admin" , "admin", DelphiUserType.Admin))
85-
authDao.addUser(DelphiUser(None, "user" , "user", DelphiUserType.User))
82+
authDao.addUser(DelphiUser(None, "admin" , Some("admin"), DelphiUserType.Admin))
83+
authDao.addUser(DelphiUser(None, "user" , Some("user"), DelphiUserType.User))
8684
}
8785

8886
/**
@@ -214,14 +212,14 @@ class ServerTest
214212
}
215213

216214
"successfully remove user when everything is valid" in {
217-
authDao.addUser(DelphiUser(None, "user3" , "user3", DelphiUserType.User))
215+
authDao.addUser(DelphiUser(None, "user3" , Some("user3"), DelphiUserType.User))
218216
Post("/users/3/remove") ~> addAuthorization("Admin") ~> Route.seal(server.routes) ~> check {
219217
assert(status === StatusCodes.ACCEPTED)
220218
}
221219
}
222220

223221
"not remove user if request is invalid" in {
224-
authDao.addUser(DelphiUser(None, "user4" , "user4", DelphiUserType.User))
222+
authDao.addUser(DelphiUser(None, "user4" , Some("user4"), DelphiUserType.User))
225223
//Required type of user authorization needed to create user
226224
Post("/users/4/remove") ~> addAuthorization("User") ~> Route.seal(server.routes) ~> check {
227225
assert(status === StatusCodes.UNAUTHORIZED)
@@ -246,7 +244,7 @@ class ServerTest
246244
}
247245

248246
"successfully get user if user exist" in {
249-
authDao.addUser(DelphiUser(None, "user3" , "user3", DelphiUserType.User))
247+
authDao.addUser(DelphiUser(None, "user3" , Some("user3"), DelphiUserType.User))
250248
Get("/users/1") ~> addAuthorization("Admin") ~> Route.seal(server.routes) ~> check {
251249
assert(status === StatusCodes.OK)
252250
}

src/test/scala/de/upb/cs/swt/delphi/instanceregistry/daos/DatabaseAuthDAOTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class DatabaseAuthDAOTest extends FlatSpec with Matchers with BeforeAndAfterEach
6464
private def buildUser(id : Int, userName : String = "") : DelphiUser = {
6565
val userType = if(id == 1) DelphiUserType.Admin else DelphiUserType.User
6666
val name = if(userName == "") "user" + id else userName
67-
DelphiUser(Some(id), name , hashString("123456"), userType)
67+
DelphiUser(Some(id), name , Some(hashString("123456")), userType)
6868
}
6969

7070
private def hashString(secret: String): String = {

0 commit comments

Comments
 (0)