Пример #1
0
def interactive_login(realm_label):
    """Logs in interactively."""
    env, config = utils.get_selected_deployment_config()
    if config is not None and URL_KEY in config:
        nxs.config.set_environment(config[URL_KEY])
    else:
        utils.error(
            "You must first select a profile using the 'profiles' command")

    issuer = ''
    if realm_label:
        try:
            response = nxs.realms.fetch(realm_label)
            issuer = response['_issuer']
        except nxs.HTTPError as e:
            print(e.response.json())
            utils.error(str(e))
    else:
        realm = utils.get_default_realm()
        if realm is None:
            realm = _select_realm()
            issuer = realm['_issuer']
            label = realm['_label']
            print("Saving realm '%s' in your profile" % realm['_label'])
            utils.set_default_realm(label, issuer)
        else:
            issuer = realm['_issuer']
            print(
                "Using default realm '%s' set in your profile. Use --realm to override."
                % realm['_label'])

    default_client_id = utils.get_default_client_id()
    client_id = input("Please enter the client ID [%s]: " % default_client_id)
    if not client_id:
        client_id = default_client_id
    utils.set_default_client_id(client_id)

    detected_user = getpass.getuser()
    username = input("Username [%s]: " % detected_user)
    if not username:
        username = detected_user
    password = getpass.getpass()
    try:
        url, name = issuer.split("realms/")
        keycloak_openid = KeycloakOpenID(server_url=url,
                                         client_id=client_id,
                                         realm_name=name,
                                         verify=True)
        response = keycloak_openid.token(username, password)
        token = response['access_token']
        _set_token(token)
        utils.success(
            "Authentication successful. Use 'view-token' to show your credentials."
        )
    except KeycloakError as e:
        utils.error("Authentication failed: %s" % e)
Пример #2
0
def update(label, data, _json, pretty):
    nxs = utils.get_nexus_client()
    try:
        response = nxs.realms.replace_(label, data, data['_rev'])
        if _json:
            utils.print_json(response, colorize=pretty)
        utils.success("Realm updated.")
    except nxs.HTTPError as e:
        print(e.response.json())
        utils.error(str(e))
Пример #3
0
def create(label, name, _open_id_config, logo, _json, pretty):
    nxs = utils.get_nexus_client()
    try:
        response = nxs.realms.create(label, name, _open_id_config, logo)
        if _json:
            utils.print_json(response, colorize=pretty)
        utils.success("Realm created.")
    except nxs.HTTPError as e:
        print(e.response.json())
        utils.error(str(e))
Пример #4
0
def select(label, _json, pretty):
    nxs = utils.get_nexus_client()
    try:
        response = nxs.realms.fetch(label)
        if _json:
            utils.print_json(response, colorize=pretty)
        utils.set_default_realm(label, response['_issuer'])
        utils.success("Realm selected.")
    except nxs.HTTPError as e:
        print(e.response.json())
        utils.error(str(e))
Пример #5
0
def deprecate(label, _json, pretty):
    nxs = utils.get_nexus_client()
    try:
        response = nxs.realms.deprecate(label)
        if _json:
            utils.print_json(response, colorize=pretty)

        utils.success("Realm deprecated.")
    except nxs.HTTPError as e:
        print(e.response.json())
        utils.error(str(e))