def exchange(self):
            if not self.pin:
                return None

            if not self.finished or self.expired:
                return None

            if self._authenticationResult:
                return self._authenticationResult

            url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_PIN,
                             constants.URL_EMBY_CONNECT_PIN_AUTHENTICATE)
            body = {
                constants.URL_QUERY_DEVICE_ID: self.deviceId,
                constants.URL_QUERY_PIN: self.pin,
            }

            resultObj = Request.PostAsJson(url, json=body)
            if not resultObj or \
               constants.PROPERTY_EMBY_CONNECT_PIN_USER_ID not in resultObj or \
               constants.PROPERTY_EMBY_CONNECT_PIN_ACCESS_TOKEN not in resultObj:
                log('failed to authenticate with PIN {} at {}: {}'.format(self.pin, url, resultObj))
                return None

            self._authenticationResult = EmbyConnect.AuthenticationResult(
                accessToken=resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN_ACCESS_TOKEN),
                userId=resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN_USER_ID))
            return self._authenticationResult
    def Authenticate(username, password):
        if not username:
            raise ValueError('invalid username')
        if not password:
            raise ValueError('invalid password')

        url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_AUTHENTICATE)
        headers = EmbyConnect._getApplicationHeader()

        body = {
            constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_NAME_OR_EMAIL: username,
            constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_PASSWORD: hashlib.md5(password),  # nosec
        }

        resultObj = Request.PostAsJson(url, headers=headers, json=body)
        if not resultObj or \
           constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_ACCESS_TOKEN not in resultObj or \
           constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER not in resultObj:
            log('invalid response from {}: {}'.format(url, resultObj))
            return None

        userObj = resultObj.get(constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER)
        if constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER_ID not in userObj:
            log('invalid response from {}: {}'.format(url, resultObj))
            return None

        return EmbyConnect.AuthenticationResult(
            accessToken=resultObj.get(constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_ACCESS_TOKEN),
            userId=userObj.get(constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER_ID)
        )
    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)
Esempio n. 4
0
        def _getPin(self):
            if self.pin:
                return self.pin

            url = Url.append(constants.URL_EMBY_CONNECT_BASE,
                             constants.URL_EMBY_CONNECT_PIN)
            body = {constants.URL_QUERY_DEVICE_ID: self.deviceId}

            resultObj = Request.PostAsJson(url, json=body)
            if not resultObj or \
               not constants.PROPERTY_EMBY_CONNECT_PIN in resultObj:
                log('failed to get a PIN from {}: {}'.format(url, resultObj))
                return None

            self.pin = resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN)

            return self.pin
Esempio n. 5
0
 def ApiPost(self, url, data=None, json=None):
     return self._request(url, lambda url, headers, data, json:
                          Request.PostAsJson(url, headers=headers, body=data, json=json),
                          data, json)