flipboxfactory /
keychain
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: dsmrt |
||
| 5 | * Date: 1/12/18 |
||
| 6 | * Time: 10:44 PM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace flipbox\keychain\services; |
||
| 10 | |||
| 11 | use craft\base\Component; |
||
| 12 | use craft\base\Plugin; |
||
| 13 | use flipbox\keychain\KeyChain; |
||
| 14 | use flipbox\keychain\keypair\KeyPairInterface; |
||
| 15 | use flipbox\keychain\keypair\OpenSSL; |
||
| 16 | use flipbox\keychain\records\KeyChainRecord; |
||
| 17 | use yii\db\ActiveQuery; |
||
| 18 | |||
| 19 | class KeyChainService extends Component |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param Plugin $plugin |
||
| 24 | * @return ActiveQuery |
||
| 25 | */ |
||
| 26 | public function findByPlugin(Plugin $plugin) |
||
| 27 | { |
||
| 28 | return KeyChainRecord::find()->where([ |
||
| 29 | 'pluginHandle' => $plugin->handle, |
||
| 30 | ]); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param KeyChainRecord $keyChainRecord |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | public function save(KeyChainRecord $keyChainRecord, $runValidation = true, $attributeNames = null) |
||
|
0 ignored issues
–
show
|
|||
| 38 | { |
||
| 39 | if (! $runValidation && $keyChainRecord->validate()) { |
||
| 40 | return false; |
||
| 41 | } |
||
| 42 | |||
| 43 | return $keyChainRecord->save(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param KeyChainRecord $keyChainRecord |
||
| 48 | * @return false|int |
||
| 49 | * @throws \Throwable |
||
| 50 | * @throws \yii\db\StaleObjectException |
||
| 51 | */ |
||
| 52 | public function delete(KeyChainRecord $keyChainRecord) |
||
| 53 | { |
||
| 54 | return $keyChainRecord->delete(); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param KeyPairInterface $keyPairConfig |
||
| 59 | * @return KeyChainRecord |
||
| 60 | */ |
||
| 61 | public function create(KeyPairInterface $keyPairConfig) |
||
| 62 | { |
||
| 63 | return $keyPairConfig->create(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param array $config |
||
| 68 | * @return KeyChainRecord |
||
| 69 | */ |
||
| 70 | public function generateOpenssl($config = []) |
||
| 71 | { |
||
| 72 | /** |
||
| 73 | * Create the key pair using the defaults |
||
| 74 | */ |
||
| 75 | $openssl = new OpenSSL( |
||
| 76 | KeyChain::getInstance()->getSettings()->opensslDefaults |
||
| 77 | ); |
||
| 78 | $keyPair = $openssl->create(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * default this to false. |
||
| 82 | */ |
||
| 83 | $keyPair->isEncrypted = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Merge in any configs passed |
||
| 87 | */ |
||
| 88 | \Craft::configure($keyPair, $config); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Save |
||
| 92 | */ |
||
| 93 | $this->save($keyPair); |
||
| 94 | |||
| 95 | return $keyPair; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param KeyChainRecord $record |
||
| 100 | * @throws \yii\base\Exception |
||
| 101 | * @throws \yii\base\InvalidConfigException |
||
| 102 | */ |
||
| 103 | public function decrypt(KeyChainRecord $record) |
||
| 104 | { |
||
| 105 | |||
| 106 | if ($record->isEncrypted && ! $record->isDecrypted) { |
||
| 107 | $record->decryptedKey = \Craft::$app->getSecurity()->decryptByKey( |
||
| 108 | base64_decode($record->key), |
||
| 109 | \Craft::$app->getConfig()->getGeneral()->securityKey |
||
| 110 | ); |
||
| 111 | $record->decryptedCertificate = \Craft::$app->getSecurity()->decryptByKey( |
||
| 112 | base64_decode($record->certificate), |
||
| 113 | \Craft::$app->getConfig()->getGeneral()->securityKey |
||
| 114 | ); |
||
| 115 | } else { |
||
| 116 | $record->decryptedKey = $record->key; |
||
| 117 | $record->decryptedCertificate = $record->certificate; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param KeyChainRecord $record |
||
| 123 | * @throws \yii\base\Exception |
||
| 124 | * @throws \yii\base\InvalidConfigException |
||
| 125 | */ |
||
| 126 | public function encrypt(KeyChainRecord $record) |
||
| 127 | { |
||
| 128 | |||
| 129 | if ($record->isEncrypted && $record->isDecrypted) { |
||
| 130 | /** |
||
| 131 | * Encrypt data at rest |
||
| 132 | */ |
||
| 133 | $record->key = base64_encode(\Craft::$app->getSecurity()->encryptByKey( |
||
| 134 | $record->key, |
||
| 135 | \Craft::$app->getConfig()->getGeneral()->securityKey |
||
| 136 | )); |
||
| 137 | $record->certificate = base64_encode(\Craft::$app->getSecurity()->encryptByKey( |
||
| 138 | $record->certificate, |
||
| 139 | \Craft::$app->getConfig()->getGeneral()->securityKey |
||
| 140 | )); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.