123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /*
- * Nextcloud SingleSignOn
- *
- * @author David Luhmer
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.nextcloud.android.sso.aidl;
- import java.io.Serializable;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class NextcloudRequest implements Serializable {
- private static final long serialVersionUID = 215521212534238L; //assign a long value
- private String method;
- private Map<String, List<String>> header = new HashMap<>();
- private Map<String, String> parameter = new HashMap<>();
- private String requestBody;
- private String url;
- private String token;
- private String packageName;
- private String accountName;
- private boolean followRedirects = false;
- private NextcloudRequest() { }
- public static class Builder {
- private NextcloudRequest ncr;
- public Builder() {
- ncr = new NextcloudRequest();
- }
- public NextcloudRequest build() {
- return ncr;
- }
- public Builder setMethod(String method) {
- ncr.method = method;
- return this;
- }
- public Builder setHeader(Map<String, List<String>> header) {
- ncr.header = header;
- return this;
- }
- public Builder setParameter(Map<String, String> parameter) {
- ncr.parameter = parameter;
- return this;
- }
- public Builder setRequestBody(String requestBody) {
- ncr.requestBody = requestBody;
- return this;
- }
- public Builder setUrl(String url) {
- ncr.url = url;
- return this;
- }
- public Builder setToken(String token) {
- ncr.token = token;
- return this;
- }
- public Builder setPackageName(String packageName) {
- ncr.packageName = packageName;
- return this;
- }
- public Builder setAccountName(String accountName) {
- ncr.accountName = accountName;
- return this;
- }
- /**
- * Default: true
- * @param followRedirects
- * @return
- */
- public Builder setFollowRedirects(boolean followRedirects) {
- ncr.followRedirects = followRedirects;
- return this;
- }
- }
- public String getMethod() {
- return this.method;
- }
- public Map<String, List<String>> getHeader() {
- return this.header;
- }
- public Map<String, String> getParameter() {
- return this.parameter;
- }
- public String getRequestBody() {
- return this.requestBody;
- }
- public String getUrl() {
- return this.url;
- }
- public String getToken() {
- return this.token;
- }
- public void setToken(String token) {
- this.token = token;
- }
- public String getPackageName() {
- return this.packageName;
- }
- public void setPackageName(String packageName) {
- this.packageName = packageName;
- }
- public String getAccountName() {
- return this.accountName;
- }
- public void setAccountName(String accountName) {
- this.accountName = accountName;
- }
- public boolean getFollowRedirects() {
- return this.followRedirects;
- }
- public boolean validateToken(String token) {
- // As discussed with Lukas R. at the Nextcloud Conf 2018, always compare whole strings
- // and don't exit prematurely if the string does not match anymore to prevent timing-attacks
- return isEqual(this.token.getBytes(), token.getBytes());
- }
- // Taken from http://codahale.com/a-lesson-in-timing-attacks/
- private static boolean isEqual(byte[] a, byte[] b) {
- if (a.length != b.length) {
- return false;
- }
- int result = 0;
- for (int i = 0; i < a.length; i++) {
- result |= a[i] ^ b[i];
- }
- return result == 0;
- }
- }
|