Skip to content

Commit

Permalink
upload lab04
Browse files Browse the repository at this point in the history
  • Loading branch information
archcentric committed Apr 14, 2018
1 parent cabb479 commit 9f5fa53
Show file tree
Hide file tree
Showing 67 changed files with 1,783 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lab04/AuthCodeApp/.gitignore
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.
29 changes: 29 additions & 0 deletions lab04/AuthCodeApp/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions lab04/AuthCodeApp/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions lab04/AuthCodeApp/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lab04/AuthCodeApp/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lab04/AuthCodeApp/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions lab04/AuthCodeApp/app/build.gradle
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'
}
21 changes: 21 additions & 0 deletions lab04/AuthCodeApp/app/proguard-rules.pro
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
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());
}
}
35 changes: 35 additions & 0 deletions lab04/AuthCodeApp/app/src/main/AndroidManifest.xml
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>
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);
}

}
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();
}
}
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);

}
}
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);
}
}
}
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);
}
}
Loading

0 comments on commit 9f5fa53

Please sign in to comment.