Skip to content

Commit

Permalink
Support Refresh Token
Browse files Browse the repository at this point in the history
  • Loading branch information
motyd authored Feb 1, 2018
1 parent 95442ff commit 81fef1c
Show file tree
Hide file tree
Showing 22 changed files with 943 additions and 688 deletions.
381 changes: 190 additions & 191 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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.tokens.RefreshToken;
import com.ibm.bluemix.appid.android.api.userattributes.UserAttributeResponseListener;
import com.ibm.bluemix.appid.android.api.userattributes.UserAttributesException;
import com.ibm.mobilefirstplatform.clientsdk.android.core.api.BMSClient;
Expand All @@ -44,6 +45,7 @@ public class MainActivity extends AppCompatActivity {
private AccessToken anonymousAccessToken;
private AccessToken identifiedAccessToken;
private AccessToken useThisToken;
private RefreshToken identifiedRefreshToken;

public final static int LOGIN_SUBMITTED = 2;
public final static int LOGIN_CANCEL = 3;
Expand All @@ -67,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void onAnonLoginClick(View v) {
logger.debug("onAnonLoginClick");
showProgress();
appId.loginAnonymously(getApplicationContext(), new AuthorizationListener() {
appId.signinAnonymously(getApplicationContext(), new AuthorizationListener() {
@Override
public void onAuthorizationFailure(AuthorizationException exception) {
logger.error("Anonymous authorization failure");
Expand All @@ -84,7 +86,7 @@ public void onAuthorizationCanceled() {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
logger.info("Anonymous authorization success");
anonymousAccessToken = accessToken;
extractAndDisplayDataFromIdentityToken(identityToken);
Expand Down Expand Up @@ -115,14 +117,18 @@ public void onAuthorizationCanceled() {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
logger.info("onAuthorizationSuccess");
if (accessToken != null && identityToken != null) {
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());
if (refreshToken != null) {
logger.info("refresh_token: " + refreshToken.getRaw());
}
identifiedAccessToken = accessToken;
identifiedRefreshToken = refreshToken;
extractAndDisplayDataFromIdentityToken(identityToken);
} else {
//in case we are in strict mode
Expand Down Expand Up @@ -152,14 +158,18 @@ public void onAuthorizationCanceled() {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
logger.info("sign up: onAuthorizationSuccess");
if (accessToken != null && identityToken != null) {
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());
if (refreshToken != null) {
logger.info("refresh_token: " + refreshToken.getRaw());
}
identifiedAccessToken = accessToken;
identifiedRefreshToken = refreshToken;
extractAndDisplayDataFromIdentityToken(identityToken);
} else {
//in case we are in strict mode
Expand Down Expand Up @@ -190,7 +200,7 @@ public void onAuthorizationCanceled() {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
logger.info("Forgot Password: onAuthorizationSuccess");
hideProgress();
}
Expand All @@ -217,13 +227,17 @@ public void onAuthorizationCanceled() {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
logger.info("Change Password: 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());
if (refreshToken != null) {
logger.info("refresh_token: " + refreshToken.getRaw());
}
identifiedAccessToken = accessToken;
identifiedRefreshToken = refreshToken;
extractAndDisplayDataFromIdentityToken(identityToken);
}
});
Expand All @@ -249,13 +263,17 @@ public void onAuthorizationCanceled() {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
logger.info("Change Details: 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());
if (refreshToken != null) {
logger.info("refresh_token: " + refreshToken.getRaw());
}
identifiedAccessToken = accessToken;
identifiedRefreshToken = refreshToken;
extractAndDisplayDataFromIdentityToken(identityToken);
}
});
Expand All @@ -280,7 +298,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == LOGIN_SUBMITTED && data != null) {
String username = data.getStringExtra("username");
String password = data.getStringExtra("password");
appId.obtainTokensWithROP(getApplicationContext(), username, password, new TokenResponseListener() {
appId.signinWithResourceOwnerPassword(getApplicationContext(), username, password, new TokenResponseListener() {
@Override
public void onAuthorizationFailure(AuthorizationException exception) {
logger.info("onAuthorizationFailure: " + exception.getMessage());
Expand All @@ -289,13 +307,17 @@ public void onAuthorizationFailure(AuthorizationException exception) {
}

@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken) {
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
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());
if (refreshToken != null) {
logger.info("refresh_token: " + refreshToken.getRaw());
}
identifiedAccessToken = accessToken;
identifiedRefreshToken = refreshToken;
extractAndDisplayDataFromIdentityToken(identityToken);
}
}, anonymousAccessToken != null ? anonymousAccessToken.getRaw() : null);
Expand Down Expand Up @@ -552,6 +574,9 @@ public void onRadioButtonClicked(View view) {
useThisToken.getRaw() : "No token";
((TextView) findViewById(R.id.textViewProtectedResourceResponse)).setText(token);
break;
case R.id.radio_refresh:
((TextView) findViewById(R.id.textViewProtectedResourceResponse)).setText(identifiedRefreshToken != null ? identifiedRefreshToken.getRaw() : "No token");
break;
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@
android:onClick="onRadioButtonClicked"
android:text="@string/use_identified" />

<RadioButton
android:id="@+id/radio_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/use_refresh_token" />

</RadioGroup>

<TextView
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<string name="help">Help</string>

<string name="use_last">last token</string>
<string name="use_anon">anonymous token</string>
<string name="use_anon">anon token</string>
<string name="use_identified">id token</string>

<string name="title_activity_tokens">Tokens</string>
<string name="closebutton">Close</string>
<string name="no_tokens">NO\nTOKENS</string>
<string name="use_refresh_token">refresh token</string>
</resources>
115 changes: 97 additions & 18 deletions lib/src/main/java/com/ibm/bluemix/appid/android/api/AppID.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import android.support.annotation.NonNull;

import com.ibm.bluemix.appid.android.api.tokens.AccessToken;
import com.ibm.bluemix.appid.android.api.tokens.RefreshToken;
import com.ibm.bluemix.appid.android.api.userattributes.UserAttributeManager;
import com.ibm.bluemix.appid.android.internal.OAuthManager;
import com.ibm.bluemix.appid.android.internal.loginwidget.LoginWidgetImpl;
import com.ibm.bluemix.appid.android.internal.userattributesmanager.UserAttributeManagerImpl;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;

public class AppID {

private static AppID instance;
Expand Down Expand Up @@ -109,6 +112,17 @@ public LoginWidget getLoginWidget() {
return this.loginWidget;
}

/**
* Sets the preferred locale for UI pages
* @param locale
*/
public void setPreferredLocale(Locale locale) {
if (null == oAuthManager) {
throw new RuntimeException("AppID is not initialized. Use .initialize() first.");
}
oAuthManager.setPreferredLocale(locale);
}

/**
* @return the OAuth Manager
*/
Expand All @@ -131,16 +145,40 @@ public UserAttributeManager getUserAttributeManager(){
return this.userAttributeManager;
}

/**
* @deprecated use {@link #signinAnonymously(Context, AuthorizationListener)}
*/
@Deprecated
public void loginAnonymously(@NotNull Context context, @NotNull AuthorizationListener authorizationListener){
this.loginAnonymously(context, null, true, authorizationListener);
this.signinAnonymously(context, authorizationListener);
}

/**
* @deprecated use {@link #signinAnonymously(Context, String, AuthorizationListener)}
*/
@Deprecated
public void loginAnonymously(@NotNull Context context, String accessToken, @NotNull AuthorizationListener authorizationListener){
this.loginAnonymously(context, accessToken, true, authorizationListener);
this.signinAnonymously(context, accessToken, authorizationListener);
}

/**
* @deprecated use {@link #signinAnonymously(Context, String, boolean, AuthorizationListener)}
*/
@Deprecated
public void loginAnonymously(@NotNull Context context, String accessToken, boolean allowCreateNewAnonymousUser, @NotNull AuthorizationListener authorizationListener){
oAuthManager.getAuthorizationManager().signinAnonymously(context, accessToken, allowCreateNewAnonymousUser, authorizationListener);
}

public void signinAnonymously(@NotNull Context context, @NotNull AuthorizationListener authorizationListener){
this.signinAnonymously(context, null, true, authorizationListener);
}

public void loginAnonymously(@NotNull Context context, String accessToken, boolean allowCreateNewAnonymousUser, @NotNull AuthorizationListener authorizationListener){
oAuthManager.getAuthorizationManager().loginAnonymously(context, accessToken, allowCreateNewAnonymousUser, authorizationListener);
public void signinAnonymously(@NotNull Context context, String accessToken, @NotNull AuthorizationListener authorizationListener){
this.signinAnonymously(context, accessToken, true, authorizationListener);
}

public void signinAnonymously(@NotNull Context context, String accessToken, boolean allowCreateNewAnonymousUser, @NotNull AuthorizationListener authorizationListener){
oAuthManager.getAuthorizationManager().signinAnonymously(context, accessToken, allowCreateNewAnonymousUser, authorizationListener);
}

/**
Expand All @@ -150,28 +188,69 @@ public void loginAnonymously(@NotNull Context context, String accessToken, bool
* @param password the resource owner password
* @param tokenResponseListener the token response listener
*/
public void obtainTokensWithROP(@NotNull Context context, @NotNull String username, @NotNull String password, @NotNull TokenResponseListener tokenResponseListener) {
public void signinWithResourceOwnerPassword(@NotNull Context context, @NotNull String username, @NotNull String password, @NotNull TokenResponseListener tokenResponseListener) {
AccessToken accessToken = oAuthManager.getTokenManager().getLatestAccessToken();
if (accessToken != null && accessToken.isAnonymous()) {
oAuthManager.getAuthorizationManager().obtainTokensWithROP(context, username, password, accessToken.getRaw(), tokenResponseListener);
oAuthManager.getAuthorizationManager().signinWithResourceOwnerPassword(context, username, password, accessToken.getRaw(), tokenResponseListener);
}
oAuthManager.getAuthorizationManager().obtainTokensWithROP(context, username, password, null, tokenResponseListener);
oAuthManager.getAuthorizationManager().signinWithResourceOwnerPassword(context, username, password, null, tokenResponseListener);
}

/**
* Obtain token using Resource owner Password (RoP).
*
* @param username the resource owner username
* @param password the resource owner password
* @param tokenResponseListener the token response listener
* @param accessTokenString previous access token of some anonymous user
*/
/**
* @deprecated use {@link #obtainTokensWithROP(Context, String, String, TokenResponseListener, String)}
*/
public void obtainTokensWithROP(@NotNull Context context, @NotNull String username, @NotNull String password, @NotNull TokenResponseListener tokenResponseListener, String accessTokenString) {
if(accessTokenString == null) {
obtainTokensWithROP(context, username, password, tokenResponseListener);
signinWithResourceOwnerPassword(context, username, password, tokenResponseListener, accessTokenString);
}

/**
* @deprecated use {@link #obtainTokensWithROP(Context, String, String, TokenResponseListener)}
*/
public void obtainTokensWithROP(@NotNull Context context, @NotNull String username, @NotNull String password, @NotNull TokenResponseListener tokenResponseListener) {
signinWithResourceOwnerPassword(context, username, password, tokenResponseListener);
}

/**
* Obtain token using Resource owner Password (RoP).
*
* @param username the resource owner username
* @param password the resource owner password
* @param tokenResponseListener the token response listener
* @param accessTokenString previous access token of some anonymous user
*/
public void signinWithResourceOwnerPassword(@NotNull Context context, @NotNull String username, @NotNull String password, @NotNull TokenResponseListener tokenResponseListener, String accessTokenString) {
if(accessTokenString == null) {
signinWithResourceOwnerPassword(context, username, password, tokenResponseListener);
} else {
oAuthManager.getAuthorizationManager().obtainTokensWithROP(context, username, password, accessTokenString, tokenResponseListener);
oAuthManager.getAuthorizationManager().signinWithResourceOwnerPassword(context, username, password, accessTokenString, tokenResponseListener);
}
}

/**
* Obtain token using a refresh token
*
* @param refreshToken the refresh token
* @param tokenResponseListener the token response listener
*/
public void signinWithRefreshToken(@NotNull Context context, @NotNull String refreshToken, @NotNull TokenResponseListener tokenResponseListener) {
if (refreshToken == null) {
tokenResponseListener.onAuthorizationFailure(new AuthorizationException("Missing refresh-token"));
return;
}
oAuthManager.getAuthorizationManager().signinWithRefreshToken(context, refreshToken, tokenResponseListener);
}

/**
* Obtain token using the latest refresh token stored in the SDK
*
* @param tokenResponseListener the token response listener
*/
public void signinWithRefreshToken(@NotNull Context context, @NotNull TokenResponseListener tokenResponseListener) {
String refreshTokenString = null;
RefreshToken refreshToken = oAuthManager.getTokenManager().getLatestRefreshToken();
if (refreshToken != null) {
refreshTokenString = refreshToken.getRaw();
}
signinWithRefreshToken(context, refreshTokenString, tokenResponseListener);
}
}
Loading

0 comments on commit 81fef1c

Please sign in to comment.