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.Username)
62 continue;
63
64 else if (i instanceof CredentialItem.Password)
65 continue;
66
67 else
68 return false;
69 }
70 return true;
71 }
72
73
74 @Override
75 public boolean get(URIish uri, CredentialItem... items)
76 throws UnsupportedCredentialItem {
77 for (CredentialItem i : items) {
78 if (i instanceof CredentialItem.Username) {
79 ((CredentialItem.Username) i).setValue(username);
80 continue;
81 }
82 if (i instanceof CredentialItem.Password) {
83 ((CredentialItem.Password) i).setValue(password);
84 continue;
85 }
86 if (i instanceof CredentialItem.StringType) {
87 if (i.getPromptText().equals("Password: ")) {
88 ((CredentialItem.StringType) i).setValue(new String(
89 password));
90 continue;
91 }
92 }
93 throw new UnsupportedCredentialItem(uri, i.getClass().getName()
94 + ":" + i.getPromptText());
95 }
96 return true;
97 }
98
99
100
101
102 public void clear() {
103 username = null;
104
105 if (password != null) {
106 Arrays.fill(password, (char) 0);
107 password = null;
108 }
109 }
110 }