Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Basic Auth implemented #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/co/poynt/postman/PostmanRequestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/co/poynt/postman/model/PostmanAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.poynt.postman.model;

import java.util.List;

public class PostmanAuth {
public String type;
public List<PostmanHeader> basic;
}
36 changes: 36 additions & 0 deletions src/main/java/co/poynt/postman/model/PostmanRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,7 @@ public class PostmanRequest {
public List<PostmanHeader> header;
public PostmanBody body;
public PostmanUrl url;
public PostmanAuth auth;

public String getData(PostmanVariables var) {
if (body == null || body.mode == null) {
Expand Down Expand Up @@ -58,4 +60,38 @@ public Map<String, String> 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};
}
}