def _convert_user_credentials(session, token_url, username=None, password=None, refresh_token=None): """ Converts username/password credentials to token credentials by using Yamcs as the authentication server. """ if username and password: data = { 'grant_type': 'password', 'username': username, 'password': password } elif refresh_token: data = {'grant_type': 'refresh_token', 'refresh_token': refresh_token} else: raise NotImplementedError() response = session.post(token_url, data=data) if response.status_code == 401: raise Unauthorized('401 Client Error: Unauthorized') elif response.status_code == 200: d = response.json() expiry = datetime.utcnow() + timedelta(seconds=d['expires_in']) return Credentials(access_token=d['access_token'], refresh_token=d['refresh_token'], expiry=expiry) else: raise YamcsError('{} Server Error'.format(response.status_code))
def authenticate_with_access_token(access_token): """Authenticate using an existing access token.""" credentials = Credentials(access_token=access_token) client = YamcsClient('localhost:8090', credentials=credentials) for link in client.list_data_links('simulator'): print(link)
def authenticate_with_username_password(): """Authenticate in by directly providing username/password to Yamcs.""" credentials = Credentials(username='******', password='******') client = YamcsClient('localhost:8090', credentials=credentials) for link in client.list_data_links('simulator'): print(link)
def authenticate_with_username_password(): """Authenticate by directly providing username/password to Yamcs.""" credentials = Credentials(username="******", password="******") client = YamcsClient("localhost:8090", credentials=credentials) for link in client.list_links("simulator"): print(link)
def impersonate_with_client_credentials(): credentials = Credentials(client_id='cf79cfbd-ed01-4ae2-93e1-c606a2ebc36f', client_secret='!#?hgbu1*3', become='admin') client = YamcsClient('localhost:8090', credentials=credentials) print('have', client.get_user_info().username) while True: print(client.get_time('simulator')) sleep(1)
def impersonate_with_client_credentials(): credentials = Credentials( client_id="cf79cfbd-ed01-4ae2-93e1-c606a2ebc36f", client_secret="!#?hgbu1*3", become="admin", ) client = YamcsClient("localhost:8090", credentials=credentials) print("have", client.get_user_info().username) while True: print(client.get_time("simulator")) sleep(1)
def convert_authorization_code(self, session, auth_url, code): data = {"grant_type": "authorization_code", "code": code} response = session.post(auth_url + "/token", data=data) if response.status_code == 401: raise Unauthorized("401 Client Error: Unauthorized") elif response.status_code == 200: d = response.json() expiry = datetime.utcnow() + timedelta(seconds=d["expires_in"]) return Credentials( access_token=d["access_token"], refresh_token=d["refresh_token"], expiry=expiry, ) else: raise YamcsError("{} Server Error".format(response.status_code))
def _convert_service_account_credentials(session, token_url, client_id, client_secret, become): """ Converts service account credentials to impersonated token credentials. """ data = {'grant_type': 'client_credentials', 'become': become} response = session.post(token_url, data=data, auth=HTTPBasicAuth(client_id, client_secret)) if response.status_code == 401: raise Unauthorized('401 Client Error: Unauthorized') elif response.status_code == 200: d = response.json() expiry = datetime.utcnow() + timedelta(seconds=d['expires_in']) return Credentials(access_token=d['access_token'], client_id=client_id, client_secret=client_secret, become=become, expiry=expiry) else: raise YamcsError('{} Server Error'.format(response.status_code))