Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit bca9a35

Browse files
committed
Added login rest endpoint.
1 parent 3f7bb56 commit bca9a35

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed

src/main/scala/org/codeoverflow/chatoverflow/ChatOverflow.scala

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,47 @@ class ChatOverflow(val pluginFolderPath: String,
5353
logger debug "Finished updating type registry."
5454

5555
logger debug "Loading configs and credentials."
56-
askForPassword()
57-
load()
56+
// TODO: Add flag for asking for the password on the console prior to the gui
57+
//askForPassword()
58+
//load()
5859
logger debug "Finished loading configs and credentials."
5960

6061
logger debug "Finished initialization."
6162
}
6263

63-
private def askForPassword(): Unit = {
64-
val password = if (System.console() != null)
65-
System.console().readPassword("Please enter password (Input hidden) > ")
66-
else
67-
scala.io.StdIn.readLine("Please enter password (Input NOT hidden) > ").toCharArray
68-
credentialsService.setPassword(password)
69-
}
70-
71-
private def enableFrameworkSecurity(): Unit = {
72-
Policy.setPolicy(new SandboxSecurityPolicy)
73-
System.setSecurityManager(new SecurityManager)
74-
}
75-
7664
/**
7765
* Loads all config settings and credentials from the config folder.
7866
*/
79-
def load(): Unit = {
67+
def load(): Boolean = {
8068
val currentTime = System.currentTimeMillis()
69+
var success = true
8170

8271
// Start by loading connectors
83-
configService.loadConnectors()
72+
if (!configService.loadConnectors())
73+
success = false
8474

8575
// Then load credentials that can be put into the connectors
86-
credentialsService.load()
76+
if (!credentialsService.load())
77+
success = false
8778

8879
// Finish by loading plugin instances
8980
configService.loadPluginInstances(pluginInstanceRegistry, pluginFramework, typeRegistry)
9081

9182
logger info s"Loading took ${System.currentTimeMillis() - currentTime} ms."
83+
success
84+
}
85+
86+
private def enableFrameworkSecurity(): Unit = {
87+
Policy.setPolicy(new SandboxSecurityPolicy)
88+
System.setSecurityManager(new SecurityManager)
89+
}
90+
91+
private def askForPassword(): Unit = {
92+
val password = if (System.console() != null)
93+
System.console().readPassword("Please enter password (Input hidden) > ")
94+
else
95+
scala.io.StdIn.readLine("Please enter password (Input NOT hidden) > ").toCharArray
96+
credentialsService.setPassword(password)
9297
}
9398

9499
/**

src/main/scala/org/codeoverflow/chatoverflow/configuration/CredentialsService.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class CredentialsService(val credentialsFilePath: String) extends WithLogger {
2222
*/
2323
def setPassword(password: Array[Char]): Unit = this.password = password
2424

25+
def isLoggedIn: Boolean = {
26+
password.length > 0
27+
}
28+
2529
/**
2630
* Loads the credentials form the credentials file and decrypts them.
2731
*
@@ -45,7 +49,7 @@ class CredentialsService(val credentialsFilePath: String) extends WithLogger {
4549
encrypted.close
4650

4751
if (decrypted.isEmpty) {
48-
logger error "Wrong password. Unable to load credentials."
52+
logger warn "Wrong password. Unable to load credentials."
4953
false
5054
} else {
5155
logger info "Password correct."

src/main/scala/org/codeoverflow/chatoverflow/ui/web/SwaggerSupport.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import org.json4s.{JValue, _}
66
import org.scalatra.ScalatraServlet
77
import org.scalatra.swagger.{Api, ApiInfo, JacksonSwaggerBase, Swagger}
88

9+
/**
10+
* The open API servlet provides a generated swagger.json with the api definitions of the framework.
11+
*
12+
* @param swagger the swagger framework, implicitly passed from the Scalatra Bootstrap
13+
*/
914
class OpenAPIServlet(implicit val swagger: Swagger) extends ScalatraServlet with JacksonSwaggerBase {
1015

1116
/**

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/DTOs.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ object DTOs {
3030

3131
case class ConnectorRef(sourceIdentifier: String, uniqueTypeString: String)
3232

33+
case class Password(password: String)
34+
3335
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/config/ConfigController.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.codeoverflow.chatoverflow.ui.web.rest.config
22

33
import org.codeoverflow.chatoverflow.api.APIVersion
44
import org.codeoverflow.chatoverflow.ui.web.JsonServlet
5-
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.ConfigInfo
5+
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConfigInfo, Password, ResultMessage}
66
import org.scalatra.swagger._
77

88
class ConfigController(implicit val swagger: Swagger) extends JsonServlet with ConfigControllerDefinition {
@@ -12,4 +12,30 @@ class ConfigController(implicit val swagger: Swagger) extends JsonServlet with C
1212
APIVersion.MINOR_VERSION, chatOverflow.pluginFolderPath,
1313
chatOverflow.configFolderPath, chatOverflow.requirementPackage)
1414
}
15+
16+
post("/save", operation(postSave)) {
17+
chatOverflow.save()
18+
true
19+
}
20+
21+
get("/login", operation(getLogin)) {
22+
chatOverflow.credentialsService.isLoggedIn
23+
}
24+
25+
post("/login", operation(postLogin)) {
26+
parsedAs[Password] {
27+
case Password(password) =>
28+
chatOverflow.credentialsService.setPassword(password.toCharArray)
29+
if (!chatOverflow.load()) {
30+
// TODO: Could have more details with a more detailed loading process
31+
ResultMessage(success = false, "Unable to load. Wrong password?")
32+
} else {
33+
ResultMessage(success = true)
34+
}
35+
}
36+
}
37+
38+
// TODO: Handle first time login / Creation of the config files (same as postLogin above...?)
39+
40+
// TODO: After this: Add the flags (pw, log output, data folder) and update the ConfigInfo
1541
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/config/ConfigControllerDefinition.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codeoverflow.chatoverflow.ui.web.rest.config
22

3-
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.ConfigInfo
3+
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConfigInfo, Password, ResultMessage}
44
import org.scalatra.swagger.SwaggerSupport
55
import org.scalatra.swagger.SwaggerSupportSyntax.OperationBuilder
66

@@ -11,5 +11,21 @@ trait ConfigControllerDefinition extends SwaggerSupport {
1111
summary "Shows general information and settings."
1212
description "Shows API version and chat overflow startup settings.")
1313

14+
val postSave: OperationBuilder =
15+
(apiOperation[Boolean]("postSave")
16+
summary "Triggers the saving process of the framework manually."
17+
description "Triggers saving of credentials and configuration. Should not be needed manually.")
18+
19+
val getLogin: OperationBuilder =
20+
(apiOperation[Boolean]("getLogin")
21+
summary "Returns if the framework is already supplied with a password to decrypt its credentials."
22+
description "Returns true, if a password has been set. Note that this says nothing about its correctness.")
23+
24+
val postLogin: OperationBuilder =
25+
(apiOperation[ResultMessage]("postLogin")
26+
summary "Logs in the the framework with the given password, loads config and credentials."
27+
description "Tries to decrypt the credentials using the provided password. Does load configuration and credentials."
28+
parameter bodyParam[Password]("body").description("Requires the user framework password."))
29+
1430
override protected def applicationDescription: String = "Handles configuration and information retrieval."
1531
}

0 commit comments

Comments
 (0)