use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class AbstractBitbucketScm method validateAndCreate.
/**
* Request payload:
* {
* "userName": "joe",
* "password":"****",
* "apiUrl":"mybitbucketserver.com"
* }
* @param request userName and password of bitbucket server
*
* @return credential id
*/
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
User authenticatedUser = User.current();
if (authenticatedUser == null) {
throw new ServiceException.UnauthorizedException("No logged in user found");
}
String userName = (String) request.get("userName");
String password = (String) request.get("password");
String apiUrl = (String) request.get("apiUrl");
validate(userName, password, apiUrl);
final StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, createCredentialId(apiUrl), "Bitbucket server credentials", userName, password);
// if credentials are wrong, this call will fail with 401 error
validateCredential(apiUrl, credential);
StandardUsernamePasswordCredentials bbCredentials = CredentialsUtils.findCredential(createCredentialId(apiUrl), StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
try {
if (bbCredentials == null) {
CredentialsUtils.createCredentialsInUserStore(credential, authenticatedUser, getDomainId(), Collections.singletonList(new BlueOceanDomainSpecification()));
} else {
CredentialsUtils.updateCredentialsInUserStore(bbCredentials, credential, authenticatedUser, getDomainId(), Collections.singletonList(new BlueOceanDomainSpecification()));
}
return createResponse(credential.getId());
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException(e.getMessage());
}
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class GithubScm method validateAndCreate.
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
String accessToken = (String) request.get("accessToken");
if (accessToken == null) {
throw new ServiceException.BadRequestException("accessToken is required");
}
accessToken = accessToken.trim();
try {
User authenticatedUser = getAuthenticatedUser();
HttpURLConnection connection = connect(String.format("%s/%s", getUri(), "user"), accessToken);
validateAccessTokenScopes(connection);
String data = IOUtils.toString(HttpRequest.getInputStream(connection), Charset.defaultCharset());
GHUser user = GithubScm.getMappingObjectReader().forType(GHUser.class).readValue(data);
if (user.getEmail() != null) {
Mailer.UserProperty p = authenticatedUser.getProperty(Mailer.UserProperty.class);
// the one from Github?
if (p == null) {
authenticatedUser.addProperty(new Mailer.UserProperty(user.getEmail()));
}
}
// Now we know the token is valid. Lets find credential
String credentialId = createCredentialId(getUri());
StandardUsernamePasswordCredentials githubCredential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
final StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, credentialId, getCredentialDescription(), authenticatedUser.getId(), accessToken);
if (githubCredential == null) {
CredentialsUtils.createCredentialsInUserStore(credential, authenticatedUser, getCredentialDomainName(), Collections.singletonList(new BlueOceanDomainSpecification()));
} else {
CredentialsUtils.updateCredentialsInUserStore(githubCredential, credential, authenticatedUser, getCredentialDomainName(), Collections.singletonList(new BlueOceanDomainSpecification()));
}
return createResponse(credential.getId());
} catch (IOException e) {
if (e instanceof MalformedURLException || e instanceof UnknownHostException) {
throw new ServiceException.BadRequestException(new ErrorMessage(400, "Invalid apiUrl").add(new ErrorMessage.Error("apiUrl", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage())));
}
throw new ServiceException.UnexpectedErrorException(e.getMessage());
}
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class CredentialApiTest method listCredentials.
@Test
public void listCredentials() throws IOException {
SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
CredentialsStore systemStore = system.getStore(j.getInstance());
systemStore.addDomain(new Domain("domain1", null, null));
systemStore.addCredentials(systemStore.getDomainByName("domain1"), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "admin", "pass$wd"));
CredentialsStoreAction credentialsStoreAction = ExtensionList.lookup(ViewCredentialsAction.class).get(0).getStore("system");
CredentialsStoreAction.DomainWrapper domainWrapper = credentialsStoreAction.getDomain("domain1");
CredentialsStoreAction.CredentialsWrapper credentialsWrapper = domainWrapper.getCredentialsList().get(0);
List<Map> creds = get("/organizations/jenkins/credentials/system/domains/domain1/credentials/", List.class);
Assert.assertEquals(1, creds.size());
Map cred = creds.get(0);
Assert.assertNotNull(cred.get("id"));
Map cred1 = get("/organizations/jenkins/credentials/system/domains/domain1/credentials/" + cred.get("id") + "/");
Assert.assertEquals(credentialsWrapper.getId(), cred1.get("id"));
Assert.assertEquals(credentialsWrapper.getTypeName(), cred1.get("typeName"));
Assert.assertEquals(credentialsWrapper.getDisplayName(), cred1.get("displayName"));
Assert.assertEquals(credentialsWrapper.getFullName(), cred1.get("fullName"));
Assert.assertEquals(String.format("%s:%s:%s", credentialsWrapper.getDisplayName(), credentialsWrapper.getDomain().getUrlName(), credentialsWrapper.getTypeName()), cred1.get("description"));
Assert.assertEquals(credentialsWrapper.getDomain().getUrlName(), cred1.get("domain"));
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class CredentialApiTest method listAllCredentials.
@Test
public void listAllCredentials() throws IOException {
SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
CredentialsStore systemStore = system.getStore(j.getInstance());
systemStore.addDomain(new Domain("domain1", null, null));
systemStore.addDomain(new Domain("domain2", null, null));
systemStore.addCredentials(systemStore.getDomainByName("domain1"), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "admin", "pass$wd"));
systemStore.addCredentials(systemStore.getDomainByName("domain2"), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "joe", "pass$wd"));
CredentialsStoreAction credentialsStoreAction = ExtensionList.lookup(ViewCredentialsAction.class).get(0).getStore("system");
CredentialsStoreAction.DomainWrapper domain1 = credentialsStoreAction.getDomain("domain1");
CredentialsStoreAction.DomainWrapper domain2 = credentialsStoreAction.getDomain("domain2");
CredentialsStoreAction.CredentialsWrapper credentials1 = domain1.getCredentialsList().get(0);
CredentialsStoreAction.CredentialsWrapper credentials2 = domain2.getCredentialsList().get(0);
List<Map> creds = get("/search?q=type:credential;organization:jenkins", List.class);
Assert.assertEquals(2, creds.size());
Assert.assertEquals(credentials1.getId(), creds.get(0).get("id"));
Assert.assertEquals(credentials2.getId(), creds.get(1).get("id"));
creds = get("/search?q=type:credential;organization:jenkins;domain:domain2", List.class);
Assert.assertEquals(1, creds.size());
Assert.assertEquals(credentials2.getId(), creds.get(0).get("id"));
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class GithubOrgFolderTest method shouldFindUserStoreCredential.
@Test
public void shouldFindUserStoreCredential() throws IOException {
//add username password credential to user's credential store in user domain and in USER scope
User user = login();
CredentialsStore store = null;
for (CredentialsStore s : CredentialsProvider.lookupStores(user)) {
if (s.hasPermission(CredentialsProvider.CREATE) && s.hasPermission(CredentialsProvider.UPDATE)) {
store = s;
break;
}
}
assertNotNull(store);
store.addDomain(new Domain("github-domain", "Github Domain to store personal access token", Collections.<DomainSpecification>singletonList(new BlueOceanDomainSpecification())));
Domain domain = store.getDomainByName("github-domain");
StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "Github Access Token", user.getId(), "12345");
store.addCredentials(domain, credential);
//create another credentials with same id in system store with different description
for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
s.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "System Github Access Token", user.getId(), "12345"));
}
//create org folder and attach user and credential id to it
OrganizationFolder organizationFolder = j.createProject(OrganizationFolder.class, "demo");
AbstractFolderProperty prop = new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credential.getId(), BlueOceanCredentialsProvider.createDomain("https://api.github.com"));
organizationFolder.addProperty(prop);
// lookup for created credential id in system store, it should resolve to previously created user store credential
StandardCredentials c = Connector.lookupScanCredentials(organizationFolder, "https://api.github.com", credential.getId());
assertEquals("Github Access Token", c.getDescription());
assertNotNull(c);
assertTrue(c instanceof StandardUsernamePasswordCredentials);
StandardUsernamePasswordCredentials usernamePasswordCredentials = (StandardUsernamePasswordCredentials) c;
assertEquals(credential.getId(), usernamePasswordCredentials.getId());
assertEquals(credential.getPassword().getPlainText(), usernamePasswordCredentials.getPassword().getPlainText());
assertEquals(credential.getUsername(), usernamePasswordCredentials.getUsername());
//check the domain
Domain d = CredentialsUtils.findDomain(credential.getId(), user);
assertNotNull(d);
assertTrue(d.test(new BlueOceanDomainRequirement()));
//now remove this property
organizationFolder.getProperties().remove(prop);
//it must resolve to system credential
c = Connector.lookupScanCredentials(organizationFolder, null, credential.getId());
assertEquals("System Github Access Token", c.getDescription());
}
Aggregations