# Test Script for Bug 29900 # based off https://wiki.koha-community.org/wiki/REST_API:_Checking_username_and_password # # Change API_CLIENT_ID and API_SECRET to your test 'apioauth2' user's api key and secret. # # See bottom for expected output of a successful validation # run the script on your KTD or koha installation using #>perl api_oauth2_test.pl use Modern::Perl; use LWP::UserAgent; use HTTP::Request::Common; use JSON; use Data::Dumper; use MIME::Base64; $Data::Dumper::Sortkeys = 1; my $domain = 'localhost:8080'; my $client_id = 'API_CLIENT_ID'; #CHANGE ME my $client_secret = 'API_SECRET'; #CHANGE ME my %data = ( 'userid' => 'koha', 'password' => 'koha', ); my $token_url = "http://$domain/api/v1/oauth/token"; my $auth = encode_base64("$client_id:$client_secret", ''); my $ua1 = LWP::UserAgent->new(); my $token_response = $ua1->request(POST $token_url, Content_Type => 'application/x-www-form-urlencoded', Authorization => "Basic $auth", Content => 'grant_type=client_credentials', ); die "Failed to get token: " . $token_response->status_line unless $token_response->is_success; my $token_data = decode_json($token_response->decoded_content); my $access_token = $token_data->{access_token}; my $ua = LWP::UserAgent->new(); my $request = HTTP::Request::Common::POST( "http://$domain/api/v1/auth/password/validation", Content_Type => 'application/json', Content => to_json( \%data ) ); $request->header('Authorization' => "Bearer $access_token"); say Dumper $request; my $response = $ua->request($request); if ($response->is_success) { say $response->as_string(); } else { die "POST API call failed: " . $response->status_line . "\n" . $response->decoded_content; } # Expected output for successful validation# # # # $VAR1 = bless( { # '_content' => '{"userid":"koha","password":"koha"}', # '_headers' => bless( { # 'authorization' => 'Bearer MTc1NTYzNjYwMC0zNzU2ODUtMC4xOTMxNzYxOTI4NDEzNzgta2VCU0xrNlh3c1ZkNkRjYkVUUTBsYmd1WEJzWFlq', # 'content-length' => 35, # 'content-type' => 'application/json' # }, 'HTTP::Headers' ), 3 '_max_body_size' => undef, # '_method' => 'POST', # '_uri' => bless( do{\(my $o = 'http://localhost:8080/api/v1/auth/password/validation')}, 'URI::http' ) # }, 'HTTP::Request' ); # # HTTP/1.1 201 Created # Connection: close # Date: Tue, 19 Aug 2025 20:50:00 GMT # erver: Apache/2.4.62 (Debian) # Vary: User-Agent # Content-Length: 50 # Content-Type: application/json;charset=UTF-8 # Client-Date: Tue, 19 Aug 2025 20:50:00 GMT # Client-Peer: 127.0.0.1:8080 # Client-Response-Num: 1 # # {"cardnumber":"42","patron_id":51,"userid":"koha"}