public Request authenticate(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.httpUrl(); int size = challenges.size(); for (int i = 0; i < size; i++) { Challenge challenge = (Challenge) challenges.get(i); if ("Basic".equalsIgnoreCase(challenge.getScheme())) { PasswordAuthentication auth = java.net.Authenticator .requestPasswordAuthentication(url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), challenge.getRealm(), challenge .getScheme(), url.url(), RequestorType.SERVER); if (auth != null) { return request.newBuilder().header("Authorization", Credentials.basic(auth .getUserName(), new String(auth.getPassword()))).build(); } } } return null; }
public Request authenticateProxy(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.httpUrl(); int size = challenges.size(); for (int i = 0; i < size; i++) { Challenge challenge = (Challenge) challenges.get(i); if ("Basic".equalsIgnoreCase(challenge.getScheme())) { InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); PasswordAuthentication auth = java.net.Authenticator .requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url .scheme(), challenge.getRealm(), challenge.getScheme(), url.url(), RequestorType.PROXY); if (auth != null) { return request.newBuilder().header("Proxy-Authorization", Credentials.basic (auth.getUserName(), new String(auth.getPassword()))).build(); } } } return null; }
public final Request authenticate(Proxy paramProxy, Response paramResponse) throws IOException { List localList = paramResponse.challenges(); Request localRequest = paramResponse.request; URL localURL = localRequest.url(); int i = 0; int j = localList.size(); while (i < j) { Challenge localChallenge = (Challenge)localList.get(i); if ("Basic".equalsIgnoreCase(localChallenge.scheme)) { PasswordAuthentication localPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(localURL.getHost(), getConnectToInetAddress(paramProxy, localURL), localURL.getPort(), localURL.getProtocol(), localChallenge.realm, localChallenge.scheme, localURL, Authenticator.RequestorType.SERVER); if (localPasswordAuthentication != null) { String str = Credentials.basic(localPasswordAuthentication.getUserName(), new String(localPasswordAuthentication.getPassword())); return localRequest.newBuilder().header("Authorization", str).build(); } } i++; } return null; }
public final Request authenticateProxy(Proxy paramProxy, Response paramResponse) throws IOException { List localList = paramResponse.challenges(); Request localRequest = paramResponse.request; URL localURL = localRequest.url(); int i = 0; int j = localList.size(); while (i < j) { Challenge localChallenge = (Challenge)localList.get(i); if ("Basic".equalsIgnoreCase(localChallenge.scheme)) { InetSocketAddress localInetSocketAddress = (InetSocketAddress)paramProxy.address(); PasswordAuthentication localPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(localInetSocketAddress.getHostName(), getConnectToInetAddress(paramProxy, localURL), localInetSocketAddress.getPort(), localURL.getProtocol(), localChallenge.realm, localChallenge.scheme, localURL, Authenticator.RequestorType.PROXY); if (localPasswordAuthentication != null) { String str = Credentials.basic(localPasswordAuthentication.getUserName(), new String(localPasswordAuthentication.getPassword())); return localRequest.newBuilder().header("Proxy-Authorization", str).build(); } } i++; } return null; }
@NonNull private RequestInterceptor getRequestInterceptor() { return new RequestInterceptor() { @Override public void intercept(RequestFacade request) { User user = UserInfoMgr.getInstance().getUser(); if (user != null) { String basic = Credentials.basic(user.getUsername(), user.getPassword()); request.addHeader("Authorization", basic); request.addHeader("UserName", user.getUsername()); request.addHeader("FacilityName", user.getFacilityName()); } addDeviceInfoToRequestHeader(request); } }; }
@Override public Request authenticate(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); URL url = request.url(); for (int i = 0, size = challenges.size(); i < size; i++) { Challenge challenge = challenges.get(i); if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication( url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url, RequestorType.SERVER); if (auth == null) continue; String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword())); return request.newBuilder() .header("Authorization", credential) .build(); } return null; }
@Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); URL url = request.url(); for (int i = 0, size = challenges.size(); i < size; i++) { Challenge challenge = challenges.get(i); if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication( proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url, RequestorType.PROXY); if (auth == null) continue; String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword())); return request.newBuilder() .header("Proxy-Authorization", credential) .build(); } return null; }
private Request createHttpProxyRequest(InetSocketAddress address, String proxyUsername, String proxyPassword) { HttpUrl tunnelUrl = new HttpUrl.Builder() .scheme("https") .host(address.getHostName()) .port(address.getPort()) .build(); Request.Builder request = new Request.Builder() .url(tunnelUrl) .header("Host", tunnelUrl.host() + ":" + tunnelUrl.port()) .header("User-Agent", userAgent); // If we have proxy credentials, set them right away if (proxyUsername != null && proxyPassword != null) { request.header("Proxy-Authorization", Credentials.basic(proxyUsername, proxyPassword)); } return request.build(); }
@Override public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { if (username == null && password == null) { return; } headerParams.put("Authorization", Credentials.basic( username == null ? "" : username, password == null ? "" : password)); }
/** * * @param params * @return */ @Override protected String doInBackground(Void... params) { OkHttpClient client = new OkHttpClient(); String cred = Credentials.basic(_key, _secret); FormEncodingBuilder formBody = new FormEncodingBuilder(); formBody.add("grant_type", "client_credentials"); Request request = new Request.Builder() .url(TWITTER_AUTH_URL) .post(formBody.build()) .addHeader("Authorization", cred) .addHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8") .addHeader("Accept-Encoding", "gzip") .build(); try { Response response = client.newCall(request).execute(); if (isZipped(response)) { return unzip(response.body()); } else { return response.body().string(); } } catch (IOException e) { e.printStackTrace(); } return null; }
public GcmRegistrationStatus sendGcmTokenToServer() throws Exception { if (mJenkinsUserEntity == null) { return null; } // Retrieve crumbInfo CrumbInfoEntity crumbInfoEntity = retrieveCrumbInfo(); if (crumbInfoEntity != null && crumbInfoEntity.isCsrfEnabled()) { setHeader(crumbInfoEntity.getCrumbRequestField(), crumbInfoEntity.getCrumb()); } FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder(); formEncodingBuilder.add("token", mJenkinsUserEntity.getSenderId()); String credential = Credentials .basic(mJenkinsUserEntity.getUsername(), mJenkinsUserEntity.getToken()); setHeader("Authorization", credential); setRequestBody(formEncodingBuilder.build()); // Send to gcm/regsiter super.url = String.format("%sgcm/register", mJenkinsUserEntity.getUrl()); setMethod(HttpMethod.POST); execute(); final int statusCode = getResponse().code(); GcmRegistrationStatus status = new GcmRegistrationStatus(); status.setStatusCode(statusCode); if (statusCode != 200) { log("Sending registering token failed with satus code %s", statusCode); status.setStatus(false); } else { status.setStatus(true); } return status; }
@Override public List<String> readArtifactVersions(Service service, Module module, boolean includeSnapshots) { List<String> versions = new LinkedList<>(); if (service.getMavenGroupId() != null && !service.getMavenGroupId().isEmpty() && module.getMavenArtifactId() != null && !module.getMavenArtifactId().isEmpty()) { try { Request.Builder builder = new Request.Builder(); String mavenRepo = parameters.getString(Const.MAVEN_URL, Const.MAVEN_URL_DEFAULT); String baseUrl = mavenRepo + service.getMavenGroupId().replace(".", "/") + "/" + module.getMavenArtifactId() + "/"; String url = baseUrl + "maven-metadata.xml"; builder.url(url); String mavenUsername = parameters.getString(Const.MAVEN_USERNAME, Const.MAVEN_USERNAME_DEFAULT); String mavenPassword = parameters.getString(Const.MAVEN_PASSWORD, Const.MAVEN_PASSWORD_DEFAULT); if (!mavenUsername.equals(Const.MAVEN_USERNAME_DEFAULT)) { String credential = Credentials.basic(mavenUsername, mavenPassword); builder.header("Authorization", credential); } Request request = builder.build(); Response response = client.newCall(request).execute(); try (InputStream inputStream = response.body().byteStream()) { versions = processMavenStream(inputStream, baseUrl, includeSnapshots); } } catch (Exception ex) { LOGGER.error("Error reading maven version from {} {}, {}", service.getMavenGroupId(), module.getMavenArtifactId(), ex.getMessage()); } } return versions; }
private String determineSnapshotVersion(String baseUrl, String snapshot) { try { Request.Builder builder = new Request.Builder(); String url = baseUrl + snapshot + "/maven-metadata.xml"; builder.url(url); String mavenUsername = parameters.getString(Const.MAVEN_USERNAME, Const.MAVEN_USERNAME_DEFAULT); String mavenPassword = parameters.getString(Const.MAVEN_PASSWORD, Const.MAVEN_PASSWORD_DEFAULT); if (!mavenUsername.equals(Const.MAVEN_USERNAME_DEFAULT)) { String credential = Credentials.basic(mavenUsername, mavenPassword); builder.header("Authorization", credential); } Request request = builder.build(); Response response = client.newCall(request).execute(); try (InputStream inputStream = response.body().byteStream()) { return snapshot + "-" + processMavenSnapshotStream(inputStream); } } catch (Exception ex) { LOGGER.error("Error reading maven snapshot version data from {} {}, {}", baseUrl, snapshot, ex.getMessage()); } return null; }
BasicAuthenticator(){ credentials = Credentials.basic(user, password); }
public static String getHardcodedApiCredentials() throws ConfigJsonIOException { return Credentials.basic(getHardcodedApiUser(), getHardcodedApiPass()); }
public static String getApiCredentialsFromLogin() { return Credentials.basic(Session.getCredentials().getUsername(), Session.getCredentials().getPassword()); }
public void authorization(String username, String password) { this.authorization = Credentials.basic(username, password); }
public static BasicAuthentication basic(final String username, final String password) { return new BasicAuthentication(Credentials.basic( checkNotNull(username, "username cannot be null"), checkNotNull(password, "password cannot be null"))); }
public SmokeTestData ExecuteSmokeTest(String smokeTestUrl, String endPoint, String serviceName, String reason) { if (smokeTestUrl == null || smokeTestUrl.isEmpty() || endPoint == null || endPoint.isEmpty()) { return null; } LOGGER.info("Smoke testing EP {} with {}", endPoint, smokeTestUrl); String url = Const.HTTP + smokeTestUrl.replace(Const.END_POINT, endPoint); Timer timer = metricRegistry.timer( "smokeTest.duration", "serviceName", serviceName, "reason", reason); try { Request.Builder builder = new Request.Builder().url(url); if (parameters.getUsername() != null && parameters.getUsername().isEmpty() && parameters.getPassword() != null && parameters.getPassword().isEmpty()) { builder.addHeader( "Authorization", Credentials.basic(parameters.getUsername(), parameters.getPassword())); } Request request = builder.build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { timer.addTag("result", "success"); try (InputStream stream = response.body().byteStream()) { Reader reader = new InputStreamReader(stream); return gson.fromJson(reader, SmokeTestData.class); } } else { timer.addTag("result", "fail"); LOGGER.warn("Call to {} failed with code {}", url, response.code()); return null; } } catch (Exception ex) { timer.addTag("result", "fail"); LOGGER.warn("Call to {} failed with exception {}", url, ex.getMessage()); return null; } finally { timer.stop(); } }
public void triggerBuild(Team team, Service service, String branch, User user) { if (team == null || service == null || branch == null || branch.isEmpty()) { return; } String url = parameters.getString(Const.SERVICE_BUILD_URL, null); if (url == null || url.isEmpty()) { return; } String username = parameters.getString(Const.SERVICE_BUILD_USERNAME, null); String password = parameters.getString(Const.SERVICE_BUILD_PASSWORD, null); ServiceBuildData serviceBuildData = new ServiceBuildData(); serviceBuildData.group = team.getGitGroup(); serviceBuildData.project = service.getGitProject(); serviceBuildData.branch = branch; Request.Builder builder = new Request.Builder() .url(url); if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { builder = builder.addHeader("Authorization", Credentials.basic(username, password)); } RequestBody body = RequestBody.create(Const.JSON_MEDIA_TYPE, gson.toJson(serviceBuildData)); Request httpRequest = builder.post(body).build(); try { Response resp = client.newCall(httpRequest).execute(); if (resp.isSuccessful()) { LOGGER.warn("Build triggered to {} with code {}", url, resp.code()); Map<String, String> notes = new HashMap<>(); notes.put("Reason", "Manually requested build"); notes.put("Source Branch",branch); Audit audit = new Audit(); audit.serviceId = service.getServiceId(); audit.setTimePerformed(GMT.getGmtAsDate()); audit.timeRequested = GMT.getGmtAsDate(); audit.requestor = user.getUsername(); audit.type = Type.service; audit.operation = Operation.build; audit.successfull = true; audit.notes = gson.toJson(notes); dataAccess.saveAudit(audit, null); } else { LOGGER.warn("Build not triggered {}, code {}", url, resp.code()); } } catch (Exception ex) { LOGGER.warn("Error while triggering build {}, error {}", url, ex.getMessage()); } }
private String encodeCredentialsForBasicAuthorization() { return Credentials.basic(user.getUsername(), user.getPassword()); }