1515use OCP \AppFramework \Db \DoesNotExistException ;
1616use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
1717use OCP \DB \Exception ;
18+ use OCP \Security \ICrypto ;
1819use Psr \Log \LoggerInterface ;
1920
2021/**
@@ -25,15 +26,25 @@ class ExAppConfigService {
2526 public function __construct (
2627 private ExAppConfigMapper $ mapper ,
2728 private LoggerInterface $ logger ,
29+ private ICrypto $ crypto ,
2830 ) {
2931 }
3032
3133 public function getAppConfigValues (string $ appId , array $ configKeys ): ?array {
3234 try {
3335 return array_map (function (ExAppConfig $ exAppConfig ) {
36+ $ value = $ exAppConfig ->getConfigvalue () ?? '' ;
37+ if ($ value !== '' && $ exAppConfig ->getSensitive ()) {
38+ try {
39+ $ value = $ this ->crypto ->decrypt ($ value );
40+ } catch (\Exception $ e ) {
41+ $ this ->logger ->warning (sprintf ('Failed to decrypt sensitive value for app %s, config key %s ' , $ exAppConfig ->getAppid (), $ exAppConfig ->getConfigkey ()), ['exception ' => $ e ]);
42+ $ value = '' ;
43+ }
44+ }
3445 return [
3546 'configkey ' => $ exAppConfig ->getConfigkey (),
36- 'configvalue ' => $ exAppConfig -> getConfigvalue () ?? '' ,
47+ 'configvalue ' => $ value ,
3748 ];
3849 }, $ this ->mapper ->findByAppConfigKeys ($ appId , $ configKeys ));
3950 } catch (Exception ) {
@@ -43,20 +54,30 @@ public function getAppConfigValues(string $appId, array $configKeys): ?array {
4354
4455 public function setAppConfigValue (string $ appId , string $ configKey , mixed $ configValue , ?int $ sensitive = null ): ?ExAppConfig {
4556 $ appConfigEx = $ this ->getAppConfig ($ appId , $ configKey );
57+ if ($ configValue !== '' && $ sensitive ) {
58+ try {
59+ $ encryptedValue = $ this ->crypto ->encrypt ($ configValue );
60+ } catch (\Exception $ e ) {
61+ $ this ->logger ->error (sprintf ('Failed to encrypt sensitive value for app %s, config key %s. Error: %s ' , $ appId , $ configKey , $ e ->getMessage ()), ['exception ' => $ e ]);
62+ return null ;
63+ }
64+ } else {
65+ $ encryptedValue = '' ;
66+ }
4667 if ($ appConfigEx === null ) {
4768 try {
4869 $ appConfigEx = $ this ->mapper ->insert (new ExAppConfig ([
4970 'appid ' => $ appId ,
5071 'configkey ' => $ configKey ,
51- 'configvalue ' => $ configValue ?? '' ,
72+ 'configvalue ' => $ sensitive ? $ encryptedValue : $ configValue ?? '' ,
5273 'sensitive ' => $ sensitive ?? 0 ,
5374 ]));
5475 } catch (Exception $ e ) {
5576 $ this ->logger ->error (sprintf ('Failed to insert appconfig_ex value. Error: %s ' , $ e ->getMessage ()), ['exception ' => $ e ]);
5677 return null ;
5778 }
5879 } else {
59- $ appConfigEx ->setConfigvalue ($ configValue );
80+ $ appConfigEx ->setConfigvalue ($ sensitive ? $ encryptedValue : $ configValue );
6081 if ($ sensitive !== null ) {
6182 $ appConfigEx ->setSensitive ($ sensitive );
6283 }
@@ -65,6 +86,10 @@ public function setAppConfigValue(string $appId, string $configKey, mixed $confi
6586 return null ;
6687 }
6788 }
89+ if ($ sensitive ) {
90+ // setting original unencrypted value for API
91+ $ appConfigEx ->setConfigvalue ($ configValue );
92+ }
6893 return $ appConfigEx ;
6994 }
7095
0 commit comments