def disconnect(self, session: OAuth2Session): # https://github.com/eduvpn/documentation/blob/v3/API.md#disconnect # We allow a timeout for 1 second here try: session.post(self.api_call_endpoint('disconnect'), timeout=1) except: # NOQA: ignore # NOTE: No need to check the response as, # according to the API specification, # this is a "best-effort" call. pass
def connect(self, app, profile: 'Profile', session: OAuth2Session) -> 'eduvpn.connection.Connection': # https://github.com/eduvpn/documentation/blob/v3/API.md#connect accept_types = ', '.join( proto.config_type for proto in profile.vpn_proto_list if proto.is_supported and proto.supports_config(app.config)) data = dict(profile_id=profile.id) keypair = None if Protocol.WIREGUARD in profile.vpn_proto_list: keypair = crypto.generate_wireguard_keys() data['public_key'] = keypair.public session = add_retry_adapter(session, MAX_HTTP_RETRIES) endpoint = self.api_call_endpoint('connect') try: response = session.post(endpoint, data=data, headers={'Accept': accept_types}, timeout=MAX_HTTP_TIMEOUT) except Exception as e: msg = f"Got exception {e} requesting {endpoint}" logger.debug(msg) raise remote.check_response(response) from .connection import Connection connection = Connection.parse(response) if keypair: connection.set_secret_key(keypair.secret) return connection
def create_keypair(oauth: OAuth2Session, api_base_uri: str) -> (str, str): response = oauth.post(api_base_uri + '/create_keypair') keypair = response.json()['create_keypair']['data'] private_key = keypair['private_key'] certificate = keypair['certificate'] return private_key, certificate