Exemplo n.º 1
0
 def store_refresh_token(self, user, refresh_token, expires):
     """
     Store refresh token in db.
     """
     user.upstream_refresh_tokens = []
     upstream_refresh_token = UpstreamRefreshToken(
         user=user,
         refresh_token=refresh_token,
         expires=expires,
     )
     current_db_session = current_session.object_session(
         upstream_refresh_token)
     current_db_session.add(upstream_refresh_token)
     current_session.commit()
Exemplo n.º 2
0
    def update_user_visas(self, user):
        """
        Updates user's RAS refresh token and uses the new access token to retrieve new visas from
        RAS's /userinfo endpoint and update the db with the new visa.
        - delete user's visas from db if we're not able to get a new access_token
        - delete user's visas from db if we're not able to get a new visa
        """
        user.ga4gh_visas_v1 = []
        current_session.commit()

        try:
            token_endpoint = self.get_value_from_discovery_doc(
                "token_endpoint", "")
            userinfo_endpoint = self.get_value_from_discovery_doc(
                "userinfo_endpoint", "")
            token = self.get_access_token(user, token_endpoint)
            userinfo = self.get_userinfo(token, userinfo_endpoint)
            encoded_visas = userinfo.get("ga4gh_passport_v1", [])
        except Exception as e:
            err_msg = "Could not retrieve visa"
            self.logger.exception("{}: {}".format(err_msg, e))
            raise

        for encoded_visa in encoded_visas:
            try:
                # TODO: These visas must be validated!!!
                decoded_visa = jwt.decode(encoded_visa, verify=False)
                visa = GA4GHVisaV1(
                    user=user,
                    source=decoded_visa["ga4gh_visa_v1"]["source"],
                    type=decoded_visa["ga4gh_visa_v1"]["type"],
                    asserted=int(decoded_visa["ga4gh_visa_v1"]["asserted"]),
                    expires=int(decoded_visa["exp"]),
                    ga4gh_visa=encoded_visa,
                )

                current_db_session = current_session.object_session(visa)

                current_db_session.add(visa)
            except Exception as e:
                err_msg = (
                    f"Could not process visa '{encoded_visa}' - skipping this visa"
                )
                self.logger.exception("{}: {}".format(err_msg, e),
                                      exc_info=True)
            current_session.commit()