1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import java.util.Arrays;
14
15 import org.eclipse.jgit.errors.UnsupportedCredentialItem;
16
17
18
19
20
21 public class UsernamePasswordCredentialsProvider extends CredentialsProvider {
22 private String username;
23
24 private char[] password;
25
26
27
28
29
30
31
32
33
34 public UsernamePasswordCredentialsProvider(String username, String password) {
35 this(username, password.toCharArray());
36 }
37
38
39
40
41
42
43
44
45
46 public UsernamePasswordCredentialsProvider(String username, char[] password) {
47 this.username = username;
48 this.password = password;
49 }
50
51
52 @Override
53 public boolean isInteractive() {
54 return false;
55 }
56
57
58 @Override
59 public boolean supports(CredentialItem... items) {
60 for (CredentialItem i : items) {
61 if (i instanceof CredentialItem.InformationalMessage) {
62 continue;
63 }
64 if (i instanceof CredentialItem.Username) {
65 continue;
66 }
67 if (i instanceof CredentialItem.Password) {
68 continue;
69 }
70 if (i instanceof CredentialItem.StringType) {
71 if (i.getPromptText().equals("Password: ")) {
72 continue;
73 }
74 }
75 return false;
76 }
77 return true;
78 }
79
80
81 @Override
82 public boolean get(URIish uri, CredentialItem... items)
83 throws UnsupportedCredentialItem {
84 for (CredentialItem i : items) {
85 if (i instanceof CredentialItem.InformationalMessage) {
86 continue;
87 }
88 if (i instanceof CredentialItem.Username) {
89 ((CredentialItem.Username) i).setValue(username);
90 continue;
91 }
92 if (i instanceof CredentialItem.Password) {
93 ((CredentialItem.Password) i).setValue(password);
94 continue;
95 }
96 if (i instanceof CredentialItem.StringType) {
97 if (i.getPromptText().equals("Password: ")) {
98 ((CredentialItem.StringType) i).setValue(new String(
99 password));
100 continue;
101 }
102 }
103 throw new UnsupportedCredentialItem(uri, i.getClass().getName()
104 + ":" + i.getPromptText());
105 }
106 return true;
107 }
108
109
110
111
112 public void clear() {
113 username = null;
114
115 if (password != null) {
116 Arrays.fill(password, (char) 0);
117 password = null;
118 }
119 }
120 }