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);
}
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();
}
}
});
}
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
}
}
Aggregations