Search in sources :

Example 16 with Challenge

use of okhttp3.Challenge in project MiMangaNu by raulhaag.

the class CFInterceptor method resolveOverCF.

public Response resolveOverCF(Chain chain, Response response) throws IOException {
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Request request = response.request();
    String content = response.body().string();
    String domain = request.url().host().trim();
    String rawOperation = getFirstMatch(OPERATION_PATTERN, content);
    String challenge = getFirstMatch(CHALLENGE_PATTERN, content);
    String challengePass = getFirstMatch(PASS_PATTERN, content);
    if (rawOperation == null || challengePass == null || challenge == null) {
        Log.e("CFI", "couldn't resolve over cloudflare");
        // returning null here is not a good idea since it could stop a download ~xtj-9182
        return response;
    }
    String operation = rawOperation.replaceAll("a\\.value =(.+?) \\+ .+?;.*", "$1").replaceAll("\\s{3,}[a-z](?: = |\\.).+", "");
    String js = operation.replace("\n", "");
    Duktape duktape = Duktape.create();
    long result = 0;
    try {
        String res = (String) duktape.evaluate(js + ".toString()");
        result = Long.parseLong(res);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        duktape.close();
    }
    String answer = String.valueOf(result + domain.length());
    String url = new HttpUrl.Builder().scheme("http").host(domain).addPathSegment("cdn-cgi").addPathSegment("l").addPathSegment("chk_jschl").addEncodedQueryParameter("jschl_vc", challenge).addEncodedQueryParameter("pass", challengePass).addEncodedQueryParameter("jschl_answer", answer).build().toString();
    Request request1 = new Request.Builder().url(url).header("User-Agent", Navigator.USER_AGENT).header("Referer", request.url().toString()).build();
    response.body().close();
    // generate the cookie
    response = chain.proceed(request1);
    response.body().close();
    try {
        // give it a time and complete the 5 seconds
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    response = chain.proceed(request.newBuilder().build());
    return response;
}
Also used : Duktape(com.squareup.duktape.Duktape) Request(okhttp3.Request) IOException(java.io.IOException)

Example 17 with Challenge

use of okhttp3.Challenge in project okhttp-digest by rburgst.

the class WrongOrderedAuthenticationHeaderTest method testWithCorrectOrder.

/**
 * See: https://github.com/square/okhttp/issues/2780
 */
@Test
public void testWithCorrectOrder() {
    // Strict RFC 2617 header
    Headers headers = new Headers.Builder().add("WWW-Authenticate", "Digest realm=\"myrealm\", nonce=\"fjalskdflwejrlaskdfjlaskdjflaksjdflkasdf\", qop=\"auth\", stale=\"FALSE\"").build();
    List<Challenge> challenges = HttpHeaders.parseChallenges(headers, "WWW-Authenticate");
    assertEquals(1, challenges.size());
    assertEquals("Digest", challenges.get(0).scheme());
    assertEquals("myrealm", challenges.get(0).realm());
}
Also used : HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) Challenge(okhttp3.Challenge) Test(org.junit.Test)

Example 18 with Challenge

use of okhttp3.Challenge in project okhttp-digest by rburgst.

the class WrongOrderedAuthenticationHeaderTest method testWithWrongOrder.

@Test
public void testWithWrongOrder() {
    // Not strict RFC 2617 header.
    Headers headers = new Headers.Builder().add("WWW-Authenticate", "Digest qop=\"auth\", realm=\"myrealm\", nonce=\"fjalskdflwejrlaskdfjlaskdjflaksjdflkasdf\", stale=\"FALSE\"").build();
    List<Challenge> challenges = HttpHeaders.parseChallenges(headers, "WWW-Authenticate");
    assertEquals(1, challenges.size());
    assertEquals(1, challenges.size());
    assertEquals("Digest", challenges.get(0).scheme());
    assertEquals("myrealm", challenges.get(0).realm());
}
Also used : HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) Challenge(okhttp3.Challenge) Test(org.junit.Test)

Example 19 with Challenge

use of okhttp3.Challenge in project okhttp-digest by rburgst.

the class DispatchingAuthenticator method authenticate.

@Override
public Request authenticate(Route route, Response response) throws IOException {
    List<Challenge> challenges = response.challenges();
    if (!challenges.isEmpty()) {
        for (Challenge challenge : challenges) {
            final String scheme = challenge.scheme();
            Authenticator authenticator = null;
            if (scheme != null) {
                authenticator = authenticatorRegistry.get(scheme.toLowerCase(Locale.getDefault()));
            }
            if (authenticator != null) {
                return authenticator.authenticate(route, response);
            }
        }
    }
    throw new IllegalArgumentException("unsupported auth scheme " + challenges);
}
Also used : Authenticator(okhttp3.Authenticator) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) Challenge(okhttp3.Challenge)

Example 20 with Challenge

use of okhttp3.Challenge in project legendarybot by greatman.

the class AffixPlugin method getWeekAffixes.

public Map<Long, String> getWeekAffixes(String region) throws IOException, ParseException {
    Map<Long, String> affixes = new HashMap<>();
    OkHttpClient clientBattleNet = new OkHttpClient.Builder().addInterceptor(new BattleNetAPIInterceptor(getBot())).build();
    HttpUrl url = new HttpUrl.Builder().scheme("https").host(region + ".api.battle.net").addPathSegments("/data/wow/mythic-challenge-mode/").addQueryParameter("namespace", "dynamic-" + region).build();
    Request request = new Request.Builder().url(url).build();
    String result = clientBattleNet.newCall(request).execute().body().string();
    JSONParser parser = new JSONParser();
    JSONObject mythicPlusDocument = (JSONObject) parser.parse(result);
    if (mythicPlusDocument.containsKey("current_keystone_affixes")) {
        JSONArray array = (JSONArray) mythicPlusDocument.get("current_keystone_affixes");
        for (Object keystoneAffixObject : array) {
            JSONObject keystoneAffixJson = (JSONObject) keystoneAffixObject;
            JSONObject keystoneAffixNameObject = (JSONObject) keystoneAffixJson.get("keystone_affix");
            affixes.put((long) keystoneAffixJson.get("starting_level"), (String) keystoneAffixNameObject.get("name"));
        }
    }
    return affixes;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) Request(okhttp3.Request) JSONArray(org.json.simple.JSONArray) HttpUrl(okhttp3.HttpUrl) JSONObject(org.json.simple.JSONObject) BattleNetAPIInterceptor(com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)7 Test (org.junit.Test)7 Request (okhttp3.Request)6 Challenge (okhttp3.Challenge)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Authenticator (okhttp3.Authenticator)4 Response (okhttp3.Response)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 Map (java.util.Map)3 HttpUrl (okhttp3.HttpUrl)3 Route (okhttp3.Route)3 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)3 HttpHeaders (okhttp3.internal.http.HttpHeaders)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Headers (okhttp3.Headers)2