|
1 | 1 | package org.codeoverflow.chatoverflow.ui.web.rest.connector |
2 | 2 |
|
3 | | -import org.codeoverflow.chatoverflow.configuration.Credentials |
| 3 | +import org.codeoverflow.chatoverflow.configuration.{Credentials, CryptoUtil} |
4 | 4 | import org.codeoverflow.chatoverflow.connector.ConnectorRegistry |
5 | 5 | 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} |
7 | 7 | import org.scalatra.swagger.Swagger |
8 | 8 |
|
| 9 | +import scala.collection.mutable |
| 10 | + |
9 | 11 | class ConnectorController(implicit val swagger: Swagger) extends JsonServlet with ConnectorControllerDefinition { |
10 | 12 |
|
11 | 13 | get("/", operation(getConnectors)) { |
@@ -76,4 +78,44 @@ class ConnectorController(implicit val swagger: Swagger) extends JsonServlet wit |
76 | 78 | } |
77 | 79 | } |
78 | 80 |
|
| 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 | + |
79 | 121 | } |
0 commit comments