HeaderCredential
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package org.maxkey.util;
|
||||
|
||||
public class AuthorizationHeaderCredential {
|
||||
|
||||
public static class Credential{
|
||||
|
||||
public static final String BASIC = "Basic ";
|
||||
|
||||
public static final String BEARER = "Bearer ";
|
||||
}
|
||||
|
||||
String credentialType = Credential.BASIC;
|
||||
String username;
|
||||
String credential;
|
||||
String authorization;
|
||||
|
||||
public AuthorizationHeaderCredential(String bearer) {
|
||||
super();
|
||||
this.credential = bearer;
|
||||
this.credentialType = Credential.BEARER;
|
||||
}
|
||||
|
||||
|
||||
public AuthorizationHeaderCredential(String username, String credential) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.credential = credential;
|
||||
}
|
||||
|
||||
public String getCredentialType() {
|
||||
return credentialType;
|
||||
}
|
||||
|
||||
public void setCredentialType(String credentialType) {
|
||||
this.credentialType = credentialType;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getCredential() {
|
||||
return credential;
|
||||
}
|
||||
public void setCredential(String credential) {
|
||||
this.credential = credential;
|
||||
}
|
||||
|
||||
public String transform() {
|
||||
if(credentialType.equalsIgnoreCase(Credential.BASIC)) {
|
||||
return AuthorizationHeaderUtils.createBasic(username, credential);
|
||||
}else {
|
||||
return AuthorizationHeaderUtils.createBearer(credential);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuthorizationHeaderCredential [credentialType=" + credentialType + ", username=" + username
|
||||
+ ", credential=" + credential + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,33 +27,33 @@ public class AuthorizationHeaderUtils {
|
||||
|
||||
public static final String AUTHORIZATION_HEADERNAME = "Authorization";
|
||||
|
||||
public static final String BASIC = "Basic ";
|
||||
|
||||
public static final String BEARER = "Bearer ";
|
||||
|
||||
public static String createBasic(String username, String password) {
|
||||
String authUserPass = username + ":" + password;
|
||||
String encodedAuthUserPass = Base64Utils.encode(authUserPass);
|
||||
return BASIC + encodedAuthUserPass;
|
||||
return AuthorizationHeaderCredential.Credential.BASIC + encodedAuthUserPass;
|
||||
}
|
||||
|
||||
public static String[] resolveBasic(String basic) {
|
||||
if (isBasic(basic)) {
|
||||
String[] userPass = basic.split(" ");
|
||||
String decodeUserPass = Base64Utils.decode(userPass[1]);
|
||||
return decodeUserPass.split(":");
|
||||
public static AuthorizationHeaderCredential resolve(String authorization) {
|
||||
if (isBasic(authorization)) {
|
||||
String decodeUserPass = Base64Utils.decode(authorization.split(" ")[1]);
|
||||
String []userPass =decodeUserPass.split(":");
|
||||
return new AuthorizationHeaderCredential(userPass[0],userPass[1]);
|
||||
} else {
|
||||
return null;
|
||||
return new AuthorizationHeaderCredential(resolveBearer(authorization));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isBasic(String basic) {
|
||||
if (basic.startsWith(BASIC)) {
|
||||
if (basic.startsWith(AuthorizationHeaderCredential.Credential.BASIC)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String createBearer(String bearer) {
|
||||
return AuthorizationHeaderCredential.Credential.BEARER + bearer;
|
||||
}
|
||||
|
||||
public static String resolveBearer(String bearer) {
|
||||
if (isBearer(bearer)) {
|
||||
@@ -62,13 +62,9 @@ public class AuthorizationHeaderUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String createBearer(String bearer) {
|
||||
return BEARER + bearer;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isBearer(String bearer) {
|
||||
if (bearer.startsWith(BEARER)) {
|
||||
if (bearer.startsWith(AuthorizationHeaderCredential.Credential.BEARER)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
||||
@@ -18,15 +18,24 @@
|
||||
package org.maxkey.rest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.maxkey.util.AuthorizationHeaderCredential;
|
||||
import org.maxkey.util.AuthorizationHeaderUtils;
|
||||
|
||||
public class AuthorizationHeaderTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
String basic =AuthorizationHeaderUtils.createBasic("Aladdin", "open sesame");
|
||||
System.out.println(basic);
|
||||
String []rb=AuthorizationHeaderUtils.resolveBasic(basic);
|
||||
System.out.println(rb[0]+":"+rb[1]);
|
||||
|
||||
String ahc_basic ="Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
|
||||
System.out.println(AuthorizationHeaderUtils.resolve(ahc_basic));
|
||||
|
||||
AuthorizationHeaderCredential ahc =new AuthorizationHeaderCredential("Aladdin");
|
||||
System.out.println(ahc.transform());
|
||||
|
||||
System.out.println(AuthorizationHeaderUtils.resolve(ahc.transform()));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user