diff --git a/src/main/java/co/poynt/postman/PostmanRequestRunner.java b/src/main/java/co/poynt/postman/PostmanRequestRunner.java index 2db16f4..57b1121 100644 --- a/src/main/java/co/poynt/postman/PostmanRequestRunner.java +++ b/src/main/java/co/poynt/postman/PostmanRequestRunner.java @@ -90,6 +90,11 @@ public boolean run(PostmanItem item, PostmanRunResult runResult) { requestId = UUID.randomUUID().toString(); headers.put(REQUEST_ID_HEADER, requestId); } + // Authorization header + final String auth = request.getAuth(); + if(auth!=null) { + headers.put("Authorization", auth); + } logger.info("===============> requestId:" + requestId); String url = request.getUrl(var); URI uri; diff --git a/src/main/java/co/poynt/postman/model/PostmanAuth.java b/src/main/java/co/poynt/postman/model/PostmanAuth.java new file mode 100644 index 0000000..7cb8d2f --- /dev/null +++ b/src/main/java/co/poynt/postman/model/PostmanAuth.java @@ -0,0 +1,8 @@ +package co.poynt.postman.model; + +import java.util.List; + +public class PostmanAuth { + public String type; + public List basic; +} diff --git a/src/main/java/co/poynt/postman/model/PostmanRequest.java b/src/main/java/co/poynt/postman/model/PostmanRequest.java index ea1a687..4bc7514 100644 --- a/src/main/java/co/poynt/postman/model/PostmanRequest.java +++ b/src/main/java/co/poynt/postman/model/PostmanRequest.java @@ -3,6 +3,7 @@ import co.poynt.postman.PostmanRequestRunner; import java.net.URLEncoder; +import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -12,6 +13,7 @@ public class PostmanRequest { public List header; public PostmanBody body; public PostmanUrl url; + public PostmanAuth auth; public String getData(PostmanVariables var) { if (body == null || body.mode == null) { @@ -58,4 +60,38 @@ public Map getHeaders(PostmanVariables var) { } return result; } + + public String getAuth() { + if(this.auth!=null && this.auth.type!=null) { + if(this.auth.type.equalsIgnoreCase("basic")) { + final String[] credentials = this.credentials(); + if(credentials!=null && credentials[0]!=null && credentials[1]!=null) { + final StringBuilder sb = new StringBuilder(credentials[0]); + sb.append(":").append(credentials[1]); + return "Basic " + Base64.getEncoder().encodeToString(sb.toString().getBytes()); + } + return null; + } + throw new IllegalArgumentException("Auth type '"+ this.auth.type + "' not supported."); + } else { + return null; + } + } + + private String[] credentials() { + String username=null; + String password=null; + if(this.auth.basic==null) { + return null; + } + for (final PostmanHeader basic : this.auth.basic) { + if(basic.key.equals("username")) { + username=basic.value; + } + if(basic.key.equals("password")) { + password=basic.value; + } + } + return new String[] {username, password}; + } }