2 * Copyright (C) 2009-2010, Google Inc. and others
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
8 * SPDX-License-Identifier: BSD-3-Clause
11 package org.eclipse.jgit.http.server.resolver;
13 import javax.servlet.http.HttpServletRequest;
15 import org.eclipse.jgit.lib.Config;
16 import org.eclipse.jgit.lib.PersonIdent;
17 import org.eclipse.jgit.lib.Repository;
18 import org.eclipse.jgit.transport.ReceivePack;
19 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
20 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
21 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
24 * Create and configure {@link org.eclipse.jgit.transport.ReceivePack} service
27 * Writing by receive-pack is permitted if any of the following is true:
29 * <li>The container has authenticated the user and set
30 * {@link javax.servlet.http.HttpServletRequest#getRemoteUser()} to the
32 * <li>The repository configuration file has {@code http.receivepack} explicitly
35 * and explicitly rejected otherwise.
37 public class DefaultReceivePackFactory implements
38 ReceivePackFactory<HttpServletRequest> {
39 private static class ServiceConfig {
42 final boolean enabled;
44 ServiceConfig(Config cfg) {
45 set = cfg.getString("http", null, "receivepack") != null;
46 enabled = cfg.getBoolean("http", "receivepack", false);
52 public ReceivePack create(HttpServletRequest req, Repository db)
53 throws ServiceNotEnabledException, ServiceNotAuthorizedException {
54 final ServiceConfig cfg = db.getConfig().get(ServiceConfig::new);
55 String user = req.getRemoteUser();
59 if (user == null || "".equals(user))
61 return createFor(req, db, user);
63 throw new ServiceNotEnabledException();
66 if (user != null && !"".equals(user))
67 return createFor(req, db, user);
68 throw new ServiceNotAuthorizedException();
71 private static ReceivePack createFor(final HttpServletRequest req,
72 final Repository db, final String user) {
73 final ReceivePack rp = new ReceivePack(db);
74 rp.setRefLogIdent(toPersonIdent(req, user));
78 private static PersonIdent toPersonIdent(HttpServletRequest req, String user) {
79 return new PersonIdent(user, user + "@" + req.getRemoteHost());