• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-client-core / #174

pending completion
#174

Pull #921

github-actions

web-flow
Merge 66d3a69a9 into f9f6e7f97
Pull Request #921: Refactor user settings task

18309 of 26768 relevant lines covered (68.4%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.78
opensrp-core/src/main/java/org/smartregister/account/AccountHelper.java
1
package org.smartregister.account;
2

3
import android.accounts.Account;
4
import android.accounts.AccountManager;
5
import android.accounts.AccountManagerFuture;
6
import android.os.Bundle;
7

8
import org.smartregister.CoreLibrary;
9

10
import java.util.Calendar;
11

12
import timber.log.Timber;
13

14
/**
15
 * Created by ndegwamartin on 2020-04-27.
16
 */
17
public class AccountHelper {
×
18

19
    public final static int MAX_AUTH_RETRIES = 1;
20
    private static AccountManager accountManager = CoreLibrary.getInstance().getAccountManager();
1✔
21

22
    /**
23
     * Gets OAuth Account by the account name and account type
24
     *
25
     * @param accountName name of account within account manage
26
     * @param accountType unique name to identify our account type in the Account Manager
27
     * @return Account retrieved
28
     */
29
    public static Account getOauthAccountByNameAndType(String accountName, String accountType) {
30

31
        Account[] accounts = accountManager.getAccountsByType(accountType);
1✔
32
        return accounts.length > 0 ? selectAccount(accounts, accountName) : null;
1✔
33
    }
34

35
    /**
36
     * Get specified Account by name from the list returned by the account manager
37
     *
38
     * @param accountName name of account within account manage
39
     * @param accounts    list of Accounts returned by the Account Manager
40
     * @return Account selected from list
41
     */
42
    public static Account selectAccount(Account[] accounts, String accountName) {
43
        for (Account account : accounts) {
1✔
44
            if (accountName.equals(account.name)) {
1✔
45
                return account;
1✔
46
            }
47
        }
48

49
        return null;
×
50
    }
51

52
    /**
53
     * Gets user data value with the specified key from the Account Manager
54
     *
55
     * @param key         of the user data value we want to retrieve
56
     * @param accountType unique name to identify our account type in the Account Manager
57
     * @param accountName name of account within account manage
58
     * @return access token
59
     */
60
    public static String getAccountManagerValue(String key, String accountName, String accountType) {
61
        Account account = AccountHelper.getOauthAccountByNameAndType(accountName, accountType);
1✔
62
        if (account != null) {
1✔
63
            return accountManager.getUserData(account, key);
1✔
64
        }
65
        return null;
1✔
66
    }
67

68
    /**
69
     * Gets OAuth Token
70
     *
71
     * @param accountName   name of account within account manage
72
     * @param accountType   unique name to identify our account type in the Account Manager
73
     * @param authTokenType type of token requested from server e.g. PROVIDER, ADMIN
74
     * @return access token
75
     */
76
    public static String getOAuthToken(String accountName, String accountType, String authTokenType) {
77
        Account account = getOauthAccountByNameAndType(accountName, accountType);
1✔
78

79
        try {
80
            return accountManager.blockingGetAuthToken(account, authTokenType, true);
1✔
81
        } catch (Exception ex) {
×
82
            Timber.e(ex, "EXCEPTION: %s", ex.toString());
×
83
            return null;
×
84
        }
85
    }
86

87
    /**
88
     * This method invalidates the auth token so that the Authenticator can fetch a new one from server
89
     *
90
     * @param accountType unique name to identify our account type in the Account Manager
91
     * @param authToken   token to invalidate
92
     */
93
    public static void invalidateAuthToken(String accountType, String authToken) {
94
        if (authToken != null)
1✔
95
            accountManager.invalidateAuthToken(accountType, authToken);
1✔
96
    }
1✔
97

98
    /**
99
     * @param accountName   name of account within account manage
100
     * @param accountType   unique name to identify our account type in the Account Manager
101
     * @param authTokenType type of token requested from server e.g. PROVIDER, ADMIN
102
     * @return access token cached
103
     */
104
    public static String getCachedOAuthToken(String accountName, String accountType, String authTokenType) {
105
        Account account = getOauthAccountByNameAndType(accountName, accountType);
1✔
106
        return account != null ? accountManager.peekAuthToken(account, authTokenType) : null;
1✔
107
    }
108

109
    /**
110
     * Prompt the user to re-authenticate
111
     *
112
     * @param accountName   name of account within account manage
113
     * @param accountType   unique name to identify our account type in the Account Manager
114
     * @param authTokenType type of token requested from server e.g. PROVIDER, ADMIN
115
     * @return access token
116
     */
117
    public static AccountManagerFuture<Bundle> reAuthenticateUserAfterSessionExpired(String accountName, String accountType, String authTokenType) {
118
        Account account = getOauthAccountByNameAndType(accountName, accountType);
1✔
119
        return accountManager.updateCredentials(account, authTokenType, null, null, null, null);
1✔
120
    }
121

122
    /**
123
     * A Helper method to check if the Refresh Token is valid.
124
     * Note: We only need to manually enter credentials if the Refresh Token is expired
125
     */
126
    public static boolean isRefreshTokenValid(String accountName, String accountType) {
127
        String createdAt = getAccountManagerValue(INTENT_KEY.ACCOUNT_REFRESH_TOKEN_CREATED_AT, accountName, accountType);
×
128
        String accountExpires = getAccountManagerValue(INTENT_KEY.ACCOUNT_REFRESH_TOKEN_EXPIRES_IN, accountName, accountType);
×
129
        Long createdAtLong = createdAt != null ? Long.parseLong(createdAt) : null;
×
130
        Long accountExpiresLong = accountExpires != null ? Long.parseLong(accountExpires) : null;
×
131
        Long now = Calendar.getInstance().getTimeInMillis() / 1000;
×
132
        Long expiry = createdAtLong != null && accountExpiresLong != null ? createdAtLong + accountExpiresLong : 0l;
×
133
        return now < expiry;
×
134
    }
135

136
    public static final class CONFIGURATION_CONSTANTS {
×
137

138
        public static final String IS_KEYCLOAK_CONFIGURED = "is_keycloack_configured";
139
        public final static String TOKEN_ENDPOINT_URL = "token_endpoint_url";
140
        public final static String AUTHORIZATION_ENDPOINT_URL = "authorization_endpoint_url";
141
        public final static String ISSUER_ENDPOINT_URL = "issuer_endpoint_url";
142
        public static final String USERINFO_ENDPOINT_URL = "userinfo_endpoint_url";
143
    }
144

145
    public static final class OAUTH {
×
146

147
        public final static String ACCOUNT_CONFIGURATION_ENDPOINT = "/rest/config/keycloak";
148
        public final static String TOKEN_ENDPOINT = "/oauth/token";
149
        public final static String PASSWORD_RESET_ENDPOINT = "/account";
150

151
        public static final class GRANT_TYPE {
×
152
            public final static String PASSWORD = "password";
153
            public final static String REFRESH_TOKEN = "refresh_token";
154

155
        }
156
    }
157

158
    public static final class INTENT_KEY {
×
159

160
        public final static String ACCOUNT_TYPE = "ACCOUNT_TYPE";
161
        public final static String AUTH_TYPE = "AUTH_TYPE";
162
        public final static String ACCOUNT_NAME = "ACCOUNT_NAME";
163
        public final static String IS_NEW_ACCOUNT = "IS_NEW_ACCOUNT";
164
        public final static String ACCOUNT_REFRESH_TOKEN = "ACCOUNT_REFRESH_TOKEN";
165
        public final static String ACCOUNT_LOCAL_PASSWORD_SALT = "ACCOUNT_LOCAL_PASSWORD_SALT";
166
        public final static String ACCOUNT_LOCAL_PASSWORD = "ACCOUNT_LOCAL_PASSWORD";
167
        public final static String ACCOUNT_ROLES = "ACCOUNT_ROLES";
168
        public final static String ACCOUNT_REFRESH_TOKEN_EXPIRES_IN = "ACCOUNT_REFRESH_TOKEN_EXPIRES_IN";
169
        public final static String ACCOUNT_ACCESS_TOKEN_EXPIRES_IN = "ACCOUNT_ACCESS_TOKEN_EXPIRES_IN";
170
        public final static String ACCOUNT_REFRESH_TOKEN_CREATED_AT = "ACCOUNT_ACCESS_TOKEN_CREATED_AT";
171
    }
172

173
    public static final class TOKEN_TYPE {
×
174
        public final static String PROVIDER = "provider";
175
        public final static String ADMIN = "admin";
176
    }
177

178
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc