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

Commit 87f5e69

Browse files
committed
Added encrypted credentials retrieval to the rest api.
1 parent 090b855 commit 87f5e69

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ object DTOs {
2424
areCredentialsSet: Boolean, isRunning: Boolean, requiredCredentialKeys: Seq[String],
2525
optionalCredentialKeys: Seq[String])
2626

27+
case class CredentialsDetails(found: Boolean, requiredCredentials: Map[String, String] = Map[String, String](),
28+
optionalCredentials: Map[String, String] = Map[String, String]())
29+
2730
case class PluginInstanceRef(instanceName: String, pluginName: String, pluginAuthor: String)
2831

2932
case class ResultMessage(success: Boolean, message: String = "")

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

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

3-
import org.codeoverflow.chatoverflow.configuration.Credentials
3+
import org.codeoverflow.chatoverflow.configuration.{Credentials, CryptoUtil}
44
import org.codeoverflow.chatoverflow.connector.ConnectorRegistry
55
import org.codeoverflow.chatoverflow.ui.web.JsonServlet
6-
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConnectorDetails, ConnectorRef, ResultMessage}
6+
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConnectorDetails, ConnectorRef, CredentialsDetails, ResultMessage}
77
import org.scalatra.swagger.Swagger
88

9+
import scala.collection.mutable
10+
911
class ConnectorController(implicit val swagger: Swagger) extends JsonServlet with ConnectorControllerDefinition {
1012

1113
get("/", operation(getConnectors)) {
@@ -76,4 +78,44 @@ class ConnectorController(implicit val swagger: Swagger) extends JsonServlet wit
7678
}
7779
}
7880

81+
// TODO: Get one encrypted entry, post, delete
82+
83+
get("/:sourceIdentifier/:qualifiedConnectorType/credentials", operation(getCredentials)) {
84+
if (!chatOverflow.isLoaded) {
85+
CredentialsDetails(found = false)
86+
} else {
87+
val connector = ConnectorRegistry.getConnector(params("sourceIdentifier"), params("qualifiedConnectorType"))
88+
89+
if (connector.isEmpty) {
90+
CredentialsDetails(found = false)
91+
92+
} else if (!connector.get.areCredentialsSet) {
93+
CredentialsDetails(found = false)
94+
95+
} else {
96+
97+
val requiredCredentials = getCredentialsMap(connector.get.getRequiredCredentialKeys, connector.get.getCredentials.get)
98+
val optionalCredentials = getCredentialsMap(connector.get.getOptionalCredentialKeys, connector.get.getCredentials.get)
99+
100+
CredentialsDetails(found = true, requiredCredentials, optionalCredentials)
101+
}
102+
}
103+
}
104+
105+
protected def getCredentialsMap(keys: List[String], credentials: Credentials): Map[String, String] = {
106+
val authKey = chatOverflow.credentialsService.generateAuthKey()
107+
val credentialsMap = mutable.Map[String, String]()
108+
109+
for (key <- keys) {
110+
if (credentials.exists(key)) {
111+
val plainValue = credentials.getValue(key).get
112+
val encryptedValue = CryptoUtil.encryptSSLcompliant(authKey, plainValue)
113+
114+
credentialsMap += key -> encryptedValue
115+
}
116+
}
117+
118+
credentialsMap.toMap
119+
}
120+
79121
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ trait ConnectorControllerDefinition extends SwaggerSupport {
3232
parameter pathParam[String]("sourceIdentifier").description("The (connector unique) identifier of e.g. a account to connect to")
3333
parameter pathParam[String]("qualifiedConnectorType").description("The fully qualified type of the connector."))
3434

35+
val getCredentials: OperationBuilder =
36+
(apiOperation[ConnectorDetails]("getCredentials")
37+
summary "Shows all credentials for a specified connector."
38+
description "Shows required and optional credentials. Note, that the user has to be logged in and the values are encrypted using the auth key. "
39+
parameter pathParam[String]("sourceIdentifier").description("The (connector unique) identifier of e.g. a account to connect to")
40+
parameter pathParam[String]("qualifiedConnectorType").description("The fully qualified type of the connector."))
41+
42+
3543
override protected def applicationDescription: String = "Handles platform connectors."
3644

3745

0 commit comments

Comments
 (0)