James Moger
2012-11-25 c8b26c51aa67fc9345b624e36aab6f819e7eed74
commit | author | age
e8c417 1 /*
JM 2  * Copyright 2012 gitblit.com.
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.gitblit.authority;
17
18 import java.util.Date;
19
20 import org.eclipse.jgit.lib.Config;
21 import org.eclipse.jgit.lib.Config.SectionParser;
22
23 import com.gitblit.utils.StringUtils;
24 import com.gitblit.utils.TimeUtils;
25 import com.gitblit.utils.X509Utils.X509Metadata;
26
27 /**
28  * Certificate config file parser.
29  *  
30  * @author James Moger
31  */
32 public class NewCertificateConfig {
33         public static final SectionParser<NewCertificateConfig> KEY = new SectionParser<NewCertificateConfig>() {
34             public NewCertificateConfig parse(final Config cfg) {
35                 return new NewCertificateConfig(cfg);
36             }
37         };
38
c8b26c 39         public String OU;
JM 40         public String O;
41         public String L;
42         public String ST;
43         public String C;
e8c417 44         
c8b26c 45         public int duration;
e8c417 46         
JM 47         private NewCertificateConfig(final Config c) {
48             duration = c.getInt("new",  null, "duration", 0);
49             OU = c.getString("new", null, "organizationalUnit");
50             O = c.getString("new", null, "organization");
51             L = c.getString("new", null, "locality");
52             ST = c.getString("new", null, "stateProvince");
53             C = c.getString("new", null, "countryCode");            
54         }
55         
56         public void update(X509Metadata metadata) {
57             update(metadata, "OU", OU);
58             update(metadata, "O", O);
59             update(metadata, "L", L);
60             update(metadata, "ST", ST);
61             update(metadata, "C", C);
62             if (duration > 0) {
63                 metadata.notAfter = new Date(System.currentTimeMillis() + duration*TimeUtils.ONEDAY);
64             }
65         }
66         
67         private void update(X509Metadata metadata, String oid, String value) {
68             if (!StringUtils.isEmpty(value)) {
69                 metadata.oids.put(oid, value);
70             }
71         }
c8b26c 72         
JM 73         public void store(Config c, X509Metadata metadata) {
74             store(c, "new", "organizationalUnit", metadata.getOID("OU", null));
75             store(c, "new", "organization", metadata.getOID("O", null));
76             store(c, "new", "locality", metadata.getOID("L", null));
77             store(c, "new", "stateProvince", metadata.getOID("ST", null));
78             store(c, "new", "countryCode", metadata.getOID("C", null));
79             if (duration <= 0) {
80                 c.unset("new", null, "duration");
81             } else {
82                 c.setInt("new", null, "duration", duration);
83             }
84         }
85         
86         private void store(Config c, String section, String name, String value) {
87             if (StringUtils.isEmpty(value)) {
88                 c.unset(section, null, name);
89             } else {
90                 c.setString(section, null, name, value);
91             }
92         }
e8c417 93     }