List of usage examples for java.net HttpCookie setDomain
public void setDomain(String pattern)
From source file:org.mariotaku.twidere.util.OAuthPasswordAuthenticator.java
public OAuthToken getOAuthAccessToken(final String username, final String password) throws AuthenticationException { final OAuthToken requestToken; try {/*from w w w . j a va 2 s .c o m*/ requestToken = oauth.getRequestToken(OAUTH_CALLBACK_OOB); } catch (final TwitterException e) { // if (e.isCausedByNetworkIssue()) throw new AuthenticationException(e); throw new AuthenticityTokenException(e); } RestHttpResponse authorizePage = null, authorizeResult = null; try { final String oauthToken = requestToken.getOauthToken(); final HashMap<String, String> inputMap = new HashMap<>(); final RestHttpRequest.Builder authorizePageBuilder = new RestHttpRequest.Builder(); authorizePageBuilder.method(GET.METHOD); authorizePageBuilder.url(endpoint.construct("/oauth/authorize", Pair.create("oauth_token", requestToken.getOauthToken()))); final RestHttpRequest authorizePageRequest = authorizePageBuilder.build(); authorizePage = client.execute(authorizePageRequest); final String[] cookieHeaders = authorizePage.getHeaders("Set-Cookie"); readInputFromHtml(BaseTypedData.reader(authorizePage.getBody()), inputMap, INPUT_AUTHENTICITY_TOKEN, INPUT_REDIRECT_AFTER_LOGIN); final List<Pair<String, String>> params = new ArrayList<>(); params.add(Pair.create("oauth_token", oauthToken)); params.add(Pair.create(INPUT_AUTHENTICITY_TOKEN, inputMap.get(INPUT_AUTHENTICITY_TOKEN))); if (inputMap.containsKey(INPUT_REDIRECT_AFTER_LOGIN)) { params.add(Pair.create(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN))); } params.add(Pair.create("session[username_or_email]", username)); params.add(Pair.create("session[password]", password)); final FormTypedBody authorizationResultBody = new FormTypedBody(params); final ArrayList<Pair<String, String>> requestHeaders = new ArrayList<>(); requestHeaders.add(Pair.create("Origin", "https://twitter.com")); requestHeaders.add(Pair.create("Referer", Endpoint.constructUrl("https://twitter.com/oauth/authorize", Pair.create("oauth_token", requestToken.getOauthToken())))); final String host = parseUrlHost(endpoint.getUrl()); for (String cookieHeader : cookieHeaders) { for (HttpCookie cookie : HttpCookie.parse(cookieHeader)) { if (HttpCookie.domainMatches(cookie.getDomain(), host)) { cookie.setVersion(1); cookie.setDomain("twitter.com"); } requestHeaders.add(Pair.create("Cookie", cookie.toString())); } } final RestHttpRequest.Builder authorizeResultBuilder = new RestHttpRequest.Builder(); authorizeResultBuilder.method(POST.METHOD); authorizeResultBuilder.url(endpoint.construct("/oauth/authorize")); authorizeResultBuilder.headers(requestHeaders); authorizeResultBuilder.body(authorizationResultBody); authorizeResult = client.execute(authorizeResultBuilder.build()); final String oauthPin = readOAuthPINFromHtml(BaseTypedData.reader(authorizeResult.getBody())); if (isEmpty(oauthPin)) throw new WrongUserPassException(); return oauth.getAccessToken(requestToken, oauthPin); } catch (final IOException | NullPointerException | XmlPullParserException | TwitterException e) { throw new AuthenticationException(e); } finally { if (authorizePage != null) { IoUtils.closeSilently(authorizePage); } if (authorizeResult != null) { IoUtils.closeSilently(authorizeResult); } } }
From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java
/** * *//* ww w. ja v a 2s. co m*/ @Override public void setCookie(TankCookie cookie) { HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue()); httpCookie.setDomain(cookie.getDomain()); httpCookie.setPath(cookie.getPath()); try { ((CookieManager) okHttpClient.getCookieHandler()).getCookieStore().add(null, httpCookie); } catch (Exception e) { LOG.error("Error setting cookie: " + e, e); } }
From source file:org.openhab.binding.amazonechocontrol.internal.Connection.java
private void exhangeToken() throws IOException, URISyntaxException { this.renewTime = 0; String cookiesJson = "{\"cookies\":{\"." + getAmazonSite() + "\":[]}}"; String cookiesBase64 = Base64.getEncoder().encodeToString(cookiesJson.getBytes()); String exchangePostData = "di.os.name=iOS&app_version=2.2.223830.0&domain=." + getAmazonSite() + "&source_token=" + URLEncoder.encode(this.refreshToken, "UTF8") + "&requested_token_type=auth_cookies&source_token_type=refresh_token&di.hw.version=iPhone&di.sdk.version=6.10.0&cookies=" + cookiesBase64 + "&app_name=Amazon%20Alexa&di.os.version=11.4.1"; HashMap<String, String> exchangeTokenHeader = new HashMap<String, String>(); exchangeTokenHeader.put("Cookie", ""); String exchangeTokenJson = makeRequestAndReturnString("POST", "https://www." + getAmazonSite() + "/ap/exchangetoken", exchangePostData, false, exchangeTokenHeader);/* w ww.java2 s . c o m*/ JsonExchangeTokenResponse exchangeTokenResponse = gson.fromJson(exchangeTokenJson, JsonExchangeTokenResponse.class); org.openhab.binding.amazonechocontrol.internal.jsons.JsonExchangeTokenResponse.Response response = exchangeTokenResponse.response; if (response != null) { org.openhab.binding.amazonechocontrol.internal.jsons.JsonExchangeTokenResponse.Tokens tokens = response.tokens; if (tokens != null) { @Nullable Map<String, Cookie[]> cookiesMap = tokens.cookies; if (cookiesMap != null) { for (String domain : cookiesMap.keySet()) { Cookie[] cookies = cookiesMap.get(domain); for (Cookie cookie : cookies) { if (cookie != null) { HttpCookie httpCookie = new HttpCookie(cookie.Name, cookie.Value); httpCookie.setPath(cookie.Path); httpCookie.setDomain(domain); Boolean secure = cookie.Secure; if (secure != null) { httpCookie.setSecure(secure); } this.cookieManager.getCookieStore().add(null, httpCookie); } } } } } } if (!verifyLogin()) { throw new ConnectionException("Verify login failed after token exchange"); } this.renewTime = (long) (System.currentTimeMillis() + Connection.expiresIn * 1000d / 0.8d); // start renew at }
From source file:org.openhab.binding.amazonechocontrol.internal.Connection.java
private @Nullable Date tryRestoreSessionData(@Nullable String data, @Nullable String overloadedDomain) { // verify store data if (StringUtils.isEmpty(data)) { return null; }// ww w . j a v a 2s . co m Scanner scanner = new Scanner(data); String version = scanner.nextLine(); // check if serialize version if (!version.equals("5") && !version.equals("6")) { scanner.close(); return null; } int intVersion = Integer.parseInt(version); frc = scanner.nextLine(); serial = scanner.nextLine(); deviceId = scanner.nextLine(); // Recreate session and cookies refreshToken = scanner.nextLine(); String domain = scanner.nextLine(); if (overloadedDomain != null) { domain = overloadedDomain; } setAmazonSite(domain); deviceName = scanner.nextLine(); if (intVersion > 5) { String accountCustomerId = scanner.nextLine(); if (!StringUtils.equals(accountCustomerId, "null")) { this.accountCustomerId = accountCustomerId; } } Date loginTime = new Date(Long.parseLong(scanner.nextLine())); CookieStore cookieStore = cookieManager.getCookieStore(); cookieStore.removeAll(); Integer numberOfCookies = Integer.parseInt(scanner.nextLine()); for (Integer i = 0; i < numberOfCookies; i++) { String name = readValue(scanner); String value = readValue(scanner); HttpCookie clientCookie = new HttpCookie(name, value); clientCookie.setComment(readValue(scanner)); clientCookie.setCommentURL(readValue(scanner)); clientCookie.setDomain(readValue(scanner)); clientCookie.setMaxAge(Long.parseLong(readValue(scanner))); clientCookie.setPath(readValue(scanner)); clientCookie.setPortlist(readValue(scanner)); clientCookie.setVersion(Integer.parseInt(readValue(scanner))); clientCookie.setSecure(Boolean.parseBoolean(readValue(scanner))); clientCookie.setDiscard(Boolean.parseBoolean(readValue(scanner))); cookieStore.add(null, clientCookie); } scanner.close(); try { checkRenewSession(); if (StringUtils.isEmpty(this.accountCustomerId)) { List<Device> devices = this.getDeviceList(); for (Device device : devices) { if (StringUtils.equals(device.serialNumber, this.serial)) { this.accountCustomerId = device.deviceOwnerCustomerId; break; } } if (StringUtils.isEmpty(this.accountCustomerId)) { for (Device device : devices) { if (StringUtils.equals(device.accountName, "This Device")) { this.accountCustomerId = device.deviceOwnerCustomerId; String serial = device.serialNumber; if (serial != null) { this.serial = serial; } break; } } } } } catch (URISyntaxException | IOException | ConnectionException e) { logger.debug("Getting account customer Id failed {}", e); } return loginTime; }