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

Commit 090b855

Browse files
committed
Added register functionality and credentials creation to the REST API.
1 parent 56f1bfb commit 090b855

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class CredentialsService(val credentialsFilePath: String) extends WithLogger {
4848
}
4949
}
5050

51+
/**
52+
* Returns if the credentials file exists. Can be used to determine if login or register is the right method.
53+
*
54+
* @return true, if the file was previously created
55+
*/
56+
def credentialsFileExists(): Boolean = new File(credentialsFilePath).exists()
57+
5158
/**
5259
* Loads the credentials form the credentials file and decrypts them.
5360
*

src/main/scala/org/codeoverflow/chatoverflow/connector/Connector.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ abstract class Connector(val sourceIdentifier: String) extends WithLogger {
3434
*/
3535
def setCredentials(credentials: Credentials): Unit = this.credentials = Some(credentials)
3636

37+
/**
38+
* Removes the entire credentials object from the connector.
39+
*/
40+
def removeCredentials(): Unit = this.credentials = None
41+
3742
def setCredentialsValue(key: String, value: String): Boolean = {
3843
if (credentials.isEmpty) false else {
3944
if (credentials.get.exists(key)) {

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
@@ -51,5 +51,31 @@ class ConfigController(implicit val swagger: Swagger) extends JsonServlet with C
5151
}
5252
}
5353

54-
// TODO: Handle first time login / Creation of the config files (same as postLogin above...?)
54+
get("/register", operation(getRegister)) {
55+
chatOverflow.credentialsService.credentialsFileExists()
56+
}
57+
58+
post("/register", operation(postRegister)) {
59+
parsedAs[Password] {
60+
case Password(password) =>
61+
62+
if (chatOverflow.credentialsService.credentialsFileExists()) {
63+
ResultMessage(success = false, "Already registered.")
64+
65+
// Extreme exotic case. Wtf?
66+
} else if (chatOverflow.isLoaded) {
67+
ResultMessage(success = false, "Framework already loaded.")
68+
69+
} else {
70+
chatOverflow.credentialsService.setPassword(password.toCharArray)
71+
72+
if (!chatOverflow.load()) {
73+
ResultMessage(success = false, "Unable to load.")
74+
} else {
75+
76+
ResultMessage(success = true, CryptoUtil.generateAuthKey(password))
77+
}
78+
}
79+
}
80+
}
5581
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,17 @@ trait ConfigControllerDefinition extends SwaggerSupport {
2727
description "Tries to decrypt the credentials using the provided password. If already loaded, does only return the communication auth key."
2828
parameter bodyParam[Password]("body").description("Requires the user framework password. The auth key is based on this input."))
2929

30+
val getRegister: OperationBuilder =
31+
(apiOperation[Boolean]("getRegister")
32+
summary "Returns if a credentials file has been created and thus a specific password been registered."
33+
description "Returns true, if the credentials file (with set password) already exists.")
34+
35+
val postRegister: OperationBuilder =
36+
(apiOperation[ResultMessage]("postRegister")
37+
summary "Registers the user with the given password. Can only be called if there is no credentials file."
38+
description "Creates a credentials file with the given password. Acts like post(login) after this (returning an auth key)."
39+
parameter bodyParam[Password]("body").description("Requires the user framework password. The auth key is based on this input."))
40+
41+
3042
override protected def applicationDescription: String = "Handles configuration and information retrieval."
3143
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/connector/ConnectorController.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.codeoverflow.chatoverflow.ui.web.rest.connector
22

3+
import org.codeoverflow.chatoverflow.configuration.Credentials
34
import org.codeoverflow.chatoverflow.connector.ConnectorRegistry
45
import org.codeoverflow.chatoverflow.ui.web.JsonServlet
56
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConnectorDetails, ConnectorRef, ResultMessage}
@@ -23,7 +24,6 @@ class ConnectorController(implicit val swagger: Swagger) extends JsonServlet wit
2324
}
2425

2526
post("/", operation(postConnector)) {
26-
// TODO: Should implicitly create the credentials object
2727
parsedAs[ConnectorRef] {
2828
case ConnectorRef(sourceIdentifier, uniqueTypeString) =>
2929
val connector = ConnectorRegistry.getConnector(sourceIdentifier, uniqueTypeString)
@@ -40,15 +40,21 @@ class ConnectorController(implicit val swagger: Swagger) extends JsonServlet wit
4040
ResultMessage(success = false, "Unable to add connector.")
4141

4242
} else {
43-
chatOverflow.save()
44-
ResultMessage(success = true)
43+
44+
val credentials = new Credentials(sourceIdentifier)
45+
if (!ConnectorRegistry.setConnectorCredentials(sourceIdentifier, uniqueTypeString, credentials)) {
46+
ResultMessage(success = false, "Unable to create credentials.")
47+
48+
} else {
49+
chatOverflow.save()
50+
ResultMessage(success = true)
51+
}
4552
}
4653
}
4754
}
4855
}
4956

5057
delete("/:sourceIdentifier/:qualifiedConnectorType", operation(deleteConnector)) {
51-
// TODO: Should implicitly delete the credentials object
5258
val sourceIdentifier = params("sourceIdentifier")
5359
val qualifiedConnectorType = params("qualifiedConnectorType")
5460

@@ -64,6 +70,7 @@ class ConnectorController(implicit val swagger: Swagger) extends JsonServlet wit
6470
ResultMessage(success = false, "Unable to remove connector.")
6571

6672
} else {
73+
connector.get.removeCredentials()
6774
chatOverflow.save()
6875
ResultMessage(success = true)
6976
}

0 commit comments

Comments
 (0)