Skip to content

Commit e24a6dc

Browse files
authored
At/kotlin authenticator (andretietz#81)
* upgrading kotlin to 1.2.61 * Converting AccountAuthenticator into kotlin. * detekt fix * ktlint and test fix
1 parent 7a4a2dc commit e24a6dc

File tree

4 files changed

+113
-120
lines changed

4 files changed

+113
-120
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Release Settings
2-
kotlinVersion=1.2.30
2+
kotlinVersion=1.2.61
33
okhttpVersion=3.10.0
44
retrofitVersion=2.3.0
55

retroauth-android/src/main/java/com/andretietz/retroauth/AccountAuthenticator.java

-117
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2015 Andre Tietz
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.andretietz.retroauth
18+
19+
import android.accounts.AbstractAccountAuthenticator
20+
import android.accounts.Account
21+
import android.accounts.AccountAuthenticatorResponse
22+
import android.accounts.AccountManager
23+
import android.content.Context
24+
import android.content.Intent
25+
import android.os.Bundle
26+
27+
/**
28+
* This AccountAuthenticator is a very basic implementation of Android's
29+
* [AbstractAccountAuthenticator]. This implementation is intentional as empty as it is. Cause of this is, that
30+
* it's executed in a different process, which makes it difficult to provide login endpoints from
31+
* the app process in here.
32+
*
33+
* NOTE: This class cannot be replaced with a kotlin version yet, since Android cannot load Authenticators
34+
* that are non java once
35+
*/
36+
class AccountAuthenticator(
37+
context: Context,
38+
internal val action: String
39+
) : AbstractAccountAuthenticator(context) {
40+
41+
override fun addAccount(
42+
response: AccountAuthenticatorResponse,
43+
accountType: String,
44+
authTokenType: String?,
45+
requiredFeatures: Array<String>?,
46+
options: Bundle
47+
) = createAuthBundle(response, action, accountType, authTokenType, null)
48+
49+
override fun getAuthToken(
50+
response: AccountAuthenticatorResponse,
51+
account: Account,
52+
authTokenType: String,
53+
options: Bundle
54+
) = createAuthBundle(response, action, account.type, authTokenType, account.name)
55+
56+
/**
57+
* Creates an Intent to open the Activity to login.
58+
*
59+
* @param response needed parameter
60+
* @param accountType The account Type
61+
* @param tokenType The requested token type
62+
* @param accountName The name of the account
63+
* @return a bundle to open the activity
64+
*/
65+
private fun createAuthBundle(
66+
response: AccountAuthenticatorResponse,
67+
action: String,
68+
accountType: String,
69+
tokenType: String?,
70+
accountName: String?
71+
): Bundle? = Bundle().apply {
72+
putParcelable(
73+
AccountManager.KEY_INTENT,
74+
Intent(action).apply {
75+
putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
76+
putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType)
77+
putExtra(KEY_TOKEN_TYPE, tokenType)
78+
accountName?.let {
79+
putExtra(AccountManager.KEY_ACCOUNT_NAME, it)
80+
}
81+
})
82+
}
83+
84+
override fun confirmCredentials(
85+
response: AccountAuthenticatorResponse,
86+
account: Account,
87+
options: Bundle?
88+
) = null
89+
90+
override fun editProperties(response: AccountAuthenticatorResponse, accountType: String) = null
91+
92+
override fun getAuthTokenLabel(authTokenType: String) = null
93+
94+
override fun updateCredentials(
95+
response: AccountAuthenticatorResponse,
96+
account: Account,
97+
authTokenType: String,
98+
options: Bundle
99+
): Bundle? = null
100+
101+
override fun hasFeatures(
102+
response: AccountAuthenticatorResponse,
103+
account: Account,
104+
features: Array<String>
105+
) = null
106+
107+
companion object {
108+
const val KEY_TOKEN_TYPE = "account_token_type"
109+
}
110+
}

retroauth-android/src/test/java/com/andretietz/retroauth/AccountAuthenticatorTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AccountAuthenticatorTest {
4040
arrayOf(), mock(Bundle::class.java))
4141

4242
assertNotNull(bundle)
43-
val intent = bundle.getParcelable<Intent>(AccountManager.KEY_INTENT)
43+
val intent = requireNotNull(bundle).getParcelable<Intent>(AccountManager.KEY_INTENT)
4444
assertNotNull(intent)
4545

4646
assertEquals(
@@ -57,7 +57,7 @@ class AccountAuthenticatorTest {
5757
val bundle = authenticator.getAuthToken(response, account, "tokenType", mock(Bundle::class.java))
5858

5959
assertNotNull(bundle)
60-
val intent = bundle.getParcelable<Intent>(AccountManager.KEY_INTENT)
60+
val intent = requireNotNull(bundle).getParcelable<Intent>(AccountManager.KEY_INTENT)
6161
assertNotNull(intent)
6262

6363
assertEquals(

0 commit comments

Comments
 (0)