AuthorizationHeaderCredential.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.util;
  17. public class AuthorizationHeaderCredential {
  18. public static class Credential {
  19. public static final String BASIC = "Basic ";
  20. public static final String BEARER = "Bearer ";
  21. }
  22. String credentialType = Credential.BASIC;
  23. String username;
  24. String credential;
  25. String authorization;
  26. public AuthorizationHeaderCredential(String bearer) {
  27. super();
  28. this.credential = bearer;
  29. this.credentialType = Credential.BEARER;
  30. }
  31. public AuthorizationHeaderCredential(String username, String credential) {
  32. super();
  33. this.username = username;
  34. this.credential = credential;
  35. }
  36. public String getCredentialType() {
  37. return credentialType;
  38. }
  39. public void setCredentialType(String credentialType) {
  40. this.credentialType = credentialType;
  41. }
  42. public String getUsername() {
  43. return username;
  44. }
  45. public void setUsername(String username) {
  46. this.username = username;
  47. }
  48. public String getCredential() {
  49. return credential;
  50. }
  51. public void setCredential(String credential) {
  52. this.credential = credential;
  53. }
  54. public String transform() {
  55. if (credentialType.equalsIgnoreCase(Credential.BASIC)) {
  56. return AuthorizationHeaderUtils.createBasic(username, credential);
  57. } else {
  58. return AuthorizationHeaderUtils.createBearer(credential);
  59. }
  60. }
  61. public boolean isBasic() {
  62. return credentialType.equals(Credential.BASIC) ? true : false;
  63. }
  64. @Override
  65. public String toString() {
  66. return "AuthorizationHeaderCredential [credentialType=" + credentialType + ", username=" + username
  67. + ", credential=" + credential + "]";
  68. }
  69. }