rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

ClientConfig.php (9078B)


      1 <?php
      2 
      3 /**
      4  *  This program is free software: you can redistribute it and/or modify
      5  *  it under the terms of the GNU Lesser General Public License as published by
      6  *  the Free Software Foundation, either version 3 of the License, or
      7  *  (at your option) any later version.
      8  *
      9  *  This program is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  *  GNU Lesser General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU Lesser General Public License
     15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 namespace fkooman\OAuth\Client;
     19 
     20 use fkooman\OAuth\Client\Exception\ClientConfigException;
     21 
     22 class ClientConfig implements ClientConfigInterface
     23 {
     24     // VSCHAR     = %x20-7E
     25     const REGEXP_VSCHAR = '/^(?:[\x20-\x7E])*$/';
     26 
     27     private $clientId;
     28     private $authorizeEndpoint;
     29     private $tokenEndpoint;
     30     private $clientSecret;
     31     private $redirectUri;
     32     private $credentialsInRequestBody;
     33     private $defaultTokenType;
     34     private $allowNullExpiresIn;
     35     private $useCommaSeparatedScope;
     36     private $useArrayScope;
     37     private $enableDebug;
     38     private $defaultServerScope;
     39     private $useRedirectUriOnRefreshTokenRequest;
     40     private $allowStringExpiresIn;
     41 
     42     public function __construct(array $data)
     43     {
     44         foreach (array('client_id', 'authorize_endpoint', 'token_endpoint') as $key) {
     45             if (!array_key_exists($key, $data)) {
     46                 throw new ClientConfigException(sprintf("missing field '%s'", $key));
     47             }
     48         }
     49         $this->setClientId($data['client_id']);
     50         $this->setAuthorizeEndpoint($data['authorize_endpoint']);
     51         $this->setTokenEndpoint($data['token_endpoint']);
     52 
     53         $clientSecret = array_key_exists('client_secret', $data) ? $data['client_secret'] : null;
     54         $this->setClientSecret($clientSecret);
     55 
     56         $redirectUri = array_key_exists('redirect_uri', $data) ? $data['redirect_uri'] : null;
     57         $this->setRedirectUri($redirectUri);
     58 
     59         $credentialsInRequestBody = array_key_exists('credentials_in_request_body', $data) ? $data['credentials_in_request_body'] : false;
     60         $this->setCredentialsInRequestBody($credentialsInRequestBody);
     61 
     62         $defaultTokenType = array_key_exists('default_token_type', $data) ? $data['default_token_type'] : null;
     63         $this->setDefaultTokenType($defaultTokenType);
     64 
     65         $allowNullExpiresIn = array_key_exists('allow_null_expires_in', $data) ? $data['allow_null_expires_in'] : false;
     66         $this->setAllowNullExpiresIn($allowNullExpiresIn);
     67 
     68         $useRedirectUriOnRefreshTokenRequest = array_key_exists('use_redirect_uri_on_refresh_token_request', $data) ? $data['use_redirect_uri_on_refresh_token_request'] : false;
     69         $this->setUseRedirectUriOnRefreshTokenRequest($useRedirectUriOnRefreshTokenRequest);
     70 
     71         $defaultServerScope = array_key_exists('default_server_scope', $data) ? $data['default_server_scope'] : null;
     72         $this->setDefaultServerScope($defaultServerScope);
     73 
     74         $useCommaSeparatedScope = array_key_exists('use_comma_separated_scope', $data) ? $data['use_comma_separated_scope'] : null;
     75         $this->setUseCommaSeparatedScope($useCommaSeparatedScope);
     76 
     77         $useArrayScope = array_key_exists('use_array_scope', $data) ? $data['use_array_scope'] : null;
     78         $this->setUseArrayScope($useArrayScope);
     79 
     80         $enableDebug = array_key_exists('enable_debug', $data) ? $data['enable_debug'] : false;
     81         $this->setEnableDebug($enableDebug);
     82 
     83         $allowStringExpiresIn = array_key_exists('allow_string_expires_in', $data) ? $data['allow_string_expires_in'] : false;
     84         $this->setAllowStringExpiresIn($allowStringExpiresIn);
     85     }
     86 
     87     public function setClientId($clientId)
     88     {
     89         if (!is_string($clientId) || 0 >= strlen($clientId)) {
     90             throw new ClientConfigException("client_id must be a non-empty string");
     91         }
     92         $this->validateUserPass($clientId);
     93         $this->clientId = $clientId;
     94     }
     95 
     96     public function getClientId()
     97     {
     98         return $this->clientId;
     99     }
    100 
    101     public function setClientSecret($clientSecret)
    102     {
    103         if (null !== $clientSecret) {
    104             if (!is_string($clientSecret) || 0 >= strlen($clientSecret)) {
    105                 throw new ClientConfigException("client_secret must be a non-empty string or null");
    106             }
    107             $this->validateUserPass($clientSecret);
    108         }
    109         $this->clientSecret = $clientSecret;
    110     }
    111 
    112     public function getClientSecret()
    113     {
    114         return $this->clientSecret;
    115     }
    116 
    117     public function setAuthorizeEndpoint($authorizeEndpoint)
    118     {
    119         $this->validateEndpointUri($authorizeEndpoint);
    120         $this->authorizeEndpoint = $authorizeEndpoint;
    121     }
    122 
    123     public function getAuthorizeEndpoint()
    124     {
    125         return $this->authorizeEndpoint;
    126     }
    127 
    128     public function setTokenEndpoint($tokenEndpoint)
    129     {
    130         $this->validateEndpointUri($tokenEndpoint);
    131         $this->tokenEndpoint = $tokenEndpoint;
    132     }
    133 
    134     public function getTokenEndpoint()
    135     {
    136         return $this->tokenEndpoint;
    137     }
    138 
    139     public function setRedirectUri($redirectUri)
    140     {
    141         if (null !== $redirectUri) {
    142             $this->validateEndpointUri($redirectUri);
    143         }
    144         $this->redirectUri = $redirectUri;
    145     }
    146 
    147     public function getRedirectUri()
    148     {
    149         return $this->redirectUri;
    150     }
    151 
    152     public function setCredentialsInRequestBody($credentialsInRequestBody)
    153     {
    154         $this->credentialsInRequestBody = (bool) $credentialsInRequestBody;
    155     }
    156 
    157     public function getCredentialsInRequestBody()
    158     {
    159         return $this->credentialsInRequestBody;
    160     }
    161 
    162     public function setDefaultTokenType($defaultTokenType)
    163     {
    164         if (null !== $defaultTokenType) {
    165             if (!is_string($defaultTokenType) || 0 >= strlen($defaultTokenType)) {
    166                 throw new ClientConfigException("default_token_type must be a non-empty string or null");
    167             }
    168         }
    169         $this->defaultTokenType = $defaultTokenType;
    170     }
    171 
    172     public function getDefaultTokenType()
    173     {
    174         return $this->defaultTokenType;
    175     }
    176 
    177     public function setDefaultServerScope($defaultServerScope)
    178     {
    179         if (null !== $defaultServerScope) {
    180             if (!is_string($defaultServerScope) || 0 >= strlen($defaultServerScope)) {
    181                 throw new ClientConfigException("default_server_scope must be a non-empty string or null");
    182             }
    183         }
    184         $this->defaultServerScope = $defaultServerScope;
    185     }
    186 
    187     public function getDefaultServerScope()
    188     {
    189         return $this->defaultServerScope;
    190     }
    191 
    192     public function setAllowNullExpiresIn($allowNullExpiresIn)
    193     {
    194         $this->allowNullExpiresIn = (bool) $allowNullExpiresIn;
    195     }
    196 
    197     public function getAllowNullExpiresIn()
    198     {
    199         return $this->allowNullExpiresIn;
    200     }
    201 
    202     public function setUseRedirectUriOnRefreshTokenRequest($useRedirectUriOnRefreshTokenRequest)
    203     {
    204         $this->useRedirectUriOnRefreshTokenRequest = (bool) $useRedirectUriOnRefreshTokenRequest;
    205     }
    206 
    207     public function getUseRedirectUriOnRefreshTokenRequest()
    208     {
    209         return $this->useRedirectUriOnRefreshTokenRequest;
    210     }
    211 
    212     public function setUseCommaSeparatedScope($useCommaSeparatedScope)
    213     {
    214         $this->useCommaSeparatedScope = (bool) $useCommaSeparatedScope;
    215     }
    216 
    217     public function getUseCommaSeparatedScope()
    218     {
    219         return $this->useCommaSeparatedScope;
    220     }
    221 
    222     public function setUseArrayScope($useArrayScope)
    223     {
    224         $this->useArrayScope = (bool) $useArrayScope;
    225     }
    226 
    227     public function getUseArrayScope()
    228     {
    229         return $this->useArrayScope;
    230     }
    231 
    232     public function setAllowStringExpiresIn($allowStringExpiresIn)
    233     {
    234         $this->allowStringExpiresIn = (bool) $allowStringExpiresIn;
    235     }
    236 
    237     public function getAllowStringExpiresIn()
    238     {
    239         return $this->allowStringExpiresIn;
    240     }
    241 
    242     public function setEnableDebug($enableDebug)
    243     {
    244         $this->enableDebug = (bool) $enableDebug;
    245     }
    246 
    247     public function getEnableDebug()
    248     {
    249         return $this->enableDebug;
    250     }
    251 
    252     private function validateUserPass($userPass)
    253     {
    254         if (1 !== preg_match(self::REGEXP_VSCHAR, $userPass)) {
    255             throw new ClientConfigException("invalid characters in client_id or client_secret");
    256         }
    257     }
    258 
    259     private function validateEndpointUri($endpointUri)
    260     {
    261         if (!is_string($endpointUri) || 0 >= strlen($endpointUri)) {
    262             throw new ClientConfigException("uri must be a non-empty string");
    263         }
    264         if (false === filter_var($endpointUri, FILTER_VALIDATE_URL)) {
    265             throw new ClientConfigException("uri must be valid URL");
    266         }
    267         // not allowed to have a fragment (#) in it
    268         if (null !== parse_url($endpointUri, PHP_URL_FRAGMENT)) {
    269             throw new ClientConfigException("uri must not contain a fragment");
    270         }
    271     }
    272 }