Commit 7113f0b6 authored by Mathieu's avatar Mathieu
Browse files

The login is now the email adress

parent 78b149ed
......@@ -43,8 +43,6 @@ object Application extends App {
server.start()
}
DB.addUser(DB.Login("moo"), DB.Password("moo"), "moo@me.com")
println(DB.users)
DB.addUser(DB.Email("moo@moo.com"), DB.Password("moo"))
}
package org.openmoleconnect.client
object AdminPanel {
}
......@@ -50,11 +50,11 @@ object Connection {
lazy val connectButton = tags.button("Connect", btn_primary, `type` := "submit").render
lazy val cookieButton = tags.button("Cookuie", btn_default, onclick := { () => println("COOKIES: " + dom.document.cookie) }).render
//lazy val cookieButton = tags.button("Cookuie", btn_default, onclick := { () => println("COOKIES: " + dom.document.cookie) }).render
lazy val loginInput = inputTag("")(
name := "login",
placeholder := "Login",
lazy val emailInput = inputTag("")(
name := "email",
placeholder := "Email",
width := "130px",
marginBottom := 15,
autofocus := true
......@@ -70,13 +70,13 @@ object Connection {
def cleanInputs = {
passwordInput.value = ""
loginInput.value = ""
emailInput.value = ""
}
val connectionForm: HTMLFormElement = form(
method := "post",
action := connectionRoute,
loginInput,
emailInput,
passwordInput,
connectButton
).render
......
......@@ -42,7 +42,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
Authentication.isValid(request, TokenType.refreshToken) match {
case true =>
withRefreshToken { refreshToken =>
val tokenData = TokenData.accessToken(refreshToken.host, refreshToken.login)
val tokenData = TokenData.accessToken(refreshToken.host, refreshToken.email)
buildAndAddCookieToHeader(tokenData)
action(tokenData)
}
......@@ -119,19 +119,19 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
post(connectionRoute) {
Authentication.isValid(request, TokenType.accessToken) match {
case false =>
val login = params.getOrElse("login", "")
val email = params.getOrElse("email", "")
// Get login and password from the post request parameters
val password = params.getOrElse("password", "")
if (login.isEmpty || password.isEmpty) connectionHtml
if (email.isEmpty || password.isEmpty) connectionHtml
//Build cookie with JWT token if login/password are valid and redirect to the openmole manager url
else {
DB.uuid(DB.Login(login), DB.Password(password)) match {
DB.uuid(DB.Email(email), DB.Password(password)) match {
case Some(uuid) =>
val host = Host(uuid, K8sService.hostIP(uuid))
buildAndAddCookieToHeader(TokenData.accessToken(host, DB.Login(login)))
buildAndAddCookieToHeader(TokenData.refreshToken(host, DB.Login(login)))
buildAndAddCookieToHeader(TokenData.accessToken(host, DB.Email(email)))
buildAndAddCookieToHeader(TokenData.refreshToken(host, DB.Email(email)))
redirect("/")
case _ => connectionHtml
}
......
......@@ -12,7 +12,7 @@ object DB {
case class UUID(value: String) extends MappedTo[String]
case class Login(value: String) extends MappedTo[String]
case class Email(value: String) extends MappedTo[String]
case class Password(value: String) extends MappedTo[String]
......@@ -21,21 +21,19 @@ object DB {
val admin = Role("admin")
val simpleUser = Role("simpleUser")
case class User(login: Login, password: Password, email: String, role: Role = simpleUser, uuid: UUID = UUID(""))
case class User(email: Email, password: Password, role: Role = simpleUser, uuid: UUID = UUID(""))
class Users(tag: Tag) extends Table[(UUID, Login, Password, String, Role)](tag, "USERS") {
class Users(tag: Tag) extends Table[(UUID, Email, Password, Role)](tag, "USERS") {
def uuid = column[UUID]("UUID", O.PrimaryKey)
def login = column[Login]("LOGIN")
def email = column[Email]("EMAIL")
def password = column[Password]("PASSWORD")
def email = column[String]("EMAIL")
def role = column[Role]("ROLE")
def * = (uuid, login, password, email, role)
def * = (uuid, email, password, role)
}
val userTable = TableQuery[Users]
......@@ -49,7 +47,7 @@ object DB {
Await.result(
db.run(userTable.result).map { x =>
x.map {
case (uuid, login, password, email, role) => User(login, password, email, role, uuid)
case (uuid, email, password, role) => User(email, password, role, uuid)
}
}, Duration.Inf
)
......@@ -57,11 +55,11 @@ object DB {
// val users = Seq(User(Login("foo"), Password("foo"), UUID("foo-123-567-foo")), User(Login("bar"), Password("bar"), UUID("bar-123-567-bar")))
def uuid(login: Login): Option[UUID] = users.find(_.login == login).map {
def uuid(email: Email): Option[UUID] = users.find(_.email == email).map {
_.uuid
}
def uuid(login: Login, password: Password): Option[UUID] = users.find(u => u.login == login && u.password == password).map {
def uuid(email: Email, password: Password): Option[UUID] = users.find(u => u.email == email && u.password == password).map {
_.uuid
}
......@@ -82,11 +80,11 @@ object DB {
def initDB = {
runTransaction(userTable.schema.createIfNotExists)
if (DB.users.isEmpty) {
DB.addUser(DB.Login("admin"), DB.Password("admin"), "", DB.admin)
DB.addUser(DB.Email("admin@admin.com"), DB.Password("admin"), DB.admin)
}
}
def exists(email: String) = {
def exists(email: Email) = {
Await.result(
db.run(
(for {
......@@ -98,11 +96,11 @@ object DB {
)
}
def addUser(login: Login, password: Password, email: String, role: Role = simpleUser) = {
def addUser(email: Email, password: Password, role: Role = simpleUser) = {
if (!exists(email)) {
runTransaction(
userTable += (UUID(util.UUID.randomUUID().toString), login, password, email, role)
userTable += (UUID(util.UUID.randomUUID().toString), email, password, role)
)
}
}
......
......@@ -34,7 +34,7 @@ object JWT {
object TokenData {
def fromTokenContent(content: String, tokenType: TokenType)(implicit secret: Secret) = {
Jwt.decode(content, secret, Seq(JwtAlgorithm.HS256)).map { jwtClaim =>
val login: Login = Login(Json.fromJson(jwtClaim.content, Json.key.login))
val email: Email = Email(Json.fromJson(jwtClaim.content, Json.key.email))
val host = {
val uuid: UUID = UUID(Json.fromJson(jwtClaim.content, Json.key.uuid))
......@@ -46,23 +46,23 @@ object JWT {
Host(uuid, hip)
}
TokenData(login, host, jwtClaim.issuedAt.get, jwtClaim.expiration.get, tokenType)
TokenData(email, host, jwtClaim.issuedAt.get, jwtClaim.expiration.get, tokenType)
}.toOption.filter {
hasExpired(_)
}
}
def accessToken(host: Host, login: Login) = TokenData(login, host, now, inFiveMinutes, TokenType.accessToken)
def accessToken(host: Host, email: Email) = TokenData(email, host, now, inFiveMinutes, TokenType.accessToken)
def refreshToken(host: Host, login: Login) = TokenData(login, host, now, inOneMonth, TokenType.refreshToken)
def refreshToken(host: Host, email: Email) = TokenData(email, host, now, inOneMonth, TokenType.refreshToken)
}
case class TokenData(login: Login, host: Host, issued: Long, expirationTime: Long, tokenType: TokenType) {
case class TokenData(email: Email, host: Host, issued: Long, expirationTime: Long, tokenType: TokenType) {
def toContent(implicit secret: Secret) = {
implicit val clock = Clock.systemUTC()
val claims = Seq((Json.key.uuid, host.uuid.value), (Json.key.hostIP, host.hostIP.getOrElse("")), (Json.key.login, login.value))
val claims = Seq((Json.key.uuid, host.uuid.value), (Json.key.hostIP, host.hostIP.getOrElse("")), (Json.key.email, email.value))
val expandedClaims = claims.map { case (k, v) =>
s"""
......
......@@ -8,7 +8,7 @@ import org.json4s.jackson.JsonMethods._
object Json {
object key {
val login = "login"
val email = "email"
val uuid = "uuid"
val hostIP = "hostIP"
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment