Commit fc841ea4 authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge pull request #9 from yvsriram/master, close #1

Add LoginActivity and retrieve AuthCode
parents 4e61f1f8 442278dd
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<component name="CopyrightManager">
<settings default="" />
</component>
\ No newline at end of file
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="JDK" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/IITB-App.iml" filepath="$PROJECT_DIR$/IITB-App.iml" /> <module fileurl="file://$PROJECT_DIR$/IITB-App.iml" filepath="$PROJECT_DIR$/IITB-App.iml" />
<module fileurl="file://$PROJECT_DIR$/IITBApp.iml" filepath="$PROJECT_DIR$/IITBApp.iml" />
<module fileurl="file://D:\IITB-App\IITBApp.iml" filepath="D:\IITB-App\IITBApp.iml" /> <module fileurl="file://D:\IITB-App\IITBApp.iml" filepath="D:\IITB-App\IITBApp.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules> </modules>
......
...@@ -5,6 +5,7 @@ android { ...@@ -5,6 +5,7 @@ android {
buildToolsVersion "25.0.3" buildToolsVersion "25.0.3"
defaultConfig { defaultConfig {
applicationId "in.ac.iitb.gymkhana.iitbapp" applicationId "in.ac.iitb.gymkhana.iitbapp"
manifestPlaceholders 'appAuthRedirectScheme': 'https'
minSdkVersion 17 minSdkVersion 17
targetSdkVersion 25 targetSdkVersion 25
versionCode 1 versionCode 1
...@@ -25,6 +26,7 @@ dependencies { ...@@ -25,6 +26,7 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'
}) })
compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:appcompat-v7:25.3.1'
compile 'net.openid:appauth:0.2.0'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
compile 'com.android.support:design:25.3.1' compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support.constraint:constraint-layout:1.0.2'
......
...@@ -13,6 +13,31 @@ ...@@ -13,6 +13,31 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="HANDLE_AUTHORIZATION_RESPONSE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="redirecturi"
android:scheme="https" />
</intent-filter>
</activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package in.ac.iitb.gymkhana.iitbapp;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationResponse;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.TokenResponse;
import static android.R.attr.action;
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private final String clientId = "pFcDDWtUUfzlAX2ibriV25lm1J2m92O5ynfT4SYk";
private final String clientSecret = "k56GXiN1qB4Dt7CnTVWjuwLJyWntNulitWOkL7Wddr6JHPiHqIZgSfgUplO6neTqumVr32zA14XgQmkuoC8y6y9jnaQT9tKDsq4jQklRb8MQNQglQ1H4YrmqOwPfaNyO";
private final Uri redirectUri = Uri.parse("https://redirecturi");
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/");
private AuthorizationService mAuthService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuthService = new AuthorizationService(this);
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) {
}
});
}
@Override
protected void onNewIntent(Intent intent) {
checkIntent(intent);
}
private void checkIntent(@Nullable Intent intent) {
if (intent != null) {
Log.d(TAG,"Intent Received");
String action = intent.getAction();
switch (action) {
case "HANDLE_AUTHORIZATION_RESPONSE":
{
handleAuthorizationResponse(intent);
}
break;
default:Log.d(TAG,intent.getAction());
}
}
}
@Override
protected void onStart() {
super.onStart();
checkIntent(getIntent());
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"In Resume");
}
@Override
protected void onDestroy() {
super.onDestroy();
mAuthService.dispose();
}
private void handleAuthorizationResponse(@NonNull Intent intent) {
AuthorizationResponse response = AuthorizationResponse.fromIntent(intent);
AuthorizationException error = AuthorizationException.fromIntent(intent);
if (response != null) {
Log.d(TAG, "Received AuthorizationResponse: "+ "AuthCode: "+response.authorizationCode);
Toast.makeText(this,
"AuthCode: "+response.authorizationCode, Toast.LENGTH_SHORT)
.show();
} else {
Log.i(TAG, "Authorization failed: " + error.getMessage());
Toast.makeText(this,
"Authorization failed", Toast.LENGTH_LONG)
.show();
}
}
private void makeAuthRequest(
@NonNull AuthorizationServiceConfiguration serviceConfig) {
AuthorizationRequest authRequest = new AuthorizationRequest.Builder(
serviceConfig,
clientId,
"code",
redirectUri)
.setScope("profile")
.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
@TargetApi(Build.VERSION_CODES.M)
@SuppressWarnings("deprecation")
private int getCustomTabColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getColor(R.color.colorPrimaryDark);
} else {
return getResources().getColor(R.color.colorPrimaryDark);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/guest_login"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:text="Guest Login"
/>
<Button
android:id="@+id/ldap_login"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:text="LDAP login"
/>
</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