Page MenuHomeWolfplex

No OneTemporary

diff --git a/_includes/SecurityData.php b/_includes/SecurityData.php
new file mode 100644
index 0000000..db04cd9
--- /dev/null
+++ b/_includes/SecurityData.php
@@ -0,0 +1,107 @@
+<?php
+
+/**
+ * This class provides methods to get credentials and security datas used by our API.
+ */
+class SecurityData {
+ /**
+ * The prefix of environment variables containing security information
+ * @var string
+ */
+ const ENV_PREFIX = 'CREDENTIAL_';
+
+ /**
+ * Name of the secret name containing the name of the files to store other secrets
+ * @var string
+ */
+ const SECRET_FILE = 'path.datasources.securitydata';
+
+ /**
+ * Gets a specified secret
+ *
+ * @param string $secretName the secret's name
+ * @return string the secret value
+ *
+ */
+ public static function getSecret ($secretName) {
+ $methods = [ 'getSecretFromEnvironment', 'getSecretFromFile' ];
+ foreach ($methods as $method) {
+ $secretValue = static::$method($secretName);
+ if ($secretValue !== "") {
+ return $secretValue;
+ }
+ }
+
+ return "";
+ }
+
+
+ /**
+ * Gets a specified secret from environment
+ *
+ * @param string $secretName the secret's name
+ * @return string the secret value
+ *
+ */
+ public static function getSecretFromFile ($secretName) {
+ $file = static::getSecretFilePath();
+ if (file_exists($file)) {
+ $data = file_get_contents($file);
+ $bagOfSecrets = json_decode($data);
+ if ($bagOfSecrets && is_object($bagOfSecrets) && property_exists($bagOfSecrets, $secretName)) {
+ return $bagOfSecrets->$secretName;
+ }
+ }
+ return "";
+ }
+
+ /**
+ * Gets secret file path
+ *
+ * @return string the path to the file containing secrets
+ */
+ public static function getSecretFilePath () {
+ return static::getSecretFromEnvironment(static::SECRET_FILE);
+ }
+
+ /**
+ * Gets a specified secret from environment
+ *
+ * @param string $secretName the secret's name
+ * @return string the secret value
+ *
+ */
+ public static function getSecretFromObject ($object, $secretName) {
+ if (property_exists($object, $secretName)) {
+ return $object->$secretName;
+ }
+ return "";
+ }
+
+ /**
+ * Gets a specified secret from environment
+ *
+ * @param string $secretName the secret's name
+ * @return string the secret value
+ *
+ */
+ public static function getSecretFromEnvironment ($secretName) {
+ $environmentVariable = static::getEnvironmentVariableName($secretName);
+ $secretValue = getenv($environmentVariable);
+ if ($secretValue === false) {
+ return "";
+ }
+ return $secretValue;
+ }
+
+ /**
+ * Gets environment variable name from secret name
+ *
+ * @param string $secretName the secret's name
+ * @return string the environment variable
+ */
+ public static function getEnvironmentVariableName ($secretName) {
+ $variableName = str_replace('.', '_', $secretName);
+ return static::ENV_PREFIX . strtoupper($variableName);
+ }
+}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Jun 9, 12:14 PM (1 w, 3 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
46404
Default Alt Text
(3 KB)

Event Timeline