Skip to content

Commit

Permalink
Merge pull request #48 from wching/kotlin_migration
Browse files Browse the repository at this point in the history
Kotlin and JUnit Migration
  • Loading branch information
wching committed Jul 25, 2019
2 parents 8e37578 + 920e34b commit d4bccc3
Show file tree
Hide file tree
Showing 39 changed files with 849 additions and 917 deletions.
28 changes: 26 additions & 2 deletions library/build.gradle
@@ -1,10 +1,17 @@
apply plugin: 'kotlin'
apply plugin: 'groovy'
apply plugin: 'maven'

buildscript {
ext.kotlin_version = '1.3.41'

repositories {
mavenCentral()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

jar.archiveName = "Heimdall.Droid.jar"
Expand All @@ -17,17 +24,34 @@ repositories {
}

dependencies {
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

// Rx
implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'

// GSON
implementation 'com.google.code.gson:gson:2.8.5'

// Testing
testImplementation "cglib:cglib:2.2"
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.27.0'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"

testImplementation "cglib:cglib:2.2"
testImplementation('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'junit'
}
}

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

This file was deleted.

@@ -0,0 +1,76 @@
package de.rheinfabrik.heimdall2

import com.google.gson.annotations.SerializedName
import java.io.Serializable
import java.util.Calendar

class OAuth2AccessToken(
/**
* REQUIRED
* The type of the token issued as described in https://tools.ietf.org/html/rfc6749#section-7.1.
* Value is case insensitive.
*/
@SerializedName("token_type")
var tokenType: String? = null,

/**
* REQUIRED
* The access token issued by the authorization server.
*/
@SerializedName("access_token")
var accessToken: String? = null,

/**
* OPTIONAL
* The refresh token, which can be used to obtain new
* access tokens using the same authorization grant as described
* in https://tools.ietf.org/html/rfc6749#section-6.
*/
@SerializedName("refresh_token")
var refreshToken: String? = null,

/**
* RECOMMENDED
* The lifetime in seconds of the access token. For
* example, the value "3600" denotes that the access token will
* expire in one hour from the time the response was generated.
* If omitted, the authorization server SHOULD provide the
* expiration time via other means or document the default value.
*/
@SerializedName("expires_in")
var expiresIn: Int? = null,

/**
* The expiration date used by Heimdall.
*/
@SerializedName("heimdall_expiration_date")
var expirationDate: Calendar? = null
) : Serializable {

// Public API

/**
* Returns whether the access token expired or not.
*
* @return True if expired. Otherwise false.
*/
fun isExpired(): Boolean =
expirationDate != null &&
Calendar.getInstance().after(expirationDate)


override fun equals(other: Any?): Boolean =
when {
this === other -> true
other !is OAuth2AccessToken -> false
else -> {
accessToken.equals(other.accessToken) && tokenType.equals(other.accessToken)
}
}


override fun hashCode(): Int =
tokenType.hashCode().let {
31 * it + accessToken.hashCode()
}
}

This file was deleted.

@@ -0,0 +1,60 @@
package de.rheinfabrik.heimdall2

import de.rheinfabrik.heimdall2.grants.OAuth2Grant
import de.rheinfabrik.heimdall2.grants.OAuth2RefreshAccessTokenGrant
import io.reactivex.Single
import java.util.Calendar

open class OAuth2AccessTokenManager(
private val mStorage: OAuth2AccessTokenStorage
) {

// Public API

/**
* Returns the underlying storage.
*
* @return - An OAuth2AccessTokenStorage.
*/
fun getStorage(): OAuth2AccessTokenStorage = mStorage

/**
* Grants a new access token using the given OAuth2 grant.
*
* @param grant A class implementing the OAuth2Grant interface.
* @return - An Single emitting the granted access token.
*/
fun grantNewAccessToken(
grant: OAuth2Grant,
calendar: Calendar = Calendar.getInstance()
): Single<OAuth2AccessToken> =
grant.grantNewAccessToken()
.doOnSuccess { token ->
token.expiresIn?.let {
val newExpirationDate = (calendar.clone() as Calendar).apply {
add(Calendar.SECOND, it)
}
token.expirationDate = newExpirationDate
}
mStorage.storeAccessToken(token)
}.cache()

/**
* Returns an Observable emitting an unexpired access token.
* NOTE: In order to work, Heimdall needs an access token which has a refresh_token and an
* expires_in field.
*
* @param refreshAccessTokenGrant The refresh grant that will be used if the access token is expired.
* @return - An Single emitting an unexpired access token.
*/
fun getValidAccessToken(refreshAccessTokenGrant: OAuth2RefreshAccessTokenGrant): Single<OAuth2AccessToken> =
mStorage.getStoredAccessToken()
.flatMap { accessToken ->
if (accessToken.isExpired()) {
refreshAccessTokenGrant.refreshToken = accessToken.refreshToken
grantNewAccessToken(refreshAccessTokenGrant)
} else {
Single.just(accessToken)
}
}
}

0 comments on commit d4bccc3

Please sign in to comment.