- 
                Notifications
    You must be signed in to change notification settings 
- Fork 62
Add Plivo as SMS Provider #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            cdjenkins
  wants to merge
  15
  commits into
  nextcloud:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
cdjenkins:master
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 13 commits
      Commits
    
    
            Show all changes
          
          
            15 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      dab96f0
              
                1st draft impl of Plivo SMS provider
              
              
                cdjenkins 6545e7e
              
                finish Plivo implementation
              
              
                cdjenkins 2d44d85
              
                fix compilation issues
              
              
                cdjenkins 8cd0c96
              
                add plivo to documentation
              
              
                cdjenkins d22c44d
              
                fix indentation. sort providers alphabetically.
              
              
                cdjenkins 0e99191
              
                improve use of variable inside string
              
              
                cdjenkins 83cb8b4
              
                fix bug. referenced const array incorrectly
              
              
                cdjenkins 16c9729
              
                fix bug. calling wrong functions
              
              
                cdjenkins 209cb8a
              
                add logger. post doesn't work, but get does on IClient
              
              
                cdjenkins f126375
              
                fix bug. don't pass 'url' param to plivo unless you have a valid url
              
              
                cdjenkins a614378
              
                remove debug flag. successfully receive 2fa code over sms from plivo
              
              
                cdjenkins 0e89632
              
                remove callback url to reduce complexity of app
              
              
                cdjenkins 72c1e25
              
                use class constants with clas name. improve logging. use multi-line a…
              
              
                cdjenkins 8d4e305
              
                fix php formatting
              
              
                cdjenkins 44fd17e
              
                Merge remote-tracking branch 'origin/master'
              
              
                cdjenkins File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?php | ||
|  | ||
| declare(strict_types=1); | ||
|  | ||
| /** | ||
| * @author Chris Jenkins <chris.j@guruinventions.com> | ||
| * | ||
| * Plivo - Config for Two-factor Gateway for Plivo | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|  | ||
| namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; | ||
|  | ||
| use Exception; | ||
| use OCA\TwoFactorGateway\Exception\SmsTransmissionException; | ||
| use OCP\Http\Client\IClient; | ||
| use OCP\Http\Client\IClientService; | ||
| use \OCP\ILogger; | ||
|  | ||
| class Plivo implements IProvider { | ||
| public const PROVIDER_ID = 'plivo'; | ||
|  | ||
| /** @var IClient */ | ||
| private $client; | ||
|  | ||
| /** @var PlivoConfig */ | ||
| private $config; | ||
|  | ||
| private $logger; | ||
|  | ||
| public function __construct(IClientService $clientService, | ||
| PlivoConfig $config, ILogger $logger) { | ||
| $this->client = $clientService->newClient(); | ||
| $this->config = $config; | ||
| $this->logger = $logger; | ||
| } | ||
|  | ||
| /** | ||
| * @param string $identifier | ||
| * @param string $message | ||
| * | ||
| * @throws SmsTransmissionException | ||
| */ | ||
| public function send(string $identifier, string $message) { | ||
| $config = $this->getConfig(); | ||
| $authToken = $config->getValue($config::AUTH_TOKEN_KEY); | ||
| $authID = $config->getValue($config::AUTH_ID_KEY); | ||
| $srcNumber = $config->getValue($config::SRC_NUMBER_KEY); | ||
|  | ||
| $apiParams = [ | ||
| 'body' => json_encode([ | ||
| 'dst' => $identifier, | ||
| 'src' => $srcNumber, | ||
| 'text' => $message | ||
| ],JSON_FORCE_OBJECT), | ||
| 'headers' => [ | ||
| 'Content-Type' => "application/json", | ||
| 'Authorization' => "Basic " . base64_encode($authID.':'.$authToken) | ||
| ] | ||
| ]; | ||
|  | ||
| try { | ||
| $this->logger->debug("api call: https://api.plivo.com/v1/Account/$authID/Message/" .print_r($apiParams,true)); | ||
| $this->client->post("https://api.plivo.com/v1/Account/$authID/Message/", $apiParams); | ||
| } catch (Exception $ex) { | ||
| $this->logger->logException($ex, [ | ||
| 'message' => 'Could not send Plivo message: ' . $ex->getMessage(), | ||
| ]); | ||
| throw new SmsTransmissionException(); | ||
| } | ||
| } | ||
|  | ||
| /** | ||
| * @return PlivoConfig | ||
| */ | ||
| public function getConfig(): IProviderConfig { | ||
| return $this->config; | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|  | ||
| declare(strict_types=1); | ||
|  | ||
| /** | ||
| * @author Chris Jenkins <chris.j@guruinventions.com> | ||
| * | ||
| * Plivo - Config for Two-factor Gateway for Plivo | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|  | ||
| namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; | ||
|  | ||
| use function array_intersect; | ||
| use OCA\TwoFactorGateway\AppInfo\Application; | ||
| use OCA\TwoFactorGateway\Exception\ConfigurationException; | ||
| use OCP\IConfig; | ||
|  | ||
| class PlivoConfig implements IProviderConfig { | ||
|  | ||
| /** @var IConfig */ | ||
| private $config; | ||
|  | ||
| public const AUTH_ID_KEY = 'plivo_auth_id'; | ||
| public const AUTH_TOKEN_KEY = 'plivo_auth_token'; | ||
| public const SRC_NUMBER_KEY = 'plivo_src_number'; | ||
|  | ||
| private const EXPECTED_KEYS = [ | ||
| self::AUTH_ID_KEY, | ||
| self::AUTH_TOKEN_KEY, | ||
| self::SRC_NUMBER_KEY | ||
| ]; | ||
|  | ||
| public function __construct(IConfig $config) { | ||
| $this->config = $config; | ||
| } | ||
|  | ||
| private function getInternalValue(string $key): string { | ||
| $val = $this->config->getAppValue(Application::APP_NAME, $key, null); | ||
| if (is_null($val)) { | ||
| throw new ConfigurationException(); | ||
| } | ||
| return $val; | ||
| } | ||
|  | ||
| public function getValue(string $key): string { | ||
| return $this->getInternalValue($key); | ||
| } | ||
|  | ||
| public function setValue(string $key, string $value) { | ||
| $this->config->setAppValue(Application::APP_NAME, $key, $value); | ||
| } | ||
|  | ||
| public function isComplete(): bool { | ||
| $set = $this->config->getAppKeys(Application::APP_NAME); | ||
| return count(array_intersect($set,self::EXPECTED_KEYS)) === count(self::EXPECTED_KEYS); | ||
| } | ||
|  | ||
| public function remove() { | ||
| foreach (self::EXPECTED_KEYS as $key) { | ||
| $this->config->deleteAppValue(Application::APP_NAME, $key); | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.