Java tutorial
/** * Copyright 2010-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * <p/> * http://aws.amazon.com/apache2.0 * <p/> * or in the "license" file accompanying this file. This file 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. */ package com.amazonaws.cognito; import android.content.Context; import android.util.Log; import com.amazonaws.auth.AWSAbstractCognitoDeveloperIdentityProvider; import com.amazonaws.regions.Regions; import com.amazonaws.utilities.Global; import com.amazonaws.utilities.JsonParser; import com.amazonaws.utilities.NetworkClass; import com.amazonaws.utilities.Prefs; import com.amazonaws.utilities.httpResponseStatusInfo; import org.json.JSONException; import org.json.JSONObject; public class DeveloperAuthenticationProvider extends AWSAbstractCognitoDeveloperIdentityProvider { Context ctx; public DeveloperAuthenticationProvider(Context context, String accountId, String identityPoolId, Regions region) { super(accountId, identityPoolId, region); this.ctx = context; } @Override public String refresh() { setToken(null); // If there is a key with developer provider name in the logins map, it // means the app user has used developer credentials if (getProviderName() != null && !this.loginsMap.isEmpty() && this.loginsMap.containsKey(getProviderName())) { Log.e("Dev Auth", "Refreshing - Loginmap contains provider name"); Foo foo = new Foo(); Thread t = new Thread(foo); t.start(); try { t.join(); } catch (Exception e) { e.printStackTrace(); } update(Global.myAccountInfo.getIdentityId(), Global.myAccountInfo.getToken()); return Global.myAccountInfo.getToken(); } else { Log.e("Dev Auth", "Refreshing - Loginmap is empty"); this.getIdentityId(); return null; } } @Override public String getIdentityId() { identityId = CognitoSyncClientManager.credentialsProvider.getCachedIdentityId(); if (identityId == null) { if (getProviderName() != null && !this.loginsMap.isEmpty() && this.loginsMap.containsKey(getProviderName())) { Log.e("Dev Auth", "Identity id is null"); Foo foo = new Foo(); Thread t = new Thread(foo); t.start(); try { t.join(); } catch (Exception e) { e.printStackTrace(); } // update(Global.myAccountInfo.getIdentityId(), Global.myAccountInfo.getToken()); return foo.getIdentityId(); } else { return super.getIdentityId(); } } else { Log.e("Dev Auth", "Identity id is available"); return identityId; } } @Override public String getProviderName() { return Constants.DEVELOPER_PROVIDER_NAME; } /* * Attempt to Refresh token */ public class Foo implements Runnable { private volatile String id; @Override public void run() { JSONObject json = new JSONObject(); try { json.put("email", Prefs.getString("email", "")); json.put("identity_id", Prefs.getString("id", "")); json.put("token", Prefs.getString("token", "")); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String responseLogin = NetworkClass.HTTPPost(Global.MOBILE_CUSTOMER_REFRESH_TOKEN_URL, json); Log.d("Dev Auth", "URL: " + Global.MOBILE_CUSTOMER_REFRESH_TOKEN_URL + ", RESPONSE : " + responseLogin); id = processLoginResponse(responseLogin); } public String getIdentityId() { return id; } } /* * Process response */ private String processLoginResponse(String response) { String id = ""; int status = httpResponseStatusInfo.getStatusCode(); if (status == 200) { try { JSONObject json = new JSONObject(response); if (json.getBoolean("login")) { Global.myAccountInfo = JsonParser.getInfo(response); id = json.getString("identityId"); Prefs.putString("id", id); Prefs.putString("token", json.getString("token")); } } catch (JSONException e) { e.printStackTrace(); } } return id; } }