Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Basic Auth using Karate?

Tags:

karate

I saw details about OAuth2 in Karate Demo. Can you also provide how to implement Basic Auth?

screenshot

like image 590
Sourabh Chapali Avatar asked Sep 03 '26 05:09

Sourabh Chapali


1 Answers

Yes, this JS function is all you need (basic-auth.js):

function(creds) {
  var temp = creds.username + ':' + creds.password;
  var Base64 = Java.type('java.util.Base64');
  var encoded = Base64.getEncoder().encodeToString(temp.getBytes());
  return 'Basic ' + encoded;
}

And then use this function to build the value of the Authorization header:

* header Authorization = call read('basic-auth.js') { username: 'john', password: 'secret' }

Refer to the docs here: https://github.com/intuit/karate#http-basic-authentication-example

For OAuth or "login form" kinds of flows, see: https://stackoverflow.com/a/58643689/143475 and https://stackoverflow.com/a/46333729/143475

like image 133
Peter Thomas Avatar answered Sep 04 '26 19:09

Peter Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!