Commit 7472f0d0 authored by Sajal Narang's avatar Sajal Narang

Merge branch 'pulsejet-issue71'

parents 52c092e8 98bf62b4
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
a {
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.logodiv {
width: 45%;
margin: 0 auto;
margin-top: 25%;
text-align: center;
}
.logo {
width: 100%;
}
.app-name {
margin-top: 10px;
font-weight: 200;
font-size: 1.5em;
}
.lowerdiv {
position: fixed;
bottom: 8%;
left: 0;
width: 100%;
}
.logindiv {
margin: 0 auto;
width: 100%;
text-align: center;
}
a {color: black;}
a:link {color: black;} /* unvisited link */
a:visited {color: black;} /* visited link */
a:hover {color: black;} /* mouse over link */
a:active {color: black;} /* selected link */
.login {
font-size: 1.05em;
padding: 10px 28%;
border-radius: 4px;
background-color: rgb(255, 215, 64);
text-decoration: none;
}
.guest {
text-decoration: none;
font-weight: 200;
line-height: 1.7em;
font-size: 1.05em;
}
</style>
</head>
<body>
<div class="logodiv">
<img src="lotus.png" class="logo">
<div class="app-name">InstiApp</div>
</div>
<div class="lowerdiv">
<div class="logindiv">
<a class="login" href="https://gymkhana.iitb.ac.in/sso/account/login/?next=/sso/oauth/authorize/%3Fclient_id%3DvR1pU7wXWyve1rUkg0fMS6StL1Kr6paoSmRIiLXJ%26response_type%3Dcode%26scope%3Dbasic%2520profile%2520picture%2520sex%2520ldap%2520phone%2520insti_address%2520program%2520secondary_emails%26redirect_uri=https://redirecturi">
LOG IN VIA SSO
</a>
<br><br>
or
<br>
<a class="guest" href="https://guesturi">
Continue as a Guest
</a>
</div>
</div>
</body>
</html>
\ No newline at end of file
...@@ -10,6 +10,7 @@ import android.content.Intent; ...@@ -10,6 +10,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.net.http.SslError;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
...@@ -19,6 +20,11 @@ import android.support.v4.content.LocalBroadcastManager; ...@@ -19,6 +20,11 @@ import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
...@@ -43,16 +49,11 @@ import retrofit2.Response; ...@@ -43,16 +49,11 @@ import retrofit2.Response;
public class LoginActivity extends AppCompatActivity { public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity"; private static final String TAG = "LoginActivity";
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static final String CLIENT_ID = "vR1pU7wXWyve1rUkg0fMS6StL1Kr6paoSmRIiLXJ"; private final String redirectUri = "https://redirecturi";
private final Uri redirectUri = Uri.parse("https://redirecturi"); private final String guestUri = "https://guesturi";
private final Uri mAuthEndpoint = Uri.parse("http://gymkhana.iitb.ac.in/sso/oauth/authorize/");
private final Uri mTokenEndpoint = Uri.parse("http://gymkhana.iitb.ac.in/sso/oauth/token/");
public String authCode = null; public String authCode = null;
SessionManager session; SessionManager session;
Context mContext = this; Context mContext = this;
private AuthorizationService mAuthService;
private BroadcastReceiver mRegistrationBroadcastReceiver;
private boolean isReceiverRegistered;
private ProgressDialog progressDialog; private ProgressDialog progressDialog;
@Override @Override
...@@ -60,169 +61,70 @@ public class LoginActivity extends AppCompatActivity { ...@@ -60,169 +61,70 @@ public class LoginActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
session = new SessionManager(mContext); session = new SessionManager(mContext);
setContentView(R.layout.activity_login); setContentView(R.layout.activity_login);
mAuthService = new AuthorizationService(this); progressDialog = new ProgressDialog(LoginActivity.this);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences.getBoolean(Constants.SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
String token = intent.getStringExtra("Token");
Log.d(TAG, "Going to login with :" + authCode + "\n" + token);
//************
//TODO Remove following 6 lines after the server is hosted
String gcmRegId = token;
// session.createLoginSession(gcmRegId);
// Intent i = new Intent(mContext, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(i);
//**************
login(authCode, redirectUri.toString(), gcmRegId);
} else {
}
}
};
registerReceiver();
Button ldapLogin = (Button) findViewById(R.id.ldap_login);
ldapLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Initiating auth");
AuthorizationServiceConfiguration config = new AuthorizationServiceConfiguration(mAuthEndpoint, mTokenEndpoint);
makeAuthRequest(config);
}
});
Button guestLogin = (Button) findViewById(R.id.guest_login);
guestLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//TODO This is for debug purposes, change once SSO is implemented
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
} }
private class WvClient extends WebViewClient
{
@Override @Override
protected void onNewIntent(Intent intent) { public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
checkIntent(intent); handler.proceed();
}
private void checkIntent(@Nullable Intent intent) {
if (intent != null) {
Log.d(TAG, "Intent Received");
String action = intent.getAction();
if (action != null) {
switch (action) {
case "HANDLE_AUTHORIZATION_RESPONSE": {
handleAuthorizationResponse(intent);
}
break;
default:
Log.d(TAG, intent.getAction());
}
}
}
}
@Override
protected void onStart() {
super.onStart();
checkIntent(getIntent());
} }
@Override @Override
protected void onResume() { public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
super.onResume(); /* Capture redirect */
registerReceiver(); if (url.startsWith(redirectUri)) {
Log.d(TAG, "On Resume"); /* Show progress dialog */
progressDialog.setMessage("Logging In");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
if (!progressDialog.isShowing()) {
progressDialog.show();
} }
@Override /* Get auth code from query */
protected void onPause() { String query = Uri.parse(url).getQuery();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); authCode = query.substring(query.lastIndexOf("=") + 1);
isReceiverRegistered = false; login(authCode, redirectUri, authCode);
super.onPause(); return true;
} }
@Override /* Guest Login */
protected void onDestroy() { if (url.startsWith(guestUri)) {
super.onDestroy(); Intent intent = new Intent(LoginActivity.this, MainActivity.class);
mAuthService.dispose(); startActivity(intent);
return true;
} }
private void handleAuthorizationResponse(@NonNull Intent intent) { if (!progressDialog.isShowing()) {
progressDialog = new ProgressDialog(this); progressDialog.setMessage("Loading");
progressDialog.setMessage("Logging In");
progressDialog.setCancelable(false); progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true); progressDialog.setIndeterminate(true);
progressDialog.show(); progressDialog.show();
AuthorizationResponse response = AuthorizationResponse.fromIntent(intent); }
AuthorizationException error = AuthorizationException.fromIntent(intent); /* Load URL */
view.loadUrl(url);
if (response != null) { return false;
authCode = response.authorizationCode;
Log.d(TAG, "Received AuthorizationResponse: " + "AuthCode: " + authCode);
if (checkPlayServices()) {
Intent registerIntent = new Intent(this, RegistrationIntentService.class);
startService(registerIntent);
} }
// @Override
public void onPageFinished(WebView view, String url) {
} else { if (progressDialog.isShowing()) {
Log.i(TAG, "Authorization failed: " + error.getMessage()); progressDialog.dismiss();
Toast.makeText(this,
"Authorization failed", Toast.LENGTH_LONG)
.show();
} }
} }
private void makeAuthRequest(@NonNull AuthorizationServiceConfiguration serviceConfig) {
AuthorizationRequest authRequest = new AuthorizationRequest.Builder(
serviceConfig,
CLIENT_ID,
"code",
redirectUri)
.setScope("basic profile picture sex ldap phone insti_address program secondary_emails")
.build();
Log.d(TAG, "Making auth request");
String action = "HANDLE_AUTHORIZATION_RESPONSE";
Intent postAuthorizationIntent = new Intent(action);
PendingIntent pendingIntent = PendingIntent.getActivity(this, authRequest.hashCode(), postAuthorizationIntent, 0);
mAuthService.performAuthorizationRequest(
authRequest,
pendingIntent,
mAuthService.createCustomTabsIntentBuilder()
.setToolbarColor(getCustomTabColor())
.build());
} }
//TODO: Change the color of Chrome custom tabs based on app theme color @Override
@TargetApi(Build.VERSION_CODES.M) protected void onStart() {
@SuppressWarnings("deprecation") super.onStart();
private int getCustomTabColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { WebView webview = (WebView) findViewById(R.id.login_webview);
return getColor(R.color.colorPrimaryDark); webview.getSettings().setJavaScriptEnabled(true);
} else { webview.getSettings().setDomStorageEnabled(true);
return getResources().getColor(R.color.colorPrimaryDark); webview.setWebViewClient(new WvClient());
} webview.loadUrl("file:///android_asset/login.html");
} }
private void login(String authorizationCode, final String redirectURI, String gcmID) { private void login(String authorizationCode, final String redirectURI, String gcmID) {
...@@ -251,15 +153,6 @@ public class LoginActivity extends AppCompatActivity { ...@@ -251,15 +153,6 @@ public class LoginActivity extends AppCompatActivity {
}); });
} }
private void registerReceiver() {
if (!isReceiverRegistered) {
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Constants.REGISTRATION_COMPLETE));
isReceiverRegistered = true;
}
}
private boolean checkPlayServices() { private boolean checkPlayServices() {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
......
...@@ -4,20 +4,17 @@ ...@@ -4,20 +4,17 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<WebView
<Button android:id="@+id/login_webview"
android:id="@+id/guest_login" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_height="48dp" android:layout_alignParentLeft="true"
android:layout_gravity="center" android:layout_alignParentStart="true"
android:text="Guest Login" /> android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
<Button android:layout_alignParentTop="false"
android:id="@+id/ldap_login" android:layout_alignWithParentIfMissing="false">
android:layout_width="wrap_content" </WebView>
android:layout_height="48dp"
android:layout_gravity="center"
android:text="LDAP login" />
</LinearLayout> </LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment