-
Notifications
You must be signed in to change notification settings - Fork 526
/
CredentialsApi.js
93 lines (78 loc) · 2.9 KB
/
CredentialsApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { capabilityAugmenter, Fetch, UrlConfig, Utils, AppConfig } from '@jenkins-cd/blueocean-core-js';
const DOMAIN = 'blueocean-git-domain';
const SCOPE = 'USER';
function getCredentialsUrl(organization) {
const path = UrlConfig.getRestBaseURL();
return Utils.cleanSlashes(`${path}/organizations/${organization}/credentials/user/`);
}
export class CredentialsApi {
constructor(fetch) {
this._fetch = fetch || Fetch.fetchJSON;
this.organization = AppConfig.getOrganizationName();
}
listAllCredentials() {
const path = UrlConfig.getRestBaseURL();
const searchUrl = Utils.cleanSlashes(`${path}/search?q=type:credential;organization:${this.organization}`, false);
return this._fetch(searchUrl)
.then(data => capabilityAugmenter.augmentCapabilities(data))
.then(creds => this._listAllCredentialsSuccess(creds), error => this._listAllCredentialsFailure(error));
}
_listAllCredentialsSuccess(credentials) {
return {
success: true,
credentials,
};
}
_listAllCredentialsFailure() {
return {
success: false,
};
}
saveUsernamePasswordCredential(username, password) {
const requestUrl = getCredentialsUrl(this.organization);
const requestBody = {
credentials: {
$class: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
'stapler-class': 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
scope: SCOPE,
domain: DOMAIN,
username,
password,
description: null,
},
};
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
};
return this._fetch(requestUrl, { fetchOptions });
}
saveSSHKeyCredential(privateKey) {
const requestUrl = getCredentialsUrl(this.organization);
const requestBody = {
credentials: {
$class: 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey',
scope: SCOPE,
domain: DOMAIN,
username: null,
passphrase: null,
description: null,
privateKeySource: {
privateKey,
'stapler-class': 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource',
},
},
};
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
};
return this._fetch(requestUrl, { fetchOptions });
}
}