-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from wching/kotlin_migration
Kotlin and JUnit Migration
- Loading branch information
Showing
39 changed files
with
849 additions
and
917 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
90 changes: 0 additions & 90 deletions
90
library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessToken.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessToken.kt
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,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() | ||
} | ||
} |
107 changes: 0 additions & 107 deletions
107
library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessTokenManager.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessTokenManager.kt
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,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) | ||
} | ||
} | ||
} |
Oops, something went wrong.