Skip to content

Commit 8fa9564

Browse files
committed
final commit for 0.1.4 BETA
1 parent 1eab71d commit 8fa9564

File tree

6 files changed

+248
-79
lines changed

6 files changed

+248
-79
lines changed

prenda.war

4.02 MB
Binary file not shown.

src/com/prenda/Mode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ public class Mode {
2929
public static final int DESC = 1;
3030

3131
public static final int ASC = 2;
32+
33+
public static final int PAT = 1;
34+
35+
public static final int JWT = 2;
36+
37+
public static final int OAUTH = 2;
38+
39+
public static final int PKCS1 = 1;
40+
41+
public static final int PKCS8 = 2;
3242
}

src/com/prenda/helper/GithubIssue.java

Lines changed: 152 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,120 @@
1313
import org.json.JSONArray;
1414
import org.json.JSONObject;
1515

16+
import com.prenda.Mode;
17+
1618
public class GithubIssue {
17-
19+
1820
private static Logger log = Logger.getLogger(GithubIssue.class);
19-
20-
public int create(String title, String body, String username, String repo, String [] labels, String [] assignees, String token) {
21+
22+
public int create(String title, String body, String username, String repo, String[] labels, String[] assignees,
23+
int tokenType, String token) {
2124
int issue = 0;
22-
if(exists(title,username,repo)) {
23-
return issue;
25+
try {
26+
if (!exists(title, username, repo)) {
27+
JSONObject json = new JSONObject();
28+
json.put("title", title);
29+
json.put("body", body);
30+
JSONArray jsonArray = new JSONArray();
31+
for (String label : labels) {
32+
jsonArray.put(label);
33+
}
34+
json.put("labels", jsonArray);
35+
jsonArray = new JSONArray();
36+
for (String assignee : assignees) {
37+
jsonArray.put(assignee);
38+
}
39+
json.put("assignees", jsonArray);
40+
log.info(json.toString(3));
41+
byte[] postData = json.toString().getBytes(StandardCharsets.UTF_8);
42+
int postDataLength = postData.length;
43+
String request = "https://api.github.com/repos/" + username + "/" + repo + "/issues";
44+
URL url = new URL(request);
45+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
46+
conn.setDoOutput(true);
47+
conn.setInstanceFollowRedirects(false);
48+
conn.setRequestMethod("POST");
49+
conn.setRequestProperty("charset", "utf-8");
50+
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
51+
conn.setUseCaches(false);
52+
conn.setRequestProperty("Content-Type", "application/json");
53+
if (tokenType == Mode.PAT) { // Personal Access Token only
54+
String encoded = Base64.getEncoder()
55+
.encodeToString((username + ":" + token).getBytes(StandardCharsets.UTF_8));
56+
conn.setRequestProperty("Authorization", "Basic " + encoded);
57+
} else if (tokenType == Mode.JWT) { // JSON Web Token
58+
//if (authenticate()) {
59+
token = getToken(120193);
60+
conn.setRequestProperty("Authorization", "Token " + token);
61+
//}
62+
}
63+
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
64+
wr.write(postData);
65+
int responseCode = conn.getResponseCode();
66+
log.info("Response Code : " + responseCode);
67+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
68+
String inputLine;
69+
StringBuffer response = new StringBuffer();
70+
while ((inputLine = in.readLine()) != null) {
71+
response.append(inputLine);
72+
}
73+
in.close();
74+
log.info(response.toString());
75+
if (responseCode == 201) {
76+
JSONObject myResponse = new JSONObject(response.toString());
77+
issue = myResponse.getInt("number");
78+
log.info("issue: " + issue);
79+
}
80+
}
81+
} catch (Exception e) {
82+
e.printStackTrace();
2483
}
84+
return issue;
85+
}
86+
87+
private String getToken(Integer id) {
88+
String token = "";
2589
try {
26-
JSONObject json = new JSONObject();
27-
json.put("title", title);
28-
json.put("body", body);
29-
JSONArray jsonArray = new JSONArray();
30-
for(String label: labels) {
31-
jsonArray.put(label);
90+
String request = "https://api.github.com/installations/" + id + "/access_tokens";
91+
log.info(request);
92+
URL url = new URL(request);
93+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
94+
conn.setInstanceFollowRedirects(false);
95+
conn.setRequestMethod("POST");
96+
conn.setRequestProperty("Accept", "application/vnd.github.machine-man-preview+json");
97+
conn.setRequestProperty("Authorization", "Bearer " + KeyUtil.getJws());
98+
int responseCode = conn.getResponseCode();
99+
log.info("Response Code : " + responseCode);
100+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
101+
String inputLine;
102+
StringBuffer response = new StringBuffer();
103+
while ((inputLine = in.readLine()) != null) {
104+
response.append(inputLine);
32105
}
33-
json.put("labels", jsonArray);
34-
jsonArray = new JSONArray();
35-
for(String assignee: assignees) {
36-
jsonArray.put(assignee);
106+
in.close();
107+
log.info(response.toString());
108+
if (responseCode == 201) {
109+
JSONObject myResponse = new JSONObject(response.toString());
110+
token = myResponse.getString("token");
111+
log.info("token: " + token);
37112
}
38-
json.put("assignees", jsonArray);
39-
log.info(json.toString(3));
40-
byte[] postData = json.toString().getBytes(StandardCharsets.UTF_8);
41-
int postDataLength = postData.length;
42-
String request = "https://api.github.com/repos/" + username + "/" + repo + "/issues";
113+
} catch (Exception e) {
114+
e.printStackTrace();
115+
}
116+
return token;
117+
}
118+
119+
protected boolean authenticate() {
120+
boolean auth = false;
121+
try {
122+
String request = "https://api.github.com/app";
123+
log.info(request);
43124
URL url = new URL(request);
44125
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
45-
conn.setDoOutput(true);
46126
conn.setInstanceFollowRedirects(false);
47-
conn.setRequestMethod("POST");
48-
conn.setRequestProperty("Content-Type", "application/json");
49-
conn.setRequestProperty("charset", "utf-8");
50-
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
51-
conn.setUseCaches(false);
52-
//Personal Access Token only
53-
String encoded = Base64.getEncoder()
54-
.encodeToString((username + ":" + token).getBytes(StandardCharsets.UTF_8));
55-
conn.setRequestProperty("Authorization", "Basic " + encoded);
56-
/*
57-
KeyUtil ku = new KeyUtil();
58-
conn.setRequestProperty("Authorization", "Bearer " + ku.getJws());
59-
*/
60-
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
61-
wr.write(postData);
127+
conn.setRequestMethod("GET");
128+
conn.setRequestProperty("Accept", "application/vnd.github.machine-man-preview+json");
129+
conn.setRequestProperty("Authorization", "Bearer " + KeyUtil.getJws());
62130
int responseCode = conn.getResponseCode();
63131
log.info("Response Code : " + responseCode);
64132
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
@@ -69,19 +137,20 @@ public int create(String title, String body, String username, String repo, Strin
69137
}
70138
in.close();
71139
log.info(response.toString());
72-
JSONObject myResponse = new JSONObject(response.toString());
73-
issue = myResponse.getInt("number");
74-
log.info("issue: " + issue);
140+
if (responseCode == 200) {
141+
auth = true;
142+
}
75143
} catch (Exception e) {
76-
log.info(e.getMessage());
144+
e.printStackTrace();
77145
}
78-
return issue;
146+
return auth;
79147
}
80148

81149
protected boolean exists(String title, String username, String repo) {
82-
boolean found=false;
150+
boolean found = false;
83151
try {
84-
String request = "https://api.github.com/search/issues?q="+URLEncoder.encode(title,"UTF-8")+"+type:issue+in:title+repo:"+username+"%2f"+repo;
152+
String request = "https://api.github.com/search/issues?q=" + URLEncoder.encode(title, "UTF-8")
153+
+ "+type:issue+in:title+repo:" + username + "%2f" + repo;
85154
log.info(request);
86155
URL url = new URL(request);
87156
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
@@ -97,15 +166,50 @@ protected boolean exists(String title, String username, String repo) {
97166
}
98167
in.close();
99168
log.info(response.toString());
100-
JSONObject myResponse = new JSONObject(response.toString());
101-
int count = myResponse.getInt("total_count");
102-
log.info("total_count: " + count);
103-
if(count>0) {
104-
found = true;
169+
if (responseCode == 200) {
170+
JSONObject myResponse = new JSONObject(response.toString());
171+
int count = myResponse.getInt("total_count");
172+
log.info("total_count: " + count);
173+
if (count > 0) {
174+
found = true;
175+
}
105176
}
106177
} catch (Exception e) {
107178
e.printStackTrace();
108179
}
109180
return found;
110181
}
182+
183+
protected String getATitle(String username, String repo) {
184+
String title = "";
185+
try {
186+
String request = "https://api.github.com/repos/"+username+"/"+repo+"/issues?state=all";
187+
log.info(request);
188+
URL url = new URL(request);
189+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
190+
conn.setInstanceFollowRedirects(false);
191+
conn.setRequestMethod("GET");
192+
int responseCode = conn.getResponseCode();
193+
log.info("Response Code : " + responseCode);
194+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
195+
String inputLine;
196+
StringBuffer response = new StringBuffer();
197+
while ((inputLine = in.readLine()) != null) {
198+
response.append(inputLine);
199+
}
200+
in.close();
201+
log.info(response.toString());
202+
if (responseCode == 200) {
203+
JSONArray myResponse = new JSONArray(response.toString());
204+
JSONObject object = myResponse.getJSONObject(1);
205+
if(object.length()>0) {
206+
title = object.getString("title");
207+
log.info("title: " + title);
208+
}
209+
}
210+
} catch (Exception e) {
211+
e.printStackTrace();
212+
}
213+
return title;
214+
}
111215
}

src/com/prenda/helper/KeyUtil.java

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,119 @@
11
package com.prenda.helper;
22

3+
import java.io.DataInputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
37
import java.io.FileReader;
8+
import java.io.InputStream;
9+
import java.io.OutputStream;
10+
import java.net.HttpURLConnection;
11+
import java.net.URL;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
415
import java.security.Key;
16+
import java.security.KeyFactory;
517
import java.security.KeyPair;
6-
import java.util.Date;
18+
import java.security.spec.PKCS8EncodedKeySpec;
19+
import java.util.Base64;
720
import java.util.GregorianCalendar;
821
import java.util.Properties;
22+
import java.util.TimeZone;
923

1024
import org.apache.log4j.Logger;
25+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1126
import org.bouncycastle.openssl.PEMKeyPair;
1227
import org.bouncycastle.openssl.PEMParser;
1328
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
14-
29+
import com.prenda.Mode;
1530
import com.prenda.servlet.RegisterOwner;
1631

32+
import io.jsonwebtoken.JwtBuilder;
1733
import io.jsonwebtoken.Jwts;
1834
import io.jsonwebtoken.SignatureAlgorithm;
1935

2036
public class KeyUtil {
2137

2238
private static Logger log = Logger.getLogger(KeyUtil.class);
2339

24-
public String getJws() {
40+
public static String getJws() {
2541
String jws="";
2642
try {
2743
Properties props = new Properties();
2844
props.load(RegisterOwner.class.getResourceAsStream("/env.properties"));
2945
String path = props.getProperty("github.pem");
46+
Path p = Paths.get(path);
47+
if(Files.notExists(p)) {
48+
download(p);
49+
}
3050
GregorianCalendar gc = new GregorianCalendar();
31-
gc.add(GregorianCalendar.MINUTE, 10);
32-
jws = Jwts.builder().setIssuer("10575").setIssuedAt(new Date()).setExpiration(gc.getTime())
33-
.signWith(SignatureAlgorithm.RS256, getPemPrivateKey(path, "BC")).compact();
51+
gc.setTimeZone(TimeZone.getTimeZone("UTC"));
52+
JwtBuilder builder = Jwts.builder();
53+
builder = builder.setIssuer("10575");
54+
builder = builder.setIssuedAt(gc.getTime());
55+
gc.add(GregorianCalendar.MINUTE, 1);
56+
builder = builder.setExpiration(gc.getTime());
57+
jws = builder.signWith(SignatureAlgorithm.RS256, getPemPrivateKey(path, Mode.PKCS1)).compact();
3458
log.info("jws: "+ jws);
3559
} catch (Exception e) {
3660
e.printStackTrace();
3761
}
3862
return jws;
3963
}
4064

41-
public Key getPemPrivateKey(String filename, String provider) {
65+
private static void download(Path path) {
66+
try {
67+
String filename = path.getFileName().toString();
68+
String request = "https://ex.kenchlightyear.com/" + filename;
69+
log.info(request);
70+
URL url = new URL(request);
71+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
72+
conn.setInstanceFollowRedirects(false);
73+
conn.setRequestMethod("GET");
74+
int responseCode = conn.getResponseCode();
75+
log.info("Response Code : " + responseCode);
76+
InputStream in = conn.getInputStream();
77+
OutputStream outputStream = new FileOutputStream(path.toString());
78+
byte[] buffer = new byte[2048];
79+
int length;
80+
while ((length = in.read(buffer)) != -1) {
81+
outputStream.write(buffer, 0, length);
82+
}
83+
in.close();
84+
outputStream.close();
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
}
88+
}
89+
90+
public static Key getPemPrivateKey(String filename, int pkcs) {
4291
Key key = null;
4392
try {
44-
/* PKCS8
4593
File f = new File(filename);
4694
FileInputStream fis = new FileInputStream(f);
4795
DataInputStream dis = new DataInputStream(fis);
4896
byte[] keyBytes = new byte[(int) f.length()];
4997
dis.readFully(keyBytes);
5098
dis.close();
51-
5299
String temp = new String(keyBytes);
53-
String privKeyPEM = temp.replace("-----BEGIN RSA PRIVATE KEY-----\n", ""); //PCKS1 is -----BEGIN PRIVATE KEY-----\n
54-
privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", ""); // //PCKS1 is -----END PRIVATE KEY-----
55-
log.info("Private key\n"+privKeyPEM);
56-
57-
byte[] decoded = Base64.getDecoder().decode(privKeyPEM); // TextCodec.BASE64.decode(privKeyPEM); //
58-
59-
/*PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
60-
KeyFactory kf = KeyFactory.getInstance(algorithm);
61-
rSAPrivateKey = (RSAPrivateKey) kf.generatePrivate(spec);*/
62-
63-
PEMParser pemParser = new PEMParser(new FileReader(filename));
64-
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(provider);
65-
Object object = pemParser.readObject();
66-
KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
67-
key = (Key) kp.getPrivate();
100+
String privKeyPEM ="";
101+
if(pkcs == Mode.PKCS8) {
102+
privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
103+
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
104+
log.info("Private key\n"+privKeyPEM);
105+
byte[] decoded = Base64.getDecoder().decode(privKeyPEM); // TextCodec.BASE64.decode(privKeyPEM); //
106+
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
107+
KeyFactory kf = KeyFactory.getInstance("RSA");
108+
key = (Key) kf.generatePrivate(spec);
109+
}else if (pkcs == Mode.PKCS1) {
110+
PEMParser pemParser = new PEMParser(new FileReader(filename));
111+
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(new BouncyCastleProvider());
112+
Object object = pemParser.readObject();
113+
KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
114+
key = (Key) kp.getPrivate();
115+
pemParser.close();
116+
}
68117
} catch (Exception e) {
69118
e.printStackTrace();
70119
}

0 commit comments

Comments
 (0)