Skip to content
Snippets Groups Projects
Commit 9db25223 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Add Session Key Gen, Add Session GET, POST, DELETE routes

parent b5672843
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,8 @@ import java.sql.Date ...@@ -5,6 +5,8 @@ import java.sql.Date
import com.j256.ormlite.field.DatabaseField import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.table.DatabaseTable import com.j256.ormlite.table.DatabaseTable
import scala.util.Random
/** /**
* Created by tobias on 15.02.17. * Created by tobias on 15.02.17.
*/ */
...@@ -15,11 +17,19 @@ import com.j256.ormlite.table.DatabaseTable ...@@ -15,11 +17,19 @@ import com.j256.ormlite.table.DatabaseTable
@DatabaseField var key: String = _ @DatabaseField var key: String = _
@DatabaseField var createDate: Date = _ @DatabaseField var createDate: Date = _
def this(key: String, createDate: Date) { def this(account: Account, key: String) {
this() this()
this.account = account
this.key = key this.key = key
this.createDate = createDate this.createDate = new Date(System.currentTimeMillis)
} }
def getId: Int = id def getId: Int = id
} }
object Session {
private val length = 100
val alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
def generateKey(): String = (1 to length).map(_ => alpha(Random.nextInt.abs % alpha.length())).mkString
}
package de.tobias.playpad.server.server.account
import com.j256.ormlite.dao.Dao
import de.tobias.playpad.server.account.{Account, Session}
import spark.{Request, Response, Route}
/**
* Created by tobias on 15.02.17.
*/
class SessionDelete(accountDao: Dao[Account, Int]) extends Route {
override def handle(request: Request, response: Response): AnyRef = {
val username = request.queryParams("username")
val password = request.queryParams("password")
val key = request.queryParams("key")
// check account
val accounts = accountDao.queryForEq("username", username)
if (accounts.size() == 1) {
val account = accounts.get(0)
if (account.password.equals(password)) {
account.sessions.removeIf(s => s.key.equals(key))
accountDao.update(account)
return "deleted"
}
}
null
}
}
package de.tobias.playpad.server.server.account
import com.google.gson.{JsonArray, JsonObject}
import com.j256.ormlite.dao.Dao
import de.tobias.playpad.server.account.{Account, Session}
import spark.{Request, Response, Route}
/**
* Created by tobias on 15.02.17.
*/
class SessionGet(accountDao: Dao[Account, Int]) extends Route {
override def handle(request: Request, response: Response): AnyRef = {
val username = request.queryParams("username")
val password = request.queryParams("password")
// check account
val accounts = accountDao.queryForEq("username", username)
if (accounts.size() == 1) {
val account = accounts.get(0)
if (account.password.equals(password)) {
val array = new JsonArray
account.sessions.forEach(session => {
val jsonObj = new JsonObject
jsonObj.addProperty("key", session.key)
jsonObj.addProperty("createDate", session.createDate.getTime)
array.add(jsonObj)
})
return array.toString
}
}
null
}
}
package de.tobias.playpad.server.server.account
import com.j256.ormlite.dao.Dao
import de.tobias.playpad.server.account.{Account, Session}
import spark.{Request, Response, Route}
/**
* Created by tobias on 15.02.17.
*/
class SessionPost(accountDao: Dao[Account, Int]) extends Route {
override def handle(request: Request, response: Response): AnyRef = {
val username = request.queryParams("username")
val password = request.queryParams("password")
// check account
val accounts = accountDao.queryForEq("username", username)
if (accounts.size() == 1) {
val account = accounts.get(0)
if (account.password.equals(password)) {
val randomKey = Session.generateKey()
val session = new Session(account, randomKey)
account.sessions.add(session)
accountDao.update(account)
return randomKey
}
}
null
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment