Exemple #1
0
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))
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
 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))
Exemple #8
0
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))