def auth_app(self, app_id, app_secret, auth_code, state=''): """ Authenticate an app :param app_id: the app id :param app_secret: the app secret :param auth_code: the app auth code """ headers = {'Content-type': 'application/json'} payload = { 'application': app_id, 'auth_code': auth_code, 'state': state } try: full_url = utils.urljoin(self.host, '/api/v1/application-tokens/validate') response = requests.post(full_url, data=json.dumps(payload), headers=headers, verify=self.tls_verify) except RequestException: raise exceptions.TaigaRestException(full_url, 400, 'NETWORK ERROR', 'POST') if response.status_code != 200: raise exceptions.TaigaRestException(full_url, response.status_code, response.text, 'POST') cyphered_token = response.json().get('cyphered_token', '') if cyphered_token: from jwkest.jwk import SYMKey from jwkest.jwe import JWE sym_key = SYMKey(key=app_secret, alg='A128KW') data, success = JWE().decrypt(cyphered_token, keys=[sym_key]), True if isinstance(data, tuple): data, success = data try: self.token = json.loads(data.decode('utf-8')).get( 'token', None) except ValueError: # pragma: no cover self.token = None if not success: self.token = None else: self.token = None if self.token is None: raise exceptions.TaigaRestException(full_url, 400, 'INVALID TOKEN', 'POST') self.raw_request = RequestMaker('/api/v1', self.host, self.token, 'Application', self.tls_verify) self._init_resources()
def auth_app(self, app_id, app_secret, auth_code, state=""): """ Authenticate an app :param app_id: the app id :param app_secret: the app secret :param auth_code: the app auth code """ headers = {"Content-type": "application/json"} payload = { "application": app_id, "auth_code": auth_code, "state": state } try: full_url = utils.urljoin(self.host, "/api/v1/application-tokens/validate") response = requests.post(full_url, data=json.dumps(payload), headers=headers, verify=self.tls_verify) except RequestException: raise exceptions.TaigaRestException(full_url, 400, "NETWORK ERROR", "POST") if response.status_code != 200: raise exceptions.TaigaRestException(full_url, response.status_code, response.text, "POST") cyphered_token = response.json().get("cyphered_token", "") if cyphered_token: from jwkest.jwe import JWE from jwkest.jwk import SYMKey sym_key = SYMKey(key=app_secret, alg="A128KW") data, success = JWE().decrypt(cyphered_token, keys=[sym_key]), True if isinstance(data, tuple): data, success = data try: self.token = json.loads(data.decode("utf-8")).get( "token", None) except ValueError: # pragma: no cover self.token = None if not success: self.token = None else: self.token = None if self.token is None: raise exceptions.TaigaRestException(full_url, 400, "INVALID TOKEN", "POST") self.raw_request = RequestMaker("/api/v1", self.host, self.token, "Application", self.tls_verify) self._init_resources()