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 { ...@@ -43,8 +43,6 @@ object Application extends App {
server.start() server.start()
} }
DB.addUser(DB.Login("moo"), DB.Password("moo"), "moo@me.com") DB.addUser(DB.Email("moo@moo.com"), DB.Password("moo"))
println(DB.users)
} }
package org.openmoleconnect.client
object AdminPanel {
}
...@@ -50,11 +50,11 @@ object Connection { ...@@ -50,11 +50,11 @@ object Connection {
lazy val connectButton = tags.button("Connect", btn_primary, `type` := "submit").render 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("")( lazy val emailInput = inputTag("")(
name := "login", name := "email",
placeholder := "Login", placeholder := "Email",
width := "130px", width := "130px",
marginBottom := 15, marginBottom := 15,
autofocus := true autofocus := true
...@@ -70,13 +70,13 @@ object Connection { ...@@ -70,13 +70,13 @@ object Connection {
def cleanInputs = { def cleanInputs = {
passwordInput.value = "" passwordInput.value = ""
loginInput.value = "" emailInput.value = ""
} }
val connectionForm: HTMLFormElement = form( val connectionForm: HTMLFormElement = form(
method := "post", method := "post",
action := connectionRoute, action := connectionRoute,
loginInput, emailInput,
passwordInput, passwordInput,
connectButton connectButton
).render ).render
......
...@@ -42,7 +42,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra ...@@ -42,7 +42,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
Authentication.isValid(request, TokenType.refreshToken) match { Authentication.isValid(request, TokenType.refreshToken) match {
case true => case true =>
withRefreshToken { refreshToken => withRefreshToken { refreshToken =>
val tokenData = TokenData.accessToken(refreshToken.host, refreshToken.login) val tokenData = TokenData.accessToken(refreshToken.host, refreshToken.email)
buildAndAddCookieToHeader(tokenData) buildAndAddCookieToHeader(tokenData)
action(tokenData) action(tokenData)
} }
...@@ -119,19 +119,19 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra ...@@ -119,19 +119,19 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
post(connectionRoute) { post(connectionRoute) {
Authentication.isValid(request, TokenType.accessToken) match { Authentication.isValid(request, TokenType.accessToken) match {
case false => case false =>
val login = params.getOrElse("login", "") val email = params.getOrElse("email", "")
// Get login and password from the post request parameters // Get login and password from the post request parameters
val password = params.getOrElse("password", "") 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 //Build cookie with JWT token if login/password are valid and redirect to the openmole manager url
else { else {
DB.uuid(DB.Login(login), DB.Password(password)) match { DB.uuid(DB.Email(email), DB.Password(password)) match {
case Some(uuid) => case Some(uuid) =>
val host = Host(uuid, K8sService.hostIP(uuid)) val host = Host(uuid, K8sService.hostIP(uuid))
buildAndAddCookieToHeader(TokenData.accessToken(host, DB.Login(login))) buildAndAddCookieToHeader(TokenData.accessToken(host, DB.Email(email)))
buildAndAddCookieToHeader(TokenData.refreshToken(host, DB.Login(login))) buildAndAddCookieToHeader(TokenData.refreshToken(host, DB.Email(email)))
redirect("/") redirect("/")
case _ => connectionHtml case _ => connectionHtml
} }
......
...@@ -12,7 +12,7 @@ object DB { ...@@ -12,7 +12,7 @@ object DB {
case class UUID(value: String) extends MappedTo[String] 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] case class Password(value: String) extends MappedTo[String]
...@@ -21,21 +21,19 @@ object DB { ...@@ -21,21 +21,19 @@ object DB {
val admin = Role("admin") val admin = Role("admin")
val simpleUser = Role("simpleUser") 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 uuid = column[UUID]("UUID", O.PrimaryKey)
def login = column[Login]("LOGIN") def email = column[Email]("EMAIL")
def password = column[Password]("PASSWORD") def password = column[Password]("PASSWORD")
def email = column[String]("EMAIL")
def role = column[Role]("ROLE") def role = column[Role]("ROLE")
def * = (uuid, login, password, email, role) def * = (uuid, email, password, role)
} }
val userTable = TableQuery[Users] val userTable = TableQuery[Users]
...@@ -49,7 +47,7 @@ object DB { ...@@ -49,7 +47,7 @@ object DB {
Await.result( Await.result(
db.run(userTable.result).map { x => db.run(userTable.result).map { x =>
x.map { 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 }, Duration.Inf
) )
...@@ -57,11 +55,11 @@ object DB { ...@@ -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"))) // 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 _.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 _.uuid
} }
...@@ -82,11 +80,11 @@ object DB { ...@@ -82,11 +80,11 @@ object DB {
def initDB = { def initDB = {
runTransaction(userTable.schema.createIfNotExists) runTransaction(userTable.schema.createIfNotExists)
if (DB.users.isEmpty) { 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( Await.result(
db.run( db.run(
(for { (for {
...@@ -98,11 +96,11 @@ object DB { ...@@ -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)) { if (!exists(email)) {
runTransaction( 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 { ...@@ -34,7 +34,7 @@ object JWT {
object TokenData { object TokenData {
def fromTokenContent(content: String, tokenType: TokenType)(implicit secret: Secret) = { def fromTokenContent(content: String, tokenType: TokenType)(implicit secret: Secret) = {
Jwt.decode(content, secret, Seq(JwtAlgorithm.HS256)).map { jwtClaim => 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 host = {
val uuid: UUID = UUID(Json.fromJson(jwtClaim.content, Json.key.uuid)) val uuid: UUID = UUID(Json.fromJson(jwtClaim.content, Json.key.uuid))
...@@ -46,23 +46,23 @@ object JWT { ...@@ -46,23 +46,23 @@ object JWT {
Host(uuid, hip) 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 { }.toOption.filter {
hasExpired(_) 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) = { def toContent(implicit secret: Secret) = {
implicit val clock = Clock.systemUTC() 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) => val expandedClaims = claims.map { case (k, v) =>
s""" s"""
......
...@@ -8,7 +8,7 @@ import org.json4s.jackson.JsonMethods._ ...@@ -8,7 +8,7 @@ import org.json4s.jackson.JsonMethods._
object Json { object Json {
object key { object key {
val login = "login" val email = "email"
val uuid = "uuid" val uuid = "uuid"
val hostIP = "hostIP" 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