def switchHomeUser(self, userId, pin=''): if userId == self.ID and self.isAuthenticated: return True # Offline support if self.isOffline: hashed = 'NONE' if pin and self.authToken: hashed = hashlib.sha256(pin + self.authToken).digest() if not self.isProtected or self.isAuthenticated or hashed == ( self.pin or ""): util.DEBUG_LOG("OFFLINE access granted") self.isAuthenticated = True self.validateToken(self.authToken, True) return True else: # build path and post to myplex to swith the user path = '/api/home/users/{0}/switch'.format(userId) req = myplexrequest.MyPlexRequest(path) xml = req.postToStringWithTimeout({'pin': pin}) data = ElementTree.fromstring(xml) if data.attrib.get('authenticationToken'): self.isAuthenticated = True # validate the token (trigger change:user) on user change or channel startup if userId != self.ID or not locks.LOCKS.isLocked("idleLock"): self.validateToken(data.attrib.get('authenticationToken'), True) return True return False
def updateHomeUsers(self): # Ignore request and clear any home users we are not signed in if not self.isSignedIn: self.homeUsers = [] if self.isOffline: self.homeUsers.append(MyPlexAccount()) self.lastHomeUserUpdate = None return # Cache home users for 60 seconds, mainly to stop back to back tests epoch = time.time() if not self.lastHomeUserUpdate: self.lastHomeUserUpdate = epoch elif self.lastHomeUserUpdate + 60 > epoch: util.DEBUG_LOG( "Skipping home user update (updated {0} seconds ago)".format( epoch - self.lastHomeUserUpdate)) return req = myplexrequest.MyPlexRequest("/api/home/users") xml = req.getToStringWithTimeout() data = ElementTree.fromstring(xml) if data.attrib.get('size') and data.find('User') is not None: self.homeUsers = [] for user in data.findall('User'): homeUser = HomeUser(user.attrib) homeUser.isAdmin = homeUser.admin == "1" homeUser.isManaged = homeUser.restricted == "1" homeUser.isProtected = homeUser.protected == "1" self.homeUsers.append(homeUser) self.lastHomeUserUpdate = epoch util.LOG("home users: {0}".format(self.homeUsers))
def validateToken(self, token, switchUser=False): self.authToken = token self.switchUser = switchUser request = myplexrequest.MyPlexRequest("/users/sign_in.xml") context = request.createRequestContext("sign_in", callback.Callable(self.onAccountResponse)) context.timeout = self.isOffline and 1000 or 10000 plexapp.APP.startRequest(request, context, {})
def refreshResources(self, force=False): if force: plexapp.SERVERMANAGER.resetLastTest() request = myplexrequest.MyPlexRequest("/pms/resources") context = request.createRequestContext("resources", callback.Callable(self.onResourcesResponse)) context.timeout = plexapp.ACCOUNT.isOffline and 1000 or 10000 if plexapp.ACCOUNT.isSecure: request.addParam("includeHttps", "1") plexapp.APP.startRequest(request, context)
def publish(self): util.LOG('MyPlexManager().publish() - NOT IMPLEMENTED') return # TODO: ----------------------------------------------------------------------------------------------------------------------------- IMPLEMENT? request = myplexrequest.MyPlexRequest("/devices/" + plexapp.INTERFACE.getGlobal("clientIdentifier")) context = request.createRequestContext("publish") addrs = plexapp.INTERFACE.getGlobal("roDeviceInfo").getIPAddrs() for iface in addrs: request.addParam(urllib.quote("Connection[][uri]"), "http://{0):8324".format(addrs[iface])) plexapp.APP.startRequest(request, context, "_method=PUT")
def loadState(self): # Look for the new JSON serialization. If it's not there, look for the # old token and Plex Pass values. plexapp.APP.addInitializer("myplex") jstring = plexapp.INTERFACE.getRegistry("MyPlexAccount", None, "myplex") if jstring: try: obj = json.loads(jstring) except: util.ERROR() obj = None if obj: self.ID = obj.get('ID') or self.ID self.title = obj.get('title') or self.title self.username = obj.get('username') or self.username self.email = obj.get('email') or self.email self.authToken = obj.get('authToken') or self.authToken self.pin = obj.get('pin') or self.pin self.isPlexPass = obj.get('isPlexPass') or self.isPlexPass self.isManaged = obj.get('isManaged') or self.isManaged self.isAdmin = obj.get('isAdmin') or self.isAdmin self.isSecure = obj.get('isSecure') or self.isSecure self.isProtected = bool(obj.get('pin')) self.adminHasPlexPass = obj.get( 'adminHasPlexPass') or self.adminHasPlexPass if self.authToken: request = myplexrequest.MyPlexRequest("/users/account") context = request.createRequestContext( "account", callback.Callable(self.onAccountResponse)) plexapp.APP.startRequest(request, context) else: plexapp.APP.clearInitializer("myplex")