forked from auth0/Guardian.Android
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationActivity.java
More file actions
254 lines (219 loc) · 10.1 KB
/
NotificationActivity.java
File metadata and controls
254 lines (219 loc) · 10.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (c) 2016 Auth0 (http://auth0.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.auth0.guardian.sample;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.auth0.android.guardian.sdk.Guardian;
import com.auth0.android.guardian.sdk.GuardianException;
import com.auth0.android.guardian.sdk.ParcelableNotification;
import com.auth0.android.guardian.sdk.RichConsent;
import com.auth0.android.guardian.sdk.networking.Callback;
import com.auth0.guardian.sample.fragments.AuthenticationRequestDetailsFragment;
import com.auth0.guardian.sample.fragments.consent.ConsentBasicDetailsFragment;
import com.auth0.guardian.sample.fragments.consent.ConsentPaymentInitiationFragment;
import com.auth0.guardian.sample.fragments.consent.DynamicAuthorizationDetailsFragment;
import com.auth0.guardian.sample.consent.authorization.details.payments.PaymentInitiationDetails;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;
public class NotificationActivity extends AppCompatActivity {
private static final String TAG = NotificationActivity.class.getName();
private Guardian guardian;
private ParcelableEnrollment enrollment;
private ParcelableNotification notification;
private RichConsent richConsent;
static Intent getStartIntent(@NonNull Context context,
@NonNull ParcelableNotification notification,
@NonNull ParcelableEnrollment enrollment) {
if (!enrollment.getId().equals(notification.getEnrollmentId())) {
final String message = String.format("Notification doesn't match enrollment (%s != %s)",
notification.getEnrollmentId(), enrollment.getId());
throw new IllegalArgumentException(message);
}
Intent intent = new Intent(context, NotificationActivity.class);
intent.putExtra(Constants.ENROLLMENT, enrollment);
intent.putExtra(Constants.NOTIFICATION, notification);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
guardian = new Guardian.Builder()
.url(Uri.parse(getString(R.string.tenant_url)))
.enableLogging()
.build();
Intent intent = getIntent();
enrollment = intent.getParcelableExtra(Constants.ENROLLMENT);
notification = intent.getParcelableExtra(Constants.NOTIFICATION);
setupUI();
if (notification.getTransactionLinkingId() != null) {
try {
guardian.fetchConsent(notification, enrollment).start(new Callback<RichConsent>() {
@Override
public void onSuccess(RichConsent response) {
richConsent = response;
updateUI();
}
@Override
public void onFailure(Throwable exception) {
if (exception instanceof GuardianException) {
GuardianException guardianException = (GuardianException) exception;
if (guardianException.isResourceNotFound()) {
// Render regular authentication request details
updateUI();
}
} else {
Log.e(TAG, "Error requesting consent details", exception);
throw new RuntimeException(exception);
}
}
});
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
} else {
updateUI();
}
}
private void setupUI() {
// TODO: spinner fragment
Button rejectButton = (Button) findViewById(R.id.rejectButton);
assert rejectButton != null;
rejectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rejectRequested();
}
});
Button allowButton = (Button) findViewById(R.id.allowButton);
assert allowButton != null;
allowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allowRequested();
}
});
}
private void updateUI() {
Fragment fragment;
if (richConsent == null) {
fragment = getStandardAuthenticationFragment();
} else if (richConsent.getRequestedDetails().getAuthorizationDetails().isEmpty()) {
fragment = getBasicConsentFragment();
} else {
List<PaymentInitiationDetails> paymentInitiationDetailsList = richConsent
.getRequestedDetails()
.filterAuthorizationDetailsByType(PaymentInitiationDetails.class);
if (!paymentInitiationDetailsList.isEmpty()) {
// For simplicity, in this example we render one single type
fragment = getPaymentInitiationConsentFragment(paymentInitiationDetailsList.get(0));
} else {
fragment = getDynamicAuthorizationDetailsConsentFragment();
}
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.authenticationDetailsFragmentContainer, fragment)
.commit();
}
@NonNull
private DynamicAuthorizationDetailsFragment getDynamicAuthorizationDetailsConsentFragment() {
return DynamicAuthorizationDetailsFragment.newInstance(
richConsent.getRequestedDetails().getBindingMessage(),
notification.getDate().toString(),
// For simplicity, in this example we render one single type
richConsent.getRequestedDetails().getAuthorizationDetails().get(0)
);
}
@NonNull
private ConsentPaymentInitiationFragment getPaymentInitiationConsentFragment(PaymentInitiationDetails paymentDetails) {
return ConsentPaymentInitiationFragment.newInstance(
richConsent.getRequestedDetails().getBindingMessage(),
paymentDetails.getRemittanceInformation(),
paymentDetails.getCreditorAccount().getAccountNumber(),
paymentDetails.getInstructedAmount().getCurrency(),
paymentDetails.getInstructedAmount().getAmount()
);
}
@NonNull
private ConsentBasicDetailsFragment getBasicConsentFragment() {
return ConsentBasicDetailsFragment.newInstance(
richConsent.getRequestedDetails().getBindingMessage(),
richConsent.getRequestedDetails().getScope(),
notification.getDate().toString()
);
}
@NonNull
private AuthenticationRequestDetailsFragment getStandardAuthenticationFragment() {
return AuthenticationRequestDetailsFragment.newInstance(
enrollment.getUserId(),
String.format("%s, %s",
notification.getBrowserName(),
notification.getBrowserVersion()),
String.format("%s, %s",
notification.getOsName(),
notification.getOsVersion()),
notification.getLocation(),
notification.getDate().toString()
);
}
private void rejectRequested() {
guardian
.reject(notification, enrollment)
.start(new DialogCallback<>(this,
R.string.progress_title_please_wait,
R.string.progress_message_reject,
new Callback<Void>() {
@Override
public void onSuccess(Void response) {
finish();
}
@Override
public void onFailure(Throwable exception) {
}
}));
}
private void allowRequested() {
guardian
.allow(notification, enrollment)
.start(new DialogCallback<>(this,
R.string.progress_title_please_wait,
R.string.progress_message_allow,
new Callback<Void>() {
@Override
public void onSuccess(Void response) {
finish();
}
@Override
public void onFailure(Throwable exception) {
}
}));
}
}