|
| 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 | +} |
0 commit comments