Example #1
0
  private static AuthConfig.Builder parseDockerConfig(final Path configPath, String serverAddress)
      throws IOException {
    checkNotNull(configPath);
    final AuthConfig.Builder authBuilder = AuthConfig.builder();
    final JsonNode authJson = extractAuthJson(configPath);

    if (isNullOrEmpty(serverAddress)) {
      final Iterator<String> servers = authJson.fieldNames();
      if (servers.hasNext()) {
        serverAddress = servers.next();
      }
    } else {
      if (!authJson.has(serverAddress)) {
        log.error("Could not find auth config for {}. Returning empty builder", serverAddress);
        return AuthConfig.builder().serverAddress(serverAddress);
      }
    }

    final JsonNode serverAuth = authJson.get(serverAddress);
    if (serverAuth != null && serverAuth.has("auth")) {
      authBuilder.serverAddress(serverAddress);
      final String authString = serverAuth.get("auth").asText();
      final String[] authParams = Base64.decodeAsString(authString).split(":");

      if (authParams.length == 2) {
        authBuilder.username(authParams[0].trim());
        authBuilder.password(authParams[1].trim());
      } else {
        log.warn("Failed to parse auth string for {}", serverAddress);
        return authBuilder;
      }
    } else {
      log.warn("Could not find auth field for {}", serverAddress);
      return authBuilder;
    }

    if (serverAuth.has("email")) {
      authBuilder.email(serverAuth.get("email").asText());
    }

    return authBuilder;
  }
Example #2
0
  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    List<String> authHeader = requestContext.getHeaders().get(AUTHORIZATION_KEY_NAME);
    if (requestContext.getUriInfo().getAbsolutePath().getPath().contains(SECURED_URL)
        && authHeader != null
        && authHeader.size() > 0) {
      String authToken = authHeader.get(0);
      authToken = authToken.replace(AUTHORIZATION_BASIC_PREFIX, "");
      String authTokenDecoded = Base64.decodeAsString(authToken.getBytes("UTF-8"));
      StringTokenizer tokenizer = new StringTokenizer(authTokenDecoded, ":");
      String userName = tokenizer.nextToken();
      String password = tokenizer.nextToken();
      if (userName.equalsIgnoreCase("user") && password.equalsIgnoreCase("password")) return;
    }

    Response unauthorizedStatus =
        Response.status(Response.Status.UNAUTHORIZED)
            .entity("User cannot Access the resource")
            .build();
    requestContext.abortWith(unauthorizedStatus);
  }