Skip to content

Commit

Permalink
Merge pull request #37 from rheinfabrik/feature/update_sample
Browse files Browse the repository at this point in the history
Feature/update sample
  • Loading branch information
Gi-lo committed Feb 15, 2016
2 parents 987a1db + 54a8d72 commit f6a92f2
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 142 deletions.
12 changes: 10 additions & 2 deletions .travis.yml
@@ -1,18 +1,26 @@
language: android

jdk: oraclejdk8

env:
global:
- MALLOC_ARENA_MAX=2
matrix:
- ANDROID_TARGET=android-22 ANDROID_ABI=armeabi-v7a

android:
components:
- build-tools-22.0.1
- tools
- build-tools-23.0.2
- android-23
- android-22
- sys-img-armeabi-v7a-android-22
- extra-google-m2repository
- extra-android-m2repository
- extra-android-support


sudo: required

before_script:
- echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
- emulator -avd test -no-skin -no-audio -no-window &
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon Jun 15 17:18:44 CEST 2015
#Tue Feb 09 20:24:04 CET 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip
14 changes: 7 additions & 7 deletions library/build.gradle
Expand Up @@ -20,8 +20,8 @@ buildscript {
group = 'com.github.rheinfabrik'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 23
buildToolsVersion "23.0.2"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -36,9 +36,9 @@ android {

defaultConfig {
minSdkVersion 9
targetSdkVersion 22
versionCode 103
versionName "1.0.3"
targetSdkVersion 23
versionCode 104
versionName "1.0.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand All @@ -57,10 +57,10 @@ repositories {
dependencies {

// Rx
compile 'io.reactivex:rxjava:1.0.12'
compile 'io.reactivex:rxjava:1.1.1'

// GSON
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.google.code.gson:gson:2.4'

// Spock
androidTestCompile 'org.codehaus.groovy:groovy:2.4.2:grooid'
Expand Down
Expand Up @@ -5,7 +5,7 @@ import de.rheinfabrik.heimdall.grants.OAuth2Grant
import de.rheinfabrik.heimdall.grants.OAuth2RefreshAccessTokenGrant
import spock.lang.Title

import static rx.Observable.just
import static rx.Single.just

@Title("Tests for the constructor of the OAuth2AccessTokenManager class")
class OAuth2AccessTokenManagerConstructorSpecs extends AndroidSpecification {
Expand Down Expand Up @@ -63,7 +63,7 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat
Calendar calendar = Calendar.getInstance()

when: "I ask for a new access token"
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant, calendar).toBlocking().first()
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant, calendar).toBlocking().value()

then: "The access token should have the correct expiration date"
newToken.expirationDate.timeInMillis == calendar.getTimeInMillis() + 3000
Expand All @@ -83,7 +83,7 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))

when: "I ask for a new access token"
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant).toBlocking().first()
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant).toBlocking().value()

then: "The access token should have the NO expiration date"
newToken.expirationDate == null
Expand All @@ -105,7 +105,7 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)

when: "I ask for a new access token"
tokenManager.grantNewAccessToken(grant).toBlocking().first()
tokenManager.grantNewAccessToken(grant).toBlocking().value()

then: "The storage is asked to save the token"
1 * storage.storeAccessToken(accessToken)
Expand Down Expand Up @@ -171,7 +171,7 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)

when: "I ask for a valid access token"
OAuth2AccessToken validToken = tokenManager.getValidAccessToken(grant).toBlocking().first()
OAuth2AccessToken validToken = tokenManager.getValidAccessToken(grant).toBlocking().value()

then: "The I receive the non-expired token"
validToken == accessToken
Expand Down
Expand Up @@ -53,7 +53,7 @@ class SharedPreferencesOAuth2AccessTokenStorageGetStoredAccessTokenSpecs extends
SharedPreferencesOAuth2AccessTokenStorage<OAuth2AccessToken> storage = new SharedPreferencesOAuth2AccessTokenStorage<>(preferences, OAuth2AccessToken.class)

when: "I ask for the access token"
OAuth2AccessToken token = storage.getStoredAccessToken().toBlocking().first()
OAuth2AccessToken token = storage.getStoredAccessToken().toBlocking().value()

then: "The token should have the correct access token"
token.accessToken == "2YotnFZFEjr1zCsicMWpAA"
Expand Down Expand Up @@ -111,7 +111,7 @@ class SharedPreferencesOAuth2AccessTokenStorageHasAccessTokenSpecs extends Andro
SharedPreferencesOAuth2AccessTokenStorage<OAuth2AccessToken> storage = new SharedPreferencesOAuth2AccessTokenStorage<>(preferences, OAuth2AccessToken.class)

when: "I ask if there is a token"
boolean hasToken = storage.hasAccessToken().toBlocking().first()
boolean hasToken = storage.hasAccessToken().toBlocking().value()

then: "It should be true"
hasToken == true
Expand All @@ -128,7 +128,7 @@ class SharedPreferencesOAuth2AccessTokenStorageHasAccessTokenSpecs extends Andro
SharedPreferencesOAuth2AccessTokenStorage<OAuth2AccessToken> storage = new SharedPreferencesOAuth2AccessTokenStorage<>(preferences, OAuth2AccessToken.class)

when: "I ask if there is a token"
boolean hasToken = storage.hasAccessToken().toBlocking().first()
boolean hasToken = storage.hasAccessToken().toBlocking().value()

then: "It should be false"
hasToken == false
Expand Down
Expand Up @@ -4,10 +4,10 @@

import de.rheinfabrik.heimdall.grants.OAuth2Grant;
import de.rheinfabrik.heimdall.grants.OAuth2RefreshAccessTokenGrant;
import rx.Observable;
import rx.Single;
import rx.functions.Func1;

import static rx.Observable.error;
import static rx.Observable.just;
import static rx.Single.error;

/**
* The all-seeing and all-hearing guardian sentry of your application who
Expand Down Expand Up @@ -55,7 +55,7 @@ public OAuth2AccessTokenStorage<TAccessToken> getStorage() {
* @param grant A class implementing the OAuth2Grant interface.
* @return - An observable emitting the granted access token.
*/
public Observable<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant) {
public Single<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant) {
return grantNewAccessToken(grant, Calendar.getInstance());
}

Expand All @@ -66,14 +66,14 @@ public Observable<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> gr
* @param calendar A calendar instance used to calculate the expiration date of the token.
* @return - An observable emitting the granted access token.
*/
public Observable<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant, Calendar calendar) {
public Single<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant, Calendar calendar) {
if (grant == null) {
throw new IllegalArgumentException("Grant MUST NOT be null.");
}

return grant
.grantNewAccessToken()
.doOnNext(accessToken -> {
.doOnSuccess(accessToken -> {
if (accessToken.expiresIn != null) {
Calendar expirationDate = (Calendar) calendar.clone();
expirationDate.add(Calendar.SECOND, accessToken.expiresIn);
Expand All @@ -92,23 +92,22 @@ public Observable<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> gr
* @param refreshAccessTokenGrant The refresh grant that will be used if the access token is expired.
* @return - An Observable emitting an unexpired access token.
*/
public Observable<TAccessToken> getValidAccessToken(final OAuth2RefreshAccessTokenGrant<TAccessToken> refreshAccessTokenGrant) {
public Single<TAccessToken> getValidAccessToken(final OAuth2RefreshAccessTokenGrant<TAccessToken> refreshAccessTokenGrant) {
if (refreshAccessTokenGrant == null) {
throw new IllegalArgumentException("RefreshAccessTokenGrant MUST NOT be null.");
}

return mStorage.getStoredAccessToken()
.concatMap(accessToken -> {
.flatMap(accessToken -> {
if (accessToken == null) {
return error(new IllegalStateException("No access token found."));
} else if (accessToken.isExpired()) {
refreshAccessTokenGrant.refreshToken = accessToken.refreshToken;

return grantNewAccessToken(refreshAccessTokenGrant);
} else {
return just(accessToken);
return Single.just(accessToken);
}
});
}

}
@@ -1,6 +1,6 @@
package de.rheinfabrik.heimdall;

import rx.Observable;
import rx.Single;

/**
* Interface used to define how to store and retrieve a stored access token.
Expand All @@ -16,7 +16,7 @@ public interface OAuth2AccessTokenStorage<TAccessToken extends OAuth2AccessToken
*
* @return - An Observable emitting the stored access token.
*/
Observable<TAccessToken> getStoredAccessToken();
Single<TAccessToken> getStoredAccessToken();

/**
* Stores the given access token.
Expand All @@ -31,7 +31,7 @@ public interface OAuth2AccessTokenStorage<TAccessToken extends OAuth2AccessToken
* @return - An Observable emitting true or false based on whether there is or is not an
* access token.
*/
Observable<Boolean> hasAccessToken();
Single<Boolean> hasAccessToken();

/**
* Removes the stored access token.
Expand Down
Expand Up @@ -6,7 +6,7 @@

import de.rheinfabrik.heimdall.OAuth2AccessToken;
import de.rheinfabrik.heimdall.OAuth2AccessTokenStorage;
import rx.Observable;
import rx.Single;

/**
* A simple storage that saves the access token as plain text in the passed shared preferences.
Expand Down Expand Up @@ -52,16 +52,9 @@ public SharedPreferencesOAuth2AccessTokenStorage(SharedPreferences sharedPrefere

@SuppressWarnings("unchecked")
@Override
public Observable<TAccessToken> getStoredAccessToken() {
return Observable
public Single<TAccessToken> getStoredAccessToken() {
return Single
.just(mSharedPreferences.getString(ACCESS_TOKEN_PREFERENCES_KEY, null))
.filter(accessToken -> {
if (accessToken == null) {
throw new RuntimeException("No access token found.");
}

return true;
})
.map(json -> (TAccessToken) new Gson().fromJson(json, mTokenClass));
}

Expand All @@ -74,8 +67,8 @@ public void storeAccessToken(TAccessToken accessToken) {
}

@Override
public Observable<Boolean> hasAccessToken() {
return Observable.just(mSharedPreferences.contains(ACCESS_TOKEN_PREFERENCES_KEY));
public Single<Boolean> hasAccessToken() {
return Single.just(mSharedPreferences.contains(ACCESS_TOKEN_PREFERENCES_KEY));
}

@Override
Expand Down
Expand Up @@ -4,6 +4,7 @@

import de.rheinfabrik.heimdall.OAuth2AccessToken;
import rx.Observable;
import rx.Single;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;

Expand Down Expand Up @@ -91,14 +92,15 @@ public final Observable<Uri> authorizationUri() {
// OAuth2AccessToken

@Override
public Observable<TAccessToken> grantNewAccessToken() {
public Single<TAccessToken> grantNewAccessToken() {
mAuthorizationUriSubject.onNext(buildAuthorizationUri());

return onUriLoadedCommand
.map(uri -> uri.getQueryParameter(RESPONSE_TYPE))
.filter(code -> code != null)
.take(1)
.retry()
.concatMap(this::exchangeTokenUsingCode);
.concatMap(this::exchangeTokenUsingCode)
.toSingle();
}
}
@@ -1,7 +1,7 @@
package de.rheinfabrik.heimdall.grants;

import de.rheinfabrik.heimdall.OAuth2AccessToken;
import rx.Observable;
import rx.Single;

/**
* Interface describing an OAuth2 Grant as described in https://tools.ietf.org/html/rfc6749#page-23.
Expand All @@ -17,5 +17,5 @@ public interface OAuth2Grant<TAccessToken extends OAuth2AccessToken> {
*
* @return - An Observable emitting the granted access token.
*/
Observable<TAccessToken> grantNewAccessToken();
Single<TAccessToken> grantNewAccessToken();
}
19 changes: 11 additions & 8 deletions sample/build.gradle
Expand Up @@ -12,13 +12,13 @@ buildscript {
}

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "de.rheinfabrik.heimdall"
minSdkVersion 15
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand All @@ -33,18 +33,21 @@ android {
dependencies {

// Android
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'

// Heimdall
compile project(':library')

// Rx
compile 'io.reactivex:rxandroid-framework:0.24.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'com.trello:rxlifecycle:0.4.0'
compile 'com.trello:rxlifecycle-components:0.4.0'

// Serialization
compile 'com.google.code.gson:gson:2.3'
compile 'com.google.code.gson:gson:2.4'
compile 'org.parceler:parceler-api:0.2.16'

// Network
Expand Down

0 comments on commit f6a92f2

Please sign in to comment.