| 1 | 1 |  | from plugin.core.environment import Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | from plugin.models.m_plex.account import PlexAccount | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 | 1 |  | from plugin.models.core import db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | from playhouse.apsw_ext import * | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | from xml.etree import ElementTree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 1 |  | import requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  | log = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 1 |  | class PlexBasicCredential(Model): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  |     class Meta: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 1 |  |         database = db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |         db_table = 'plex.credential.basic' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  |     account = ForeignKeyField(PlexAccount, 'basic_credentials', unique=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |     password = CharField(null=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     # Authorization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |     token_plex = CharField(null=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |     token_server = CharField(null=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     def state(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if self.token_plex is not None and self.token_server is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             return 'valid' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if self.password is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             return 'warning' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return 'empty' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |     def refresh(self, save=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         # Refresh account details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if not self.refresh_details(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         # Store changes in database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if save: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             self.save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |     def refresh_details(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if self.token_plex is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             # Missing token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if self.token_server is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             # Already authenticated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if self.token_plex == 'anonymous': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return self.refresh_anonymous() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         # Fetch server token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         response = requests.get('https://plex.tv/api/resources?includeHttps=1', headers={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'X-Plex-Token': self.token_plex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if not (200 <= response.status_code < 300): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             # Invalid response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         devices = ElementTree.fromstring(response.content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         # Find server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         server = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         for s in devices.findall('Device'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if s.attrib.get('clientIdentifier') != Environment.platform.machine_identifier: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             server = s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if server is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             log.warn('Unable to find server with identifier: %r', Environment.platform.machine_identifier) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         # Update `token_server` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         self.token_server = server.attrib.get('accessToken') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return True | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |     def refresh_anonymous(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         log.debug('Refreshing anonymous plex credential') | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         self.token_server = 'anonymous' | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     def to_json(self, account): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         result = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'state': self.state, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'username': account.username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if self.password: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             result['password'] = '*' * len(self.password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         elif self.token_plex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             result['password'] = '*' * 8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |         return result | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |