Search in sources :

Example 1 with OAuthHelper

use of net.dean.jraw.http.oauth.OAuthHelper in project Slide by ccrama.

the class Login method onCreate.

@Override
public void onCreate(Bundle savedInstance) {
    overrideSwipeFromAnywhere();
    super.onCreate(savedInstance);
    applyColorTheme("");
    try {
        setContentView(R.layout.activity_login);
    } catch (Exception e) {
        finish();
        return;
    }
    setupAppBar(R.id.toolbar, R.string.title_login, true, true);
    String[] scopes = { "identity", "modcontributors", "modconfig", "modothers", "modwiki", "creddits", "livemanage", "account", "privatemessages", "modflair", "modlog", "report", "modposts", "modwiki", "read", "vote", "edit", "submit", "subscribe", "save", "wikiread", "flair", "history", "mysubreddits" };
    if (Authentication.reddit == null) {
        new Authentication(getApplicationContext());
    }
    final OAuthHelper oAuthHelper = Authentication.reddit.getOAuthHelper();
    final Credentials credentials = Credentials.installedApp(CLIENT_ID, REDIRECT_URL);
    String authorizationUrl = oAuthHelper.getAuthorizationUrl(credentials, true, scopes).toExternalForm();
    authorizationUrl = authorizationUrl.replace("www.", "i.");
    authorizationUrl = authorizationUrl.replace("%3A%2F%2Fi", "://www");
    Log.v(LogUtil.getTag(), "Auth URL: " + authorizationUrl);
    final WebView webView = (WebView) findViewById(R.id.web);
    webView.clearCache(true);
    webView.clearHistory();
    WebSettings webSettings = webView.getSettings();
    webSettings.setSaveFormData(false);
    // Not needed for API level 18 or greater (deprecated)
    webSettings.setSavePassword(false);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(this);
        cookieSyncMngr.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (url.contains("code=")) {
                Log.v(LogUtil.getTag(), "WebView URL: " + url);
                // Authentication code received, prevent HTTP call from being made.
                webView.stopLoading();
                new UserChallengeTask(oAuthHelper, credentials).execute(url);
                webView.setVisibility(View.GONE);
            }
        }
    });
    webView.loadUrl(authorizationUrl);
}
Also used : OAuthHelper(net.dean.jraw.http.oauth.OAuthHelper) OAuthException(net.dean.jraw.http.oauth.OAuthException) NetworkException(net.dean.jraw.http.NetworkException) Bitmap(android.graphics.Bitmap) Authentication(me.ccrama.redditslide.Authentication) WebSettings(android.webkit.WebSettings) CookieSyncManager(android.webkit.CookieSyncManager) WebView(android.webkit.WebView) Credentials(net.dean.jraw.http.oauth.Credentials) CookieManager(android.webkit.CookieManager) WebViewClient(android.webkit.WebViewClient)

Example 2 with OAuthHelper

use of net.dean.jraw.http.oauth.OAuthHelper in project Slide by ccrama.

the class Reauthenticate method onCreate.

@Override
public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    applyColorTheme("");
    setContentView(R.layout.activity_login);
    setupAppBar(R.id.toolbar, "Re-authenticate", true, true);
    String[] scopes = { "identity", "modcontributors", "modconfig", "modothers", "modwiki", "creddits", "livemanage", "account", "privatemessages", "modflair", "modlog", "report", "modposts", "modwiki", "read", "vote", "edit", "submit", "subscribe", "save", "wikiread", "flair", "history", "mysubreddits" };
    final OAuthHelper oAuthHelper = Authentication.reddit.getOAuthHelper();
    final Credentials credentials = Credentials.installedApp(CLIENT_ID, REDIRECT_URL);
    String authorizationUrl = oAuthHelper.getAuthorizationUrl(credentials, true, scopes).toExternalForm();
    authorizationUrl = authorizationUrl.replace("www.", "i.");
    authorizationUrl = authorizationUrl.replace("%3A%2F%2Fi", "://www");
    Log.v(LogUtil.getTag(), "Auth URL: " + authorizationUrl);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    final WebView webView = (WebView) findViewById(R.id.web);
    webView.loadUrl(authorizationUrl);
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
        // activity.setProgress(newProgress * 1000);
        }
    });
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (url.contains("code=")) {
                Log.v(LogUtil.getTag(), "WebView URL: " + url);
                // Authentication code received, prevent HTTP call from being made.
                webView.stopLoading();
                new UserChallengeTask(oAuthHelper, credentials).execute(url);
                webView.setVisibility(View.GONE);
                webView.clearCache(true);
                webView.clearHistory();
            }
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) WebChromeClient(android.webkit.WebChromeClient) OAuthHelper(net.dean.jraw.http.oauth.OAuthHelper) WebView(android.webkit.WebView) Credentials(net.dean.jraw.http.oauth.Credentials) CookieManager(android.webkit.CookieManager) WebViewClient(android.webkit.WebViewClient)

Example 3 with OAuthHelper

use of net.dean.jraw.http.oauth.OAuthHelper in project Slide by ccrama.

the class Authentication method doVerify.

public static void doVerify(String lastToken, RedditClient baseReddit, boolean single, Context mContext) {
    try {
        String token = lastToken;
        if (BuildConfig.DEBUG)
            LogUtil.v("TOKEN IS " + token);
        if (!token.isEmpty()) {
            Credentials credentials = Credentials.installedApp(CLIENT_ID, REDIRECT_URL);
            OAuthHelper oAuthHelper = baseReddit.getOAuthHelper();
            oAuthHelper.setRefreshToken(token);
            try {
                OAuthData finalData;
                if (!single && authentication.contains("backedCreds") && authentication.getLong("expires", 0) > Calendar.getInstance().getTimeInMillis()) {
                    finalData = oAuthHelper.refreshToken(credentials, authentication.getString("backedCreds", ""));
                } else {
                    // does a request
                    finalData = oAuthHelper.refreshToken(credentials);
                    if (!single) {
                        authentication.edit().putLong("expires", Calendar.getInstance().getTimeInMillis() + 3000000).apply();
                    }
                }
                baseReddit.authenticate(finalData);
                if (!single) {
                    authentication.edit().putString("backedCreds", finalData.getDataNode().toString()).apply();
                    refresh = oAuthHelper.getRefreshToken();
                    if (BuildConfig.DEBUG) {
                        LogUtil.v("ACCESS TOKEN IS " + finalData.getAccessToken());
                    }
                    Authentication.isLoggedIn = true;
                    UserSubscriptions.doCachedModSubs();
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (e instanceof NetworkException) {
                    Toast.makeText(mContext, "Error " + ((NetworkException) e).getResponse().getStatusMessage() + ": " + (e).getMessage(), Toast.LENGTH_LONG).show();
                }
            }
            didOnline = true;
        } else if (!single) {
            if (BuildConfig.DEBUG)
                LogUtil.v("NOT LOGGED IN");
            final Credentials fcreds = Credentials.userlessApp(CLIENT_ID, UUID.randomUUID());
            OAuthData authData;
            try {
                authData = reddit.getOAuthHelper().easyAuth(fcreds);
                authentication.edit().putLong("expires", Calendar.getInstance().getTimeInMillis() + 3000000).apply();
                authentication.edit().putString("backedCreds", authData.getDataNode().toString()).apply();
                reddit.authenticate(authData);
                Authentication.name = "LOGGEDOUT";
                Reddit.notFirst = true;
                didOnline = true;
            } catch (Exception e) {
                e.printStackTrace();
                if (e instanceof NetworkException) {
                    Toast.makeText(mContext, "Error " + ((NetworkException) e).getResponse().getStatusMessage() + ": " + (e).getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
        if (!single)
            authedOnce = true;
    } catch (Exception e) {
    // TODO fail
    }
}
Also used : OAuthHelper(net.dean.jraw.http.oauth.OAuthHelper) NetworkException(net.dean.jraw.http.NetworkException) Credentials(net.dean.jraw.http.oauth.Credentials) OAuthData(net.dean.jraw.http.oauth.OAuthData) NetworkException(net.dean.jraw.http.NetworkException)

Aggregations

Credentials (net.dean.jraw.http.oauth.Credentials)3 OAuthHelper (net.dean.jraw.http.oauth.OAuthHelper)3 Bitmap (android.graphics.Bitmap)2 CookieManager (android.webkit.CookieManager)2 WebView (android.webkit.WebView)2 WebViewClient (android.webkit.WebViewClient)2 NetworkException (net.dean.jraw.http.NetworkException)2 CookieSyncManager (android.webkit.CookieSyncManager)1 WebChromeClient (android.webkit.WebChromeClient)1 WebSettings (android.webkit.WebSettings)1 Authentication (me.ccrama.redditslide.Authentication)1 OAuthData (net.dean.jraw.http.oauth.OAuthData)1 OAuthException (net.dean.jraw.http.oauth.OAuthException)1