1 package com.neuronrobotics.bowlerstudio.scripting;
3 import java.awt.GraphicsEnvironment;
4 import java.io.BufferedReader;
5 import java.io.Console;
7 import java.io.IOException;
8 import java.io.InputStreamReader;
10 import java.net.URLConnection;
11 import java.nio.charset.StandardCharsets;
12 import java.nio.file.Files;
13 import java.nio.file.Paths;
14 import java.security.GeneralSecurityException;
15 import java.text.DateFormat;
16 import java.text.SimpleDateFormat;
17 import java.util.Arrays;
18 import java.util.Date;
19 import java.util.List;
20 import com.google.crypto.tink.Aead;
21 import com.google.crypto.tink.KeysetHandle;
22 import com.google.crypto.tink.aead.AeadKeyTemplates;
23 import org.eclipse.jgit.transport.CredentialsProvider;
24 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
25 import org.kohsuke.github.GHAuthorization;
26 import org.kohsuke.github.GitHub;
28 import com.google.crypto.tink.CleartextKeysetHandle;
29 import com.google.crypto.tink.JsonKeysetReader;
30 import com.google.crypto.tink.JsonKeysetWriter;
31 import com.google.crypto.tink.config.TinkConfig;
36 private static List<String>
listOfScopes = Arrays.asList(
"repo",
"gist",
"write:packages",
"read:packages",
"delete:packages",
37 "user",
"delete_repo");
41 public String[] prompt(String username) {
44 if (username !=
null) {
45 if (username.equals(
""))
48 String[] creds =
new String[] {
"",
"" };
49 System.out.println(
"#Github Login Prompt#");
50 System.out.println(
"For anynomous mode hit enter twice");
51 System.out.print(
"Github Username: " + (username !=
null ?
"(" + username +
")" :
""));
53 BufferedReader buf =
new BufferedReader(
new InputStreamReader(System.in));
58 }
catch (InterruptedException e) {
64 creds[0] = buf.readLine();
66 }
catch (IOException e) {
70 if (creds[0].equals(
"") && (username ==
null)) {
71 System.out.println(
"No username, using anynomous login");
75 }
while (creds[0] ==
null);
81 if ((cons = System.console()) !=
null
82 && (passwd = cons.readPassword(
"[%s]",
"GitHub Password:")) !=
null) {
83 creds[1] =
new String(passwd);
84 java.util.Arrays.fill(passwd,
' ');
87 if (creds[1].equals(
"")) {
88 System.out.println(
"GitHub Password Cleartext:");
89 creds[1] = buf.readLine();
90 if (creds[1].equals(
"")) {
91 System.out.println(
"No password, using anynomous login");
94 }
catch (Exception e) {
101 public String twoFactorAuthCodePrompt() {
102 System.out.print(
"Github 2 factor temp key: ");
104 BufferedReader buf =
new BufferedReader(
new InputStreamReader(System.in));
107 return buf.readLine().trim();
108 }
catch (IOException e) {
117 TinkConfig.register();
118 }
catch (GeneralSecurityException e) {
126 final URL url =
new URL(
"http://github.com");
127 final URLConnection conn = url.openConnection();
129 conn.getInputStream();
131 }
catch (Exception e) {
143 private static String
pw =
null;
144 private static CredentialsProvider
cp;
165 public static synchronized void login() throws IOException {
185 }
catch (Exception e) {
186 throw new RuntimeException(e);
192 }
catch (Exception e) {
212 System.out.println(
"\nERROR: Wrong Password!\n");
219 private static void performLogin(String u, String p)
throws Exception {
226 byte[] passEncrypt = Files.readAllBytes(Paths.get(
getTokenfile().toURI()));
228 Aead aead =
getKey().getPrimitive(Aead.class);
231 byte[] decrypted = aead.decrypt(passEncrypt,
null);
232 token =
new String(decrypted).trim();
233 }
catch (GeneralSecurityException ex) {
234 ex.printStackTrace();
237 }
catch (IOException e) {
263 gh = GitHub.connect(u, token);
265 if (gh.getRateLimit().getRemaining() < 2) {
266 System.err.println(
"##Github Is Rate Limiting You## Disabling autoupdate");
268 }
catch(java.lang.NoSuchMethodError er) {
269 er.printStackTrace();
271 u=gh.getMyself().getLogin();
273 }
catch (Throwable e1) {
275 e1.printStackTrace();
286 System.out.println(
"\n\nSuccess Login " + u +
"\n\n");
289 }
catch (Exception e) {
310 public static void logout() throws IOException {
326 System.err.println(
"Using anynomous login, autoupdate disabled");
336 if (GraphicsEnvironment.isHeadless()) {
355 List linesu = Files.readAllLines(Paths.get(
getUsernamefile().toURI()), StandardCharsets.UTF_8);
358 KeysetHandle keysetHandle =
getKey();
363 byte[] passEncrypt = Files.readAllBytes(Paths.get(
getPassfile().toURI()));
365 Aead aead = keysetHandle.getPrimitive(Aead.class);
368 byte[] decrypted = aead.decrypt(passEncrypt,
null);
369 String cleartext =
new String(decrypted).trim();
371 }
catch (GeneralSecurityException ex) {
372 ex.printStackTrace();
379 private static KeysetHandle
getKey() throws IOException {
380 KeysetHandle keysetHandle =
null;
381 File keyfile =
new File(
getWorkspace().getAbsoluteFile() +
"/loadData.json");
382 String keysetFilename = keyfile.getAbsolutePath();
383 if (!keyfile.exists()) {
387 keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM);
390 CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withFile(
new File(keysetFilename)));
391 }
catch (GeneralSecurityException e) {
398 keysetHandle = CleartextKeysetHandle.read(JsonKeysetReader.withFile(
new File(keysetFilename)));
399 }
catch (GeneralSecurityException e) {
407 private static void writeData(String user, String passcleartext)
throws Exception {
413 KeysetHandle keysetHandle =
getKey();
417 Aead aead = keysetHandle.getPrimitive(Aead.class);
418 byte[] ciphertext = aead.encrypt(passcleartext.getBytes(),
null);
419 Files.write(Paths.get(
getPassfile().toURI()), ciphertext);
422 private static void writeToken(String user, String passcleartext)
throws Exception {
428 KeysetHandle keysetHandle =
getKey();
432 Aead aead = keysetHandle.getPrimitive(Aead.class);
433 byte[] ciphertext = aead.encrypt(passcleartext.getBytes(),
null);
434 Files.write(Paths.get(
getTokenfile().toURI()), ciphertext);
460 workspace =
new File(System.getProperty(
"user.home") +
"/bowler-workspace/");
469 return new File(
getWorkspace().getAbsoluteFile() +
"/username.json");
474 return new File(
getWorkspace().getAbsoluteFile() +
"/timestamp.json");
479 return new File(
getWorkspace().getAbsoluteFile() +
"/token.json");
static boolean hasStoredToken()
static List< String > listOfScopes
static void setWorkspace(File workspace)
static void writeData(String user, String passcleartext)
static File getUsernamefile()
static List< String > getListOfScopes()
static GitHub setupAnyonmous()
static String getUsername()
static void writeToken(String user, String passcleartext)
static boolean hasNetwork()
static void checkInternet()
static File getPassfile()
static KeysetHandle getKey()
static File getWorkspace()
static boolean hasStoredCredentials()
static void setListOfScopes(List< String > listOfScopes)
static CredentialsProvider getCredentialProvider()
static String getLoginID()
static GitHub getGithub()
static boolean hasnetwork
static void waitForLogin()
static boolean isAnonMode
static synchronized void login()
static void setAnonMode(boolean isAnonMode)
static void performLogin(String u, String p)
static void setCredentialProvider(CredentialsProvider cp)
static IGitHubLoginManager getLoginManager()
static boolean isAnonMode()
static void setLoginManager(IGitHubLoginManager lm)
static void setLoginID(String loginID)
static IGitHubLoginManager loginWebFlow
static boolean loggedIn()
static void loadLoginData(File ws)
static boolean isLoggedIn
static IGitHubLoginManager loginManager
static CredentialsProvider cp
static void setGithub(GitHub g)
static String getPassword()
static File getTokenfile()
static IGitHubLoginManager loginHeadless
String[] prompt(String loginID)