-
Notifications
You must be signed in to change notification settings - Fork 0
/
CognitoAndroidCustomLogin.java
132 lines (119 loc) · 6.03 KB
/
CognitoAndroidCustomLogin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.example.rishabh.example;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.ResponseMetadata;
import com.amazonaws.auth.AWSAbstractCognitoDeveloperIdentityProvider;
import com.amazonaws.auth.AWSAbstractCognitoIdentityProvider;
import com.amazonaws.auth.AWSBasicCognitoIdentityProvider;
import com.amazonaws.auth.AWSCognitoIdentityProvider;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSEnhancedCognitoIdentityProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.auth.CognitoCredentialsProvider;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBQueryExpression;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentity;
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient;
import com.amazonaws.services.cognitoidentity.model.Credentials;
import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityRequest;
import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityResult;
import com.amazonaws.services.cognitoidentity.model.GetIdRequest;
import com.amazonaws.services.cognitoidentity.model.GetIdResult;
import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenRequest;
import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenResult;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.s3.AmazonS3;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class CognitoCustomLogin extends AppCompatActivity {
OkHttpClient client = new OkHttpClient();
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
GetCredentialsForIdentityResult getCredentialsResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cognito_custom_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
new sendUserDetailsAndReadDataSecurely().execute();
}
String doPostRequest(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
class sendUserDetailsAndReadDataSecurely extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", "abc");
jsonObject.put("password", "cde");
} catch (JSONException e) {
e.printStackTrace();
}
String identityId = null;
String token = null;
String val = null;
try {
val = doPostRequest("http://localhost:5000", jsonObject.toString());
JSONObject jsonObject1 = new JSONObject(val);
identityId = jsonObject1.getString("IdentityId");
token = jsonObject1.getString("Token");
} catch (Exception ex) {
}
Map<String, String> logins = new HashMap();
logins.put("cognito-identity.amazonaws.com", token);
GetCredentialsForIdentityRequest getCredentialsRequest = new GetCredentialsForIdentityRequest().withIdentityId(identityId).withLogins(logins);
AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials());
getCredentialsResult = cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);
BasicSessionCredentials sessionCredentials = new BasicSessionCredentials(
getCredentialsResult.getCredentials().getAccessKeyId(),
getCredentialsResult.getCredentials().getSecretKey(),
getCredentialsResult.getCredentials().getSessionToken());
AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(sessionCredentials);
DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
PostDataClass postToFind = new PostDataClass();
postToFind.setemployee_id("123");
DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
.withHashKeyValues(postToFind)
.withScanIndexForward(false)
.withConsistentRead(false);
List<PostDataClass> post_data_class_list = mapper.query(PostDataClass.class, queryExpression);
for (PostDataClass postDataClass : post_data_class_list) {
Log.d("my_info", postDataClass.toString() + "#" + postDataClass.getComments());
}
return null;
}
}
}