mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Move OAuth module from API to common and solve PHPStan's errors
This commit is contained in:
		
							
								
								
									
										33
									
								
								common/components/OAuth2/CryptTrait.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								common/components/OAuth2/CryptTrait.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
<?php
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace common\components\OAuth2;
 | 
			
		||||
 | 
			
		||||
use LogicException;
 | 
			
		||||
use RangeException;
 | 
			
		||||
use SodiumException;
 | 
			
		||||
use Yii;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This trait is intended to override the standard data encryption behavior
 | 
			
		||||
 * with the help of \Defuse\Crypto\Crypto class, because the resultant string
 | 
			
		||||
 * is much larger than the original one.
 | 
			
		||||
 *
 | 
			
		||||
 * The implementation under the hood relies on using libsodium library
 | 
			
		||||
 * that provides more compact result values.
 | 
			
		||||
 */
 | 
			
		||||
trait CryptTrait {
 | 
			
		||||
 | 
			
		||||
    protected function encrypt(string $unencryptedData): string {
 | 
			
		||||
        return Yii::$app->tokens->encryptValue($unencryptedData);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function decrypt(string $encryptedData): string {
 | 
			
		||||
        try {
 | 
			
		||||
            return Yii::$app->tokens->decryptValue($encryptedData);
 | 
			
		||||
        } catch (SodiumException|RangeException $e) {
 | 
			
		||||
            throw new LogicException($e->getMessage(), 0, $e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user