示例#1
0
    def _request(self, url, function, *args):
        headers = Request.PrepareApiCallHeaders(authToken=self.AccessToken(), userId=self.UserId(),
                                                deviceId=self._devideId)
        try:
            return function(url, headers, *args)
        except NotAuthenticatedError:
            # try to authenticate
            if not self._authenticate(force=True):
                return False

            # retrieve the headers again because the access token has changed
            headers = Request.PrepareApiCallHeaders(authToken=self.AccessToken(), userId=self.UserId(),
                                                    deviceId=self._devideId)

            # execute the actual request again
            return function(url, headers, *args)
    def Exchange(baseUrl, accessKey, userId, deviceId=None):
        if not baseUrl:
            raise ValueError('invalid baseUrl')
        if not accessKey:
            raise ValueError('invalid accessKey')
        if not userId:
            raise ValueError('invalid userId')

        exchangeUrl = server.Server.BuildConnectExchangeUrl(baseUrl, userId)
        headers = Request.PrepareApiCallHeaders(deviceId=deviceId)
        headers.update({
            constants.EMBY_CONNECT_TOKEN_HEADER: accessKey,
        })

        resultObj = Request.GetAsJson(exchangeUrl, headers=headers)
        if not resultObj or \
           constants.PROPERTY_EMBY_CONNECT_EXCHANGE_LOCAL_USER_ID not in resultObj or \
           constants.PROPERTY_EMBY_CONNECT_EXCHANGE_ACCESS_TOKEN not in resultObj:
            log('invalid response from {}: {}'.format(exchangeUrl, resultObj))
            return None

        return EmbyConnect.AuthenticationResult(
            accessToken=resultObj.get(constants.PROPERTY_EMBY_CONNECT_EXCHANGE_ACCESS_TOKEN),
            userId=resultObj.get(constants.PROPERTY_EMBY_CONNECT_EXCHANGE_LOCAL_USER_ID)
        )
示例#3
0
    def GetPublicUsers(baseUrl, deviceId=None):
        users = []

        usersUrl = Url.append(baseUrl, constants.EMBY_PROTOCOL,
                              constants.URL_USERS, constants.URL_USERS_PUBLIC)
        headers = Request.PrepareApiCallHeaders(deviceId=deviceId)
        resultObj = Request.GetAsJson(usersUrl, headers=headers)
        if not resultObj:
            return users

        for userObj in resultObj:
            # make sure the 'Name' and 'Id' properties are available
            if not constants.PROPERTY_USER_NAME in userObj or not constants.PROPERTY_USER_ID in userObj:
                continue

            # make sure the name and id properties are valid
            user = User(userObj[constants.PROPERTY_USER_NAME],
                        userObj[constants.PROPERTY_USER_ID])
            if not user.name or not user.id:
                continue

            # check if the user is disabled
            if constants.PROPERTY_USER_POLICY in userObj and \
               constants.PROPERTY_USER_IS_DISABLED in userObj[constants.PROPERTY_USER_POLICY] and \
               userObj[constants.PROPERTY_USER_POLICY][constants.PROPERTY_USER_IS_DISABLED]:
                continue

            users.append(user)

        return users
    def Authenticate(baseUrl,
                     authenticationMethod,
                     username=None,
                     userId=None,
                     password=None,
                     deviceId=None):
        if not password:
            raise ValueError('invalid password')

        # prepare the authentication URL
        authUrl = baseUrl
        authUrl = Url.append(authUrl, constants.URL_USERS)

        body = {constants.PROPERTY_USER_AUTHENTICATION_PASSWORD: password}
        if authenticationMethod == Authentication.Method.UserId:
            if not userId:
                raise ValueError('invalid userId')

            authUrl = Url.append(authUrl, userId, constants.URL_AUTHENTICATE)

        elif authenticationMethod == Authentication.Method.Username:
            if not username:
                raise ValueError('invalid username')

            authUrl = Url.append(authUrl, constants.URL_AUTHENTICATE_BY_NAME)

            body[constants.PROPERTY_USER_AUTHENTICATION_USERNAME] = username

        else:
            raise ValueError('invalid authenticationMethod')

        headers = Request.PrepareApiCallHeaders(deviceId=deviceId,
                                                userId=userId)
        headers['Content-Type'] = constants.EMBY_CONTENT_TYPE

        resultObj = Request.PostAsJson(
            authUrl,
            headers=headers,
            json=body,
            timeout=Authentication.REQUEST_TIMEOUT_S)
        if not resultObj:
            return Authentication.Result()

        if constants.PROPERTY_USER_AUTHENTICATION_ACCESS_TOKEN not in resultObj:
            return Authentication.Result()
        accessToken = \
            resultObj[constants.PROPERTY_USER_AUTHENTICATION_ACCESS_TOKEN]

        if constants.PROPERTY_USER_AUTHENTICATION_USER not in resultObj:
            return Authentication.Result()
        userObj = resultObj[constants.PROPERTY_USER_AUTHENTICATION_USER]
        if constants.PROPERTY_USER_AUTHENTICATION_USER_ID not in userObj:
            return Authentication.Result()

        userId = userObj[constants.PROPERTY_USER_AUTHENTICATION_USER_ID]

        return Authentication.Result(result=True,
                                     accessToken=accessToken,
                                     userId=userId)
示例#5
0
    def _request(self, url, function, *args):
        if not self._authenticate():
            return False

        headers = Request.PrepareApiCallHeaders(authToken=self.AccessToken(),
                                                userId=self.UserId(),
                                                deviceId=self._devideId)
        return function(url, headers, *args)
示例#6
0
    def GetInfo(baseUrl):
        publicInfoUrl = server.Server.BuildPublicInfoUrl(baseUrl)
        headers = Request.PrepareApiCallHeaders()
        resultObj = Request.GetAsJson(publicInfoUrl, headers=headers)

        return Server.Info.fromPublicInfo(resultObj)