Skip to content

Commit

Permalink
Merge pull request #42 from ibm-cloud-security/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
motyd authored Jun 29, 2017
2 parents bcbff29 + 9e632dd commit c9107f6
Show file tree
Hide file tree
Showing 29 changed files with 2,544 additions and 223 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,24 @@ loginWidget.launch(this, new AuthorizationListener() {

The Login widget default configuration use Facebook and Google as authentication options.
If you configure only one of them the login widget will NOT launch and the user will be redirect to the configured idp authentication screen.


<!--
### Login using Resource Owner Password
You can obtain access token and id token by supplying the end user's username and the end user's password.
```java
AppID.getInstance().obtainTokensWithROP(getApplicationContext(), username, password,
new TokenResponseListener() {
@Override
public void onAuthorizationFailure (AuthorizationException exception) {
//Exception occurred
}
@Override
public void onAuthorizationSuccess (AccessToken accessToken, IdentityToken identityToken) {
//User authenticated
}
});
```
-->
### Anonymous Login
```java
AppID.getInstance().loginAnonymously(getApplicationContext(), new AuthorizationListener() {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignInActivity"
android:label="@string/title_activity_login"
android:theme="@android:style/Theme.Holo.Dialog"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name=".TokensActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.ibm.bluemix.appid.android.api.AuthorizationException;
import com.ibm.bluemix.appid.android.api.AuthorizationListener;
import com.ibm.bluemix.appid.android.api.LoginWidget;
import com.ibm.bluemix.appid.android.api.TokenResponseListener;
import com.ibm.bluemix.appid.android.api.tokens.AccessToken;
import com.ibm.bluemix.appid.android.api.tokens.IdentityToken;
import com.ibm.bluemix.appid.android.api.userattributes.UserAttributeResponseListener;
Expand Down Expand Up @@ -44,6 +45,9 @@ public class MainActivity extends AppCompatActivity {
private AccessToken identifiedAccessToken;
private AccessToken useThisToken;

public final static int LOGIN_SUBMITTED = 2;
public final static int LOGIN_CANCEL = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
logger.setLogLevel(Logger.LEVEL.DEBUG);
Expand Down Expand Up @@ -99,7 +103,8 @@ public void onLoginClick(View v) {
loginWidget.launch(this, new AuthorizationListener() {
@Override
public void onAuthorizationFailure(AuthorizationException exception) {
logger.info("onAuthorizationFailure");
logger.info("onAuthorizationFailure: " + exception.getMessage());
showResponse(exception.getMessage());
hideProgress();
}

Expand All @@ -119,7 +124,48 @@ public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identi
identifiedAccessToken = accessToken;
extractAndDisplayDataFromIdentityToken(identityToken);
}
});
}, anonymousAccessToken != null ? anonymousAccessToken.getRaw() : null);
}

public void onGetTokenUsingRoP(View v) {
logger.debug("onGetTokenUsingRoP");
showResponse("");
showProgress();
Intent inent;
inent = new Intent(getApplicationContext(), SignInActivity.class);
startActivityForResult(inent, LOGIN_SUBMITTED);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == LOGIN_CANCEL) {
hideProgress();
logger.info("onGetTokenUsingRoP canceled");
return;
}
if (resultCode == LOGIN_SUBMITTED && data != null) {
String username = data.getStringExtra("username");
String password = data.getStringExtra("password");
appId.obtainTokensWithROP(getApplicationContext(), username, password, new TokenResponseListener() {
@Override
public void onAuthorizationFailure(AuthorizationException exception) {
logger.info("onAuthorizationFailure: " + exception.getMessage());
showResponse(exception.getMessage());
hideProgress();
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
logger.info("onAuthorizationSuccess");
logger.info("access_token: " + accessToken.getRaw());
logger.info("id_token: " + identityToken.getRaw());
logger.info("access_token isExpired: " + accessToken.isExpired());
logger.info("id_token isExpired: " + identityToken.isExpired());
identifiedAccessToken = accessToken;
extractAndDisplayDataFromIdentityToken(identityToken);
}
});
}
}

public void onProtectedRequestClick(View v) {
Expand Down Expand Up @@ -301,6 +347,7 @@ public void run() {
findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
findViewById(R.id.loadingPanel).bringToFront();
findViewById(R.id.loginButton).setEnabled(false);
findViewById(R.id.ropButton).setEnabled(false);
findViewById(R.id.protectedRequestButton).setEnabled(false);
findViewById(R.id.deleteAttribute).setEnabled(false);
findViewById(R.id.anonloginButton).setEnabled(false);
Expand All @@ -320,6 +367,7 @@ private void hideProgress() {
public void run() {
findViewById(R.id.loadingPanel).setVisibility(View.GONE);
findViewById(R.id.loginButton).setEnabled(true);
findViewById(R.id.ropButton).setEnabled(true);
findViewById(R.id.protectedRequestButton).setEnabled(true);
findViewById(R.id.deleteAttribute).setEnabled(true);
findViewById(R.id.anonloginButton).setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.ibm.mobilefirstplatform.appid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

import static com.ibm.mobilefirstplatform.appid.MainActivity.LOGIN_CANCEL;
import static com.ibm.mobilefirstplatform.appid.MainActivity.LOGIN_SUBMITTED;

/**
* A login screen that offers login via userName/password.
*/

public class SignInActivity extends Activity {
// UI references.
private AutoCompleteTextView muserNameView;
private EditText mPasswordView;
private View mLoginFormView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
// Set up the login form.
muserNameView = (AutoCompleteTextView) findViewById(R.id.userName);
mPasswordView = (EditText) findViewById(R.id.password);
mLoginFormView = findViewById(R.id.login_form);
}

public void attemptLogin(View v) {
String userName = muserNameView.getText().toString();
String password = mPasswordView.getText().toString();
Intent inent = new Intent();
inent.putExtra("username",userName);
inent.putExtra("password", password);
setResult(LOGIN_SUBMITTED, inent);
finish();
}

public void cancelLogin(View v){
Intent inent = new Intent();
setResult(LOGIN_CANCEL, inent);
finish();
}
}
21 changes: 18 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/profilePic"
android:layout_marginTop="15dp"
android:onClick="onTokensClick"
android:text="Tokens"
Expand All @@ -54,7 +53,21 @@
android:layout_below="@+id/profilePic"
android:layout_weight="1"
android:onClick="onAnonLoginClick"
android:text="Use Anonymously" />
android:text="Use Anonymously"
android:textSize="12sp" />

<Button
android:id="@+id/ropButton"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/name"
android:layout_weight="1"
android:onClick="onGetTokenUsingRoP"
android:text="Get tokens with RoP"
android:textAllCaps="false" />

<Button
android:id="@+id/loginButton"
Expand All @@ -76,7 +89,9 @@
android:layout_below="@+id/orText"
android:layout_weight="1"
android:onClick="onProtectedRequestClick"
android:text="Protected Resource Request" />
android:text="Protected Resource Request"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>

<RelativeLayout
Expand Down
67 changes: 67 additions & 0 deletions app/src/main/res/layout/sign_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~     Copyright 2015 IBM Corp.
~     Licensed under the Apache License, Version 2.0 (the "License");
~     you may not use this file except in compliance with the License.
~     You may obtain a copy of the License at
~     http://www.apache.org/licenses/LICENSE-2.0
~     Unless required by applicable law or agreed to in writing, software
~     distributed under the License is distributed on an "AS IS" BASIS,
~     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~     See the License for the specific language governing permissions and
~     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center_horizontal"
android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<!-- Login progress -->
<ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:visibility="gone" />

<ScrollView android:id="@+id/login_form" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false">

<LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">

<AutoCompleteTextView android:id="@+id/userName" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="@string/prompt_email"
android:inputType="textEmailAddress" android:maxLines="1" />

<EditText android:id="@+id/password" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified" android:inputType="textPassword"
android:maxLines="1" android:singleLine="true" />

<Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:text="@string/action_sign_in"
android:textStyle="bold"
android:onClick="attemptLogin" />

<Button
style="?android:textAppearanceSmall"
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="cancel"
android:textStyle="bold"
android:onClick="cancelLogin"
android:layout_gravity="center_horizontal" />

</LinearLayout>
</ScrollView>

</LinearLayout>

11 changes: 11 additions & 0 deletions app/src/main/res/values/sign_in_strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="title_activity_login">Sign in</string>
<!-- Strings related to login -->
<string name="prompt_email">User Name</string>
<string name="prompt_password">Password</string>
<string name="action_sign_in">Sign in</string>
<string name="action_sign_in_short">Sign in</string>

</resources>
5 changes: 0 additions & 5 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
android:allowBackup="true"
android:supportsRtl="true">

<activity
android:name="com.ibm.bluemix.appid.android.internal.authorizationmanager.WebViewActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
</activity>

<activity android:name="com.ibm.bluemix.appid.android.internal.authorizationmanager.ChromeTabActivity">
<intent-filter>
<action android:name="${appIdRedirectScheme}.POST_AUTHORIZATION_INTENT" />
Expand Down
Loading

0 comments on commit c9107f6

Please sign in to comment.