Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. package com.microfocus.intellij.plugin.gitclient.credential;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7.  
  8. /**
  9. * @author Rakovets Yurii
  10. * class for handle with credential
  11. */
  12.  
  13. public class CredentialManager {
  14. private static CredentialManager instance;
  15.  
  16. private CredentialManager() {
  17. }
  18.  
  19. public static CredentialManager getInstance() {
  20. if (instance == null) {
  21. instance = new CredentialManager();
  22. }
  23. return instance;
  24. }
  25.  
  26. public Credential getCredential(String host) {
  27. final String param = "protocol=%s\nhost=%s\nquit=true\n\n";
  28. final String userParam = "username=";
  29. final String passParam = "password=";
  30. String username = "";
  31. String password = "";
  32.  
  33. ProcessBuilder pb = new ProcessBuilder("git", "credential", "fill");
  34. try {
  35. Process p = pb.start();
  36. OutputStreamWriter osw = new OutputStreamWriter(p.getOutputStream());
  37. osw.write(String.format(param, "dimensions", host));
  38. osw.close();
  39.  
  40. new Thread(() -> {
  41. try {
  42. BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  43. String s;
  44. while ((s = br.readLine()) != null) {
  45. // git sends "Logon failed" to stderr if user presses Cancel
  46. // then it waits for username and password from stdin
  47. // we don't want to this to happen
  48. if (!s.contains("told us to quit")) {
  49. System.out.print(s);
  50. System.out.print("\n");
  51. } else {
  52. p.destroyForcibly();
  53. break;
  54. }
  55. }
  56. } catch (IOException e) {
  57. }
  58. }).start();
  59.  
  60. if (p.waitFor() == 0) {
  61. BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
  62. String s;
  63. while ((s = input.readLine()) != null) {
  64. if (s.startsWith(userParam)) {
  65. username = s.substring(userParam.length());
  66. }
  67. if (s.startsWith(passParam)) {
  68. password = s.substring(passParam.length());
  69. }
  70. }
  71. }
  72.  
  73. } catch (IOException | InterruptedException e) {
  74. e.printStackTrace();
  75. }
  76.  
  77. return new Credential(username, password);
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement