/src/main/java/com/couchbase/mock/http/UserManagementHandler.java

http://github.com/couchbase/CouchbaseMock · Java · 155 lines · 105 code · 22 blank · 28 comment · 29 complexity · cde9894565e3e10fad6b92eaa9914a57 MD5 · raw file

  1. /*
  2. * Copyright 2017 Couchbase, Inc.
  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 com.couchbase.mock.http;
  17. import com.couchbase.mock.CouchbaseMock;
  18. import com.couchbase.mock.JsonUtils;
  19. import com.couchbase.mock.httpio.HandlerUtil;
  20. import com.couchbase.mock.httpio.HttpServer;
  21. import org.apache.http.HttpEntity;
  22. import org.apache.http.HttpEntityEnclosingRequest;
  23. import org.apache.http.HttpException;
  24. import org.apache.http.HttpRequest;
  25. import org.apache.http.HttpResponse;
  26. import org.apache.http.HttpStatus;
  27. import org.apache.http.NameValuePair;
  28. import org.apache.http.client.utils.URLEncodedUtils;
  29. import org.apache.http.protocol.HttpContext;
  30. import org.apache.http.protocol.HttpRequestHandler;
  31. import java.io.IOException;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.regex.Matcher;
  35. import java.util.regex.Pattern;
  36. /**
  37. * @author Mike Goldsmith
  38. */
  39. public class UserManagementHandler {
  40. private final CouchbaseMock mock;
  41. public UserManagementHandler(CouchbaseMock mock) {
  42. this.mock = mock;
  43. }
  44. public void register(HttpServer server) {
  45. server.register("/settings/rbac/users/local*", getUsersHandler);
  46. }
  47. private final HttpRequestHandler getUsersHandler = new HttpRequestHandler() {
  48. @java.lang.Override
  49. public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
  50. String method = httpRequest.getRequestLine().getMethod();
  51. String username = getUsername(httpRequest);
  52. String password = null;
  53. Boolean userExists = mock.getUsers().containsKey(username);
  54. if (method.equals("GET")) {
  55. // if no username, return all users
  56. if (username.isEmpty()) {
  57. ArrayList<User> localUsers = new ArrayList<User>();
  58. localUsers.addAll(mock.getUsers().values());
  59. HandlerUtil.makeJsonResponse(httpResponse, JsonUtils.encode(localUsers));
  60. return;
  61. }
  62. // if user doesn't exist, return bad request
  63. if (!userExists) {
  64. httpResponse.setStatusCode(HttpStatus.SC_NOT_FOUND);
  65. return;
  66. }
  67. // return user as JSON
  68. User user = mock.getUsers().get(username);
  69. HandlerUtil.makeJsonResponse(httpResponse, JsonUtils.encode(user));
  70. return;
  71. } else if (method.equals("PUT") && !username.isEmpty() && httpRequest instanceof HttpEntityEnclosingRequest) {
  72. User user;
  73. ArrayList<Role> roles = new ArrayList<Role>();
  74. if (userExists) {
  75. user = mock.getUsers().get(username);
  76. } else {
  77. user = new User("local", username);
  78. }
  79. // convert query string to key value pairs
  80. HttpEntity entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
  81. List<NameValuePair> content = URLEncodedUtils.parse(entity);
  82. for (NameValuePair pair : content) {
  83. if ("roles".equals(pair.getName())) {
  84. Pattern pattern = Pattern.compile("(.+)(\\[(.+)])");
  85. String rolesEncoded = pair.getValue();
  86. String[] rolesList = rolesEncoded.split(",");
  87. for (String role : rolesList) {
  88. Matcher matcher = pattern.matcher(role);
  89. if (matcher.find()) {
  90. roles.add(new Role(matcher.group(1), matcher.group(3)));
  91. }
  92. }
  93. } else if("password".equals(pair.getName())) {
  94. password = pair.getValue();
  95. } else if ("name".equals(pair.getName())) {
  96. user.setName(pair.getValue());
  97. }
  98. }
  99. // if no roles provided, return bad request
  100. if (roles.size() == 0) {
  101. httpResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
  102. return;
  103. }
  104. // replace role set
  105. user.setRoles(roles);
  106. // if new user and no password provided, return bad request
  107. if (!userExists && (password == null || password.isEmpty())) {
  108. httpResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
  109. return;
  110. }
  111. // add user to user list
  112. mock.getUsers().put(username, user);
  113. return;
  114. } else if (method.equals("DELETE") && !username.isEmpty()) {
  115. // if user exists, remove from list
  116. if (userExists) {
  117. mock.getUsers().remove(username);
  118. return;
  119. }
  120. }
  121. // no good path found, return bad request
  122. httpResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
  123. }
  124. };
  125. private String getUsername(HttpRequest httpRequest) {
  126. String username = "";
  127. String[] parts = httpRequest.getRequestLine().getUri().split("local/");
  128. if (parts.length == 2) {
  129. username = parts[1];
  130. }
  131. return username;
  132. }
  133. }