NextcloudRequest.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Nextcloud SingleSignOn
  3. *
  4. * @author David Luhmer
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.nextcloud.android.sso.aidl;
  20. import java.io.Serializable;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. public class NextcloudRequest implements Serializable {
  25. private static final long serialVersionUID = 215521212534238L; //assign a long value
  26. private String method;
  27. private Map<String, List<String>> header = new HashMap<>();
  28. private Map<String, String> parameter = new HashMap<>();
  29. private String requestBody;
  30. private String url;
  31. private String token;
  32. private String packageName;
  33. private String accountName;
  34. private boolean followRedirects = false;
  35. private NextcloudRequest() { }
  36. public static class Builder {
  37. private NextcloudRequest ncr;
  38. public Builder() {
  39. ncr = new NextcloudRequest();
  40. }
  41. public NextcloudRequest build() {
  42. return ncr;
  43. }
  44. public Builder setMethod(String method) {
  45. ncr.method = method;
  46. return this;
  47. }
  48. public Builder setHeader(Map<String, List<String>> header) {
  49. ncr.header = header;
  50. return this;
  51. }
  52. public Builder setParameter(Map<String, String> parameter) {
  53. ncr.parameter = parameter;
  54. return this;
  55. }
  56. public Builder setRequestBody(String requestBody) {
  57. ncr.requestBody = requestBody;
  58. return this;
  59. }
  60. public Builder setUrl(String url) {
  61. ncr.url = url;
  62. return this;
  63. }
  64. public Builder setToken(String token) {
  65. ncr.token = token;
  66. return this;
  67. }
  68. public Builder setPackageName(String packageName) {
  69. ncr.packageName = packageName;
  70. return this;
  71. }
  72. public Builder setAccountName(String accountName) {
  73. ncr.accountName = accountName;
  74. return this;
  75. }
  76. /**
  77. * Default: true
  78. * @param followRedirects
  79. * @return
  80. */
  81. public Builder setFollowRedirects(boolean followRedirects) {
  82. ncr.followRedirects = followRedirects;
  83. return this;
  84. }
  85. }
  86. public String getMethod() {
  87. return this.method;
  88. }
  89. public Map<String, List<String>> getHeader() {
  90. return this.header;
  91. }
  92. public Map<String, String> getParameter() {
  93. return this.parameter;
  94. }
  95. public String getRequestBody() {
  96. return this.requestBody;
  97. }
  98. public String getUrl() {
  99. return this.url;
  100. }
  101. public String getToken() {
  102. return this.token;
  103. }
  104. public void setToken(String token) {
  105. this.token = token;
  106. }
  107. public String getPackageName() {
  108. return this.packageName;
  109. }
  110. public void setPackageName(String packageName) {
  111. this.packageName = packageName;
  112. }
  113. public String getAccountName() {
  114. return this.accountName;
  115. }
  116. public void setAccountName(String accountName) {
  117. this.accountName = accountName;
  118. }
  119. public boolean getFollowRedirects() {
  120. return this.followRedirects;
  121. }
  122. public boolean validateToken(String token) {
  123. // As discussed with Lukas R. at the Nextcloud Conf 2018, always compare whole strings
  124. // and don't exit prematurely if the string does not match anymore to prevent timing-attacks
  125. return isEqual(this.token.getBytes(), token.getBytes());
  126. }
  127. // Taken from http://codahale.com/a-lesson-in-timing-attacks/
  128. private static boolean isEqual(byte[] a, byte[] b) {
  129. if (a.length != b.length) {
  130. return false;
  131. }
  132. int result = 0;
  133. for (int i = 0; i < a.length; i++) {
  134. result |= a[i] ^ b[i];
  135. }
  136. return result == 0;
  137. }
  138. }