Beispiel #1
0
def handle_auth_requests(args):
    session = session_manager.SessionManager_Client()
    auth_client = authentication_helper.ThriftAuthHelper(
        args.host, args.port, '/Authentication',
        session.getToken(args.host, args.port))

    try:
        handshake = auth_client.getAuthParameters()
    except TApplicationException as tex:
        print("This server does not support privileged access.")
        return

    if not handshake.requiresAuthentication:
        print("This server does not require privileged access.")
        return

    if args.logout:
        if args.username or args.password:
            print('ERROR! Do not supply username and password '
                  'with `--logout` command.')
            sys.exit(1)

        logout_done = auth_client.destroySession()
        if logout_done:
            session.saveToken(args.host, args.port, None, True)
            print('Successfully deauthenticated from server.')

        return

    methods = auth_client.getAcceptedAuthMethods()
    # Attempt username-password auth first
    if 'Username:Password' in str(methods):
        if not args.username or not args.password:
            # Try to use a previously saved credential from configuration file
            savedAuth = session.getAuthString(args.host, args.port)

            if savedAuth:
                print('Logging in using preconfigured credentials.')
                args.username = savedAuth.split(":")[0]
                args.password = savedAuth.split(":")[1]
            else:
                print('Can not authenticate with username'
                      'and password if it is not specified...')
                sys.exit(1)

        try:
            session_token = auth_client.performLogin(
                "Username:Password", args.username + ":" + args.password)
            session.saveToken(args.host, args.port, session_token)
            print("Server reported successful authentication!")
        except shared.ttypes.RequestFailed as reqfail:
            print(reqfail.message)
            sys.exit(1)
Beispiel #2
0
def setupClient(host, port, uri):
    ''' setup the thrift client and check
    API version and authentication needs. '''
    manager = session_manager.SessionManager_Client()
    session_token = manager.getToken(host, port)

    # Before actually communicating with the server,
    # we need to check authentication first
    auth_client = authentication_helper.ThriftAuthHelper(
        host, port, uri + 'Authentication', session_token)
    try:
        auth_response = auth_client.getAuthParameters()
    except TApplicationException as tex:
        auth_response = AuthTypes.HandshakeInformation()
        auth_response.requiresAuthentication = False

    if auth_response.requiresAuthentication and \
            not auth_response.sessionStillActive:
        print_err = False

        if manager.is_autologin_enabled():
            auto_auth_string = manager.getAuthString(host, port)
            if auto_auth_string:
                # Try to automatically log in with a saved credential
                # if it exists for the server
                try:
                    session_token = auth_client.performLogin(
                        "Username:Password", auto_auth_string)
                    manager.saveToken(host, port, session_token)
                    print("Authenticated using pre-configured credentials...")
                except shared.ttypes.RequestFailed:
                    print_err = True
        else:
            print_err = True

        if print_err:
            print('Access denied. This server requires authentication.')
            print('Please log in onto the server '
                  'using `CodeChecker cmd login`.')
            sys.exit(1)

    client = thrift_helper.ThriftClientHelper(host, port, uri, session_token)
    # test if client can work with thrift API getVersion
    if not check_API_version(client):
        print('Backward incompatible change was in the API.')
        print('Please update client. Server version is not supported.')
        sys.exit(1)

    return client
def handle_auth_requests(args):
    session = session_manager.SessionManager_Client()

    auth_token = session.getToken(args.host, args.port)

    auth_client = authentication_helper.ThriftAuthHelper(
        args.host, args.port, '/Authentication', auth_token)
    try:
        handshake = auth_client.getAuthParameters()

        if not handshake.requiresAuthentication:
            print("This server does not require privileged access.")
            return

        if auth_token and handshake.sessionStillActive:
            print("Active authentication token found no new login required.")
            return
        else:
            print("No/Old authentication token found please login again.")

    except TApplicationException as tex:
        print("This server does not support privileged access.")
        return

    if args.logout:
        logout_done = auth_client.destroySession()
        if logout_done:
            session.saveToken(args.host, args.port, None, True)
            print('Successfully deauthenticated from server.')
        return

    methods = auth_client.getAcceptedAuthMethods()
    # Attempt username-password auth first
    if 'Username:Password' in str(methods):
        pwd = None
        username = args.username

        # Try to use a previously saved credential from configuration file
        savedAuth = session.getAuthString(args.host, args.port)

        if savedAuth:
            print('Logging in using preconfigured credentials.')
            username = savedAuth.split(":")[0]
            pwd = savedAuth.split(":")[1]
        else:
            print('Logging in using command line credentials.')
            print('Please provide password for user: '******'Password:'******'Username, password authentication is not supported.')
        sys.exit(1)