FAR part 61.51 states that when an intention to fly exists you must provide, your pilot's license, medical, airworthiness certificate, registration and photo ID to any law enforcement officer that asks for it. Most state laws are written in line with this. If you just got out of a running airplane or are loading bags in your airplane then intent to fly exists. If you are just working on your airplane you can tell them that you don't intend to fly. Depending on the airport, you may have to have Airport ID anyway.
When asked for this stuff the officer will also want to see the data plate in the aircraft (not a search). What they will pull when you are providing all of this is run a dog around your airplane. Again, thanks to the SCOTUS, this isn't considered a search. If the dog 'hits' then they are going in the airplane (a search with PC). Don't give consent, but you can't stop them from searching with PC.
If you have heartburn about a search the Feds often carry a pamphlet on ramp checks, with a complaint number. If you get raw treatment from CBP aircrew (dressed in khaki) you can call 1-866-air-bust and ask for instructions on filing a complaint.
I’ve never had any problems with any officers ever requesting to see ID or search the plane, before takeoff or on landing. Maybe they just leave us grouchy old men alone. I have had them walk by while I was loading my baggage but they kept on walking.
More reason to fly something ( most homebuilts ) with the data plate fixed below the pilots rear horizontal stab. I don't mean to be obtuse, but I'd tell them to look their, while I sit inside...