Skip to content

Commit ee27b04

Browse files
committed
Add option for web auth
1 parent 056be28 commit ee27b04

File tree

5 files changed

+125
-18
lines changed

5 files changed

+125
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ build
66
.classpath
77
bin
88
.vscode
9+
.idea
10+
.gradle

afero-sdk-client-retrofit2/src/main/java/io/afero/sdk/client/retrofit2/AferoClientRetrofit2.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ private Config validate() {
126126
e = new IllegalArgumentException("oauthClientId must be specified");
127127
}
128128

129-
if (oauthClientSecret == null || oauthClientSecret.isEmpty()) {
130-
e = new IllegalArgumentException("oauthClientSecret must be specified");
131-
}
132-
133129
if (httpLogLevel == null) {
134130
e = new IllegalArgumentException("httpLogLevel cannot be null");
135131
}
@@ -249,7 +245,7 @@ public AferoClientRetrofit2(Config config) {
249245
mConfig = config;
250246
mHttpClient = createHttpClient(config.httpLogLevel, config.defaultTimeout);
251247
mAferoService = createRetrofit().create(AferoClientAPI.class);
252-
mOAuthAuthorizationBase64 = Credentials.basic(config.oauthClientId, config.oauthClientSecret);
248+
mOAuthAuthorizationBase64 = config.oauthClientSecret != null ? Credentials.basic(config.oauthClientId, config.oauthClientSecret) : "";
253249
}
254250

255251
// don't use me

samples/afero-lab/app/build.gradle

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,18 @@ android {
3535
versionName '1.0'
3636

3737
buildConfigField 'String', 'AFERO_CLIENT_ID', project.aferoClientId
38-
buildConfigField 'String', 'AFERO_CLIENT_SECRET', project.aferoClientSecret
38+
buildConfigField 'String', 'AFERO_CLIENT_SECRET', project.findProperty('aferoClientSecret') ?: 'null'
3939
buildConfigField 'String', 'AFERO_SERVICE_URL', project.findProperty('aferoServiceUrl') ?: '"https://api.afero.io"'
4040
buildConfigField 'String', 'AFERO_SOFTHUB_SERVICE', project.findProperty('aferoSofthubService') ?: '"prod"'
41+
42+
buildConfigField 'String', 'AFERO_OAUTH_AUTH_URL', project.findProperty('aferoOauthAuthUrl') ?: 'null'
43+
buildConfigField 'String', 'AFERO_OAUTH_TOKEN_URL', project.findProperty('aferoOauthTokenUrl') ?: 'null'
44+
buildConfigField 'String', 'AFERO_OAUTH_REDIRECT_SCHEME', project.findProperty('aferoOauthRedirectScheme') ?: 'null'
45+
buildConfigField 'String', 'AFERO_OAUTH_REDIRECT_HOST', project.findProperty('aferoOauthRedirectHost') ?: 'null'
46+
47+
manifestPlaceholders = [scheme: project.findProperty('aferoOauthRedirectScheme') ?: 'null',
48+
host: project.findProperty('aferoOauthRedirectHost') ?: 'null']
49+
4150
}
4251

4352
buildTypes {
@@ -66,10 +75,16 @@ dependencies {
6675
implementation fileTree(dir: 'libs', include: ['*.jar'])
6776

6877
// https://github.com/aferodeveloper/AferoJavaSDK
69-
implementation "io.afero.sdk:afero-sdk-core:${sdkVersion}"
70-
implementation "io.afero.sdk:afero-sdk-client-retrofit2:${sdkVersion}"
71-
implementation "io.afero.sdk:afero-sdk-android:${sdkVersion}@aar"
72-
implementation "io.afero.sdk:afero-sdk-softhub:${sdkVersion}@aar"
78+
// implementation "io.afero.sdk:afero-sdk-core:${sdkVersion}"
79+
// implementation "io.afero.sdk:afero-sdk-client-retrofit2:${sdkVersion}"
80+
// implementation "io.afero.sdk:afero-sdk-android:${sdkVersion}@aar"
81+
// implementation "io.afero.sdk:afero-sdk-softhub:${sdkVersion}@aar"
82+
compile files('../../../afero-sdk-client-retrofit2/build/libs/afero-sdk-client-retrofit2.jar')
83+
compile files('../../../afero-sdk-android/build/outputs/aar/afero-sdk-android-debug.aar')
84+
compile files('../../../afero-sdk-core/build/libs/afero-sdk-core.jar')
85+
compile files('../../../afero-sdk-softhub/build/outputs/aar/afero-sdk-softhub-debug.aar')
86+
87+
7388
implementation "io.afero.sdk:hubby:1.0.844@aar"
7489

7590
// https://github.com/square/retrofit
@@ -100,4 +115,7 @@ dependencies {
100115
testImplementation 'junit:junit:4.12'
101116
implementation 'com.android.support:cardview-v7:27.1.1'
102117
implementation 'com.android.support:design:27.1.1'
118+
119+
//https://github.com/openid/AppAuth-Android
120+
implementation 'net.openid:appauth:0.9.1'
103121
}

samples/afero-lab/app/src/main/AndroidManifest.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
-->
55

66
<manifest package="io.afero.aferolab"
7-
xmlns:android="http://schemas.android.com/apk/res/android">
7+
xmlns:android="http://schemas.android.com/apk/res/android"
8+
xmlns:tools="http://schemas.android.com/tools">
89

910
<uses-permission android:name="android.permission.INTERNET"/>
1011
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
@@ -30,6 +31,19 @@
3031
<category android:name="android.intent.category.LAUNCHER"/>
3132
</intent-filter>
3233
</activity>
34+
<activity
35+
android:name="net.openid.appauth.RedirectUriReceiverActivity"
36+
tools:node="replace"
37+
>
38+
<intent-filter>
39+
<action android:name="android.intent.action.VIEW" />
40+
<category android:name="android.intent.category.DEFAULT" />
41+
<category android:name="android.intent.category.BROWSABLE" />
42+
<data
43+
android:scheme="hubspace-app"
44+
android:host="loginredirect" />
45+
</intent-filter>
46+
</activity>
3347
</application>
3448

3549
</manifest>

samples/afero-lab/app/src/main/java/io/afero/aferolab/MainActivity.java

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
package io.afero.aferolab;
66

7+
import android.app.Activity;
78
import android.content.BroadcastReceiver;
89
import android.content.Context;
910
import android.content.Intent;
1011
import android.content.IntentFilter;
1112
import android.net.ConnectivityManager;
1213
import android.net.NetworkInfo;
14+
import android.net.Uri;
1315
import android.os.Bundle;
1416
import android.support.annotation.NonNull;
1517
import android.support.v7.app.AppCompatActivity;
@@ -19,8 +21,20 @@
1921
import android.view.MenuItem;
2022
import android.view.View;
2123
import android.view.ViewGroup;
24+
import android.widget.Button;
2225
import android.widget.TextView;
2326

27+
import androidx.activity.result.ActivityResultLauncher;
28+
import androidx.activity.result.contract.ActivityResultContracts;
29+
30+
import net.openid.appauth.AuthState;
31+
import net.openid.appauth.AuthorizationException;
32+
import net.openid.appauth.AuthorizationRequest;
33+
import net.openid.appauth.AuthorizationResponse;
34+
import net.openid.appauth.AuthorizationService;
35+
import net.openid.appauth.AuthorizationServiceConfiguration;
36+
import net.openid.appauth.ResponseTypeValues;
37+
2438
import java.lang.ref.WeakReference;
2539
import java.net.HttpURLConnection;
2640

@@ -74,6 +88,11 @@ public class MainActivity extends AppCompatActivity {
7488
private AferoSofthub mAferoSofthub;
7589
private ConnectivityReceiver mConnectivityReceiver;
7690

91+
private ActivityResultLauncher<Intent> launcher;
92+
private AuthorizationService mAuthService;
93+
private AuthorizationServiceConfiguration mServiceConfig;
94+
private AuthState mAuthState;
95+
7796
private String mUserId;
7897

7998
private final Observer<AferoSofthub> mAferoSofthubStartObserver = new RxUtils.IgnoreResponseObserver<>();
@@ -99,6 +118,12 @@ public class MainActivity extends AppCompatActivity {
99118
@BindView(R.id.edit_text_password)
100119
AferoEditText mPasswordEditText;
101120

121+
@BindView(R.id.button_forgot_password)
122+
Button mForgotPasswordButton;
123+
124+
@BindView(R.id.button_sign_in)
125+
Button mSignInButton;
126+
102127
@BindView(R.id.group_sign_in)
103128
ViewGroup mSignInGroup;
104129

@@ -135,14 +160,38 @@ protected void onCreate(Bundle savedInstanceState) {
135160
? new AccessToken(accessToken, refreshToken)
136161
: null;
137162

138-
AferoClientRetrofit2.Config aferoClientConfig = new AferoClientRetrofit2.ConfigBuilder()
163+
AferoClientRetrofit2.ConfigBuilder configBuilder = new AferoClientRetrofit2.ConfigBuilder()
139164
.oauthClientId(BuildConfig.AFERO_CLIENT_ID)
140-
.oauthClientSecret(BuildConfig.AFERO_CLIENT_SECRET)
141165
.baseUrl(BuildConfig.AFERO_SERVICE_URL)
142-
.logLevel(BuildConfig.HTTP_LOG_LEVEL)
143-
.build();
166+
.logLevel(BuildConfig.HTTP_LOG_LEVEL);
167+
168+
if (BuildConfig.AFERO_CLIENT_SECRET != null) {
169+
configBuilder.oauthClientSecret(BuildConfig.AFERO_CLIENT_SECRET);
170+
}
171+
172+
if (BuildConfig.AFERO_OAUTH_AUTH_URL != null && BuildConfig.AFERO_OAUTH_TOKEN_URL != null) {
173+
mServiceConfig =
174+
new AuthorizationServiceConfiguration(
175+
Uri.parse(BuildConfig.AFERO_OAUTH_AUTH_URL), // authorization endpoint
176+
Uri.parse(BuildConfig.AFERO_OAUTH_TOKEN_URL));
177+
mAuthState = new AuthState(mServiceConfig);
178+
launcher = registerForActivityResult(
179+
new ActivityResultContracts.StartActivityForResult(),
180+
result -> {
181+
if (result.getResultCode() == Activity.RESULT_OK) {
182+
// There are no request codes
183+
Intent data = result.getData();
184+
AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
185+
AuthorizationException ex = AuthorizationException.fromIntent(data);
186+
187+
System.out.println("Access Token " + response.authorizationCode);
188+
exchangeAuthorizationCode(response);
189+
}
190+
});
191+
}
144192

145-
mAferoClient = new AferoClientRetrofit2(aferoClientConfig);
193+
194+
mAferoClient = new AferoClientRetrofit2(configBuilder.build());
146195
mAferoClient.setOwnerAndActiveAccountId(accountId);
147196

148197
mDeviceCollection = new DeviceCollection(mAferoClient);
@@ -320,6 +369,12 @@ private void stopDeviceInspector() {
320369
}
321370

322371
private void setupViews() {
372+
if (BuildConfig.AFERO_OAUTH_AUTH_URL != null) {
373+
mEmailEditText.setVisibility(View.GONE);
374+
mPasswordEditText.setVisibility(View.GONE);
375+
mForgotPasswordButton.setVisibility(View.GONE);
376+
}
377+
323378
if (isSignedIn()) {
324379
mSignInGroup.setVisibility(View.GONE);
325380
mStatusGroup.setVisibility(View.VISIBLE);
@@ -380,8 +435,30 @@ public boolean onEditorActionSignIn(TextView textView, int actionId, KeyEvent ev
380435

381436
@OnClick(R.id.button_sign_in)
382437
public void onClickSignIn() {
383-
mPasswordEditText.hideKeyboard();
384-
startSignIn(mEmailEditText.getText().toString(), mPasswordEditText.getText().toString());
438+
if (BuildConfig.AFERO_OAUTH_AUTH_URL != null) {
439+
440+
mSignInButton.setEnabled(false);
441+
showConclaveStatus(ConclaveClient.Status.CONNECTING);
442+
443+
AuthorizationRequest.Builder authRequestBuilder =
444+
new AuthorizationRequest.Builder(
445+
mServiceConfig, // the authorization service configuration
446+
"hubspace_android",
447+
// the client ID, typically pre-registered and static
448+
ResponseTypeValues.CODE, // the response_type value: we want a code
449+
Uri.parse("hubspace-app://loginredirect")
450+
);
451+
452+
453+
mAuthService = new AuthorizationService(this);
454+
Intent authIntent = mAuthService.getAuthorizationRequestIntent(authRequestBuilder.build());
455+
456+
launcher.launch(authIntent);
457+
} else {
458+
459+
mPasswordEditText.hideKeyboard();
460+
startSignIn(mEmailEditText.getText().toString(), mPasswordEditText.getText().toString());
461+
}
385462
}
386463

387464
@OnClick(R.id.button_forgot_password)

0 commit comments

Comments
 (0)