forked from spring2go/oauth2lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
archcentric
committed
Apr 14, 2018
1 parent
cabb479
commit 9f5fa53
Showing
67 changed files
with
1,783 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 27 | ||
defaultConfig { | ||
applicationId "spring2go.io.authcodeapp" | ||
minSdkVersion 21 | ||
targetSdkVersion 27 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
packagingOptions { | ||
exclude 'META-INF/LICENSE' | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'com.android.support:appcompat-v7:27.1.1' | ||
implementation 'com.android.support.constraint:constraint-layout:1.0.2' | ||
testImplementation 'junit:junit:4.12' | ||
compile 'com.squareup.retrofit2:retrofit:2.3.0' | ||
compile 'com.squareup.retrofit2:converter-jackson:2.3.0' | ||
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.1' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
...uthCodeApp/app/src/androidTest/java/spring2go/io/authcodeapp/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package spring2go.io.authcodeapp; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
||
assertEquals("spring2go.io.authcodeapp", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="spring2go.io.authcodeapp"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".presenter.MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<activity android:name=".presenter.AuthorizationCodeActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.VIEW"/> | ||
<category android:name="android.intent.category.DEFAULT"/> | ||
<category android:name="android.intent.category.BROWSABLE"/> | ||
<data android:scheme="oauth2" | ||
android:host="userinfo" | ||
android:path="/callback"/> | ||
</intent-filter> | ||
</activity> | ||
<activity android:name=".presenter.UserInfoActivity"> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
22 changes: 22 additions & 0 deletions
22
lab04/AuthCodeApp/app/src/main/java/spring2go/io/authcodeapp/client/ClientAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package spring2go.io.authcodeapp.client; | ||
|
||
import spring2go.io.authcodeapp.client.interceptor.OAuth2ClientAuthenticationInterceptor; | ||
import spring2go.io.authcodeapp.client.oauth2.OAuth2API; | ||
import spring2go.io.authcodeapp.client.userinfo.UserInfoAPI; | ||
import spring2go.io.authcodeapp.client.userinfo.UserInfoAPI; | ||
|
||
public class ClientAPI { | ||
public static final String BASE_URL = "172.30.17.42:8080"; | ||
|
||
public static UserInfoAPI userInfo() { | ||
RetrofitAPIFactory api = new RetrofitAPIFactory(BASE_URL, null); | ||
return api.getRetrofit().create(UserInfoAPI.class); | ||
} | ||
|
||
public static OAuth2API oauth2() { | ||
RetrofitAPIFactory api = new RetrofitAPIFactory(BASE_URL, | ||
new OAuth2ClientAuthenticationInterceptor()); | ||
return api.getRetrofit().create(OAuth2API.class); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
lab04/AuthCodeApp/app/src/main/java/spring2go/io/authcodeapp/client/RetrofitAPIFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package spring2go.io.authcodeapp.client; | ||
|
||
import spring2go.io.authcodeapp.client.interceptor.BearerTokenHeaderInterceptor; | ||
import spring2go.io.authcodeapp.client.interceptor.ErrorInterceptor; | ||
import spring2go.io.authcodeapp.client.interceptor.OAuth2ClientAuthenticationInterceptor; | ||
import okhttp3.OkHttpClient; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
|
||
class RetrofitAPIFactory { | ||
private final Retrofit retrofit; | ||
|
||
RetrofitAPIFactory(String baseUrl, | ||
OAuth2ClientAuthenticationInterceptor clientAuthentication) { | ||
retrofit = new Retrofit.Builder() | ||
.baseUrl("http://" + baseUrl) | ||
.addConverterFactory(JacksonConverterFactory.create()) | ||
.client(createClient(clientAuthentication)) | ||
.build(); | ||
} | ||
|
||
public Retrofit getRetrofit() { return retrofit; } | ||
|
||
private OkHttpClient createClient( | ||
OAuth2ClientAuthenticationInterceptor clientAuthentication) { | ||
OkHttpClient.Builder client = new OkHttpClient.Builder(); | ||
client.addInterceptor(new ErrorInterceptor()); | ||
client.addInterceptor(new BearerTokenHeaderInterceptor()); | ||
if (clientAuthentication != null) { | ||
client.addInterceptor(clientAuthentication); | ||
} | ||
return client.build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c/main/java/spring2go/io/authcodeapp/client/interceptor/BearerTokenHeaderInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package spring2go.io.authcodeapp.client.interceptor; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* Interceptor that adds Bearer prefix to an access token | ||
*/ | ||
public class BearerTokenHeaderInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
|
||
Request request = chain.request(); | ||
|
||
List<String> headers = request.headers("Authorization"); | ||
if (headers.size() > 0) { | ||
String accessTokenValue = headers.get(0); | ||
|
||
request = request.newBuilder() | ||
.removeHeader("Authorization") | ||
.addHeader("Authorization", "Bearer " + accessTokenValue) | ||
.build(); | ||
|
||
} | ||
|
||
return chain.proceed(request); | ||
|
||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...deApp/app/src/main/java/spring2go/io/authcodeapp/client/interceptor/ErrorInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package spring2go.io.authcodeapp.client.interceptor; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class ErrorInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
Response response = chain.proceed(request); | ||
|
||
boolean httpError = (response.code() >= 400); | ||
if (httpError) { | ||
throw new HttpException(response.code() + ":" + response.message()); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
public static class HttpException extends RuntimeException { | ||
public HttpException(String message) { | ||
super(message); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...va/spring2go/io/authcodeapp/client/interceptor/OAuth2ClientAuthenticationInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package spring2go.io.authcodeapp.client.interceptor; | ||
|
||
import android.util.Base64; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class OAuth2ClientAuthenticationInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
|
||
Request authenticatedRequest = request.newBuilder() | ||
.addHeader("Authorization", getEncodedAuthorization()) | ||
.addHeader("Content-Type", "application/x-www-form-urlencoded") | ||
.method(request.method(), request.body()) | ||
.build(); | ||
|
||
return chain.proceed(authenticatedRequest); | ||
} | ||
|
||
private String getEncodedAuthorization() { | ||
return "Basic " + Base64.encodeToString( | ||
"mobileclient:112233".getBytes(), Base64.NO_WRAP); | ||
} | ||
} |
Oops, something went wrong.