]> source.dussan.org Git - jgit.git/blob
16b27e8bd6378e47f15a74579eb21e6fccedc22f
[jgit.git] /
1 /*
2  * Copyright (C) 2009-2010, Google Inc. and others
3  *
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.
7  *
8  * SPDX-License-Identifier: BSD-3-Clause
9  */
10
11 package org.eclipse.jgit.http.server.resolver;
12
13 import javax.servlet.http.HttpServletRequest;
14
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;
22
23 /**
24  * Create and configure {@link org.eclipse.jgit.transport.ReceivePack} service
25  * instance.
26  * <p>
27  * Writing by receive-pack is permitted if any of the following is true:
28  * <ul>
29  * <li>The container has authenticated the user and set
30  * {@link javax.servlet.http.HttpServletRequest#getRemoteUser()} to the
31  * authenticated name.
32  * <li>The repository configuration file has {@code http.receivepack} explicitly
33  * set to true.
34  * </ul>
35  * and explicitly rejected otherwise.
36  */
37 public class DefaultReceivePackFactory implements
38                 ReceivePackFactory<HttpServletRequest> {
39         private static class ServiceConfig {
40                 final boolean set;
41
42                 final boolean enabled;
43
44                 ServiceConfig(Config cfg) {
45                         set = cfg.getString("http", null, "receivepack") != null;
46                         enabled = cfg.getBoolean("http", "receivepack", false);
47                 }
48         }
49
50         /** {@inheritDoc} */
51         @Override
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();
56
57                 if (cfg.set) {
58                         if (cfg.enabled) {
59                                 if (user == null || "".equals(user))
60                                         user = "anonymous";
61                                 return createFor(req, db, user);
62                         }
63                         throw new ServiceNotEnabledException();
64                 }
65
66                 if (user != null && !"".equals(user))
67                         return createFor(req, db, user);
68                 throw new ServiceNotAuthorizedException();
69         }
70
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));
75                 return rp;
76         }
77
78         private static PersonIdent toPersonIdent(HttpServletRequest req, String user) {
79                 return new PersonIdent(user, user + "@" + req.getRemoteHost());
80         }
81 }