| 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\RemoteStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use fkooman\RemoteStorage\Http\Exception\HttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use fkooman\RemoteStorage\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use fkooman\RemoteStorage\Http\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class WebfingerModule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $serverMode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $serverMode | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct($serverMode) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->serverMode = $serverMode; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function getWebfinger(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $resource = $request->getQueryParameter('resource'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (null === $resource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             throw new HttpException('resource parameter missing', 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (0 !== strpos($resource, 'acct:')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             throw new HttpException('unsupported resource type', 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $userAddress = substr($resource, 5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $atPos = strpos($userAddress, '@'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (false === $atPos) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             throw new HttpException('invalid user address', 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $user = substr($userAddress, 0, $atPos); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $webFingerData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'links' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     'href' => sprintf('%s%s', $request->getRootUri(), $user), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     'properties' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         'http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-05', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         'http://remotestorage.io/spec/web-authoring' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                         'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getRootUri(), $user), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         'http://tools.ietf.org/html/rfc6750#section-2.3' => 'true', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                         'http://tools.ietf.org/html/rfc7233' => 'development' !== $this->serverMode ? 'GET' : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     'rel' => 'http://tools.ietf.org/id/draft-dejong-remotestorage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 // legacy -03 WebFinger response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     'href' => sprintf('%s%s', $request->getRootUri(), $user), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     'properties' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         'http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-03', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         'http://tools.ietf.org/html/rfc2616#section-14.16' => 'development' !== $this->serverMode ? 'GET' : false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                         'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getRootUri(), $user), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         'http://tools.ietf.org/html/rfc6750#section-2.3' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     'rel' => 'remotestorage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $response = new Response(200, 'application/jrd+json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $response->addHeader('Access-Control-Allow-Origin', '*'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $response->setBody( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             json_encode($webFingerData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |