|
| 1 | +/*- |
| 2 | + * Copyright 2010 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.authenticator.blackberry; |
| 18 | + |
| 19 | +import java.util.Hashtable; |
| 20 | +import java.util.Vector; |
| 21 | + |
| 22 | +import com.google.authenticator.blackberry.resource.AuthenticatorResource; |
| 23 | + |
| 24 | +import net.rim.device.api.i18n.ResourceBundle; |
| 25 | +import net.rim.device.api.system.CodeSigningKey; |
| 26 | +import net.rim.device.api.system.ControlledAccess; |
| 27 | +import net.rim.device.api.system.PersistentObject; |
| 28 | +import net.rim.device.api.system.PersistentStore; |
| 29 | + |
| 30 | +/** |
| 31 | + * BlackBerry port of {@code AccountDb}. |
| 32 | + */ |
| 33 | +public class AccountDb { |
| 34 | + private static final String TABLE_NAME = "accounts"; |
| 35 | + static final String ID_COLUMN = "_id"; |
| 36 | + static final String EMAIL_COLUMN = "email"; |
| 37 | + static final String SECRET_COLUMN = "secret"; |
| 38 | + static final String COUNTER_COLUMN = "counter"; |
| 39 | + static final String TYPE_COLUMN = "type"; |
| 40 | + static final Integer TYPE_LEGACY_TOTP = new Integer(-1); // TODO: remove April 2010 |
| 41 | + |
| 42 | + private static final long PERSISTENT_STORE_KEY = 0x9f1343901e600bf7L; |
| 43 | + |
| 44 | + static Hashtable sPreferences; |
| 45 | + |
| 46 | + static PersistentObject sPersistentObject; |
| 47 | + |
| 48 | + /** |
| 49 | + * Types of secret keys. |
| 50 | + */ |
| 51 | + public static class OtpType { |
| 52 | + private static ResourceBundle sResources = ResourceBundle.getBundle( |
| 53 | + AuthenticatorResource.BUNDLE_ID, AuthenticatorResource.BUNDLE_NAME); |
| 54 | + |
| 55 | + public static final OtpType TOTP = new OtpType(0); // time based |
| 56 | + public static final OtpType HOTP = new OtpType(1); // counter based |
| 57 | + |
| 58 | + private static final OtpType[] values = { TOTP, HOTP }; |
| 59 | + |
| 60 | + public final Integer value; // value as stored in database |
| 61 | + |
| 62 | + OtpType(int value) { |
| 63 | + this.value = new Integer(value); |
| 64 | + } |
| 65 | + |
| 66 | + public static OtpType getEnum(Integer i) { |
| 67 | + for (int index = 0; index < values.length; index++) { |
| 68 | + OtpType type = values[index]; |
| 69 | + if (type.value.intValue() == i.intValue()) { |
| 70 | + return type; |
| 71 | + } |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + public static OtpType[] values() { |
| 77 | + return values; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * {@inheritDoc} |
| 82 | + */ |
| 83 | + public String toString() { |
| 84 | + if (this == TOTP) { |
| 85 | + return sResources.getString(AuthenticatorResource.TOTP); |
| 86 | + } else if (this == HOTP) { |
| 87 | + return sResources.getString(AuthenticatorResource.HOTP); |
| 88 | + } else { |
| 89 | + return super.toString(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private AccountDb() { |
| 95 | + // Don't new me |
| 96 | + } |
| 97 | + |
| 98 | + static { |
| 99 | + sPersistentObject = PersistentStore |
| 100 | + .getPersistentObject(PERSISTENT_STORE_KEY); |
| 101 | + if (sPersistentObject.getContents() == null) { |
| 102 | + sPreferences = new Hashtable(10); |
| 103 | + // Use an instance of a class owned by this application |
| 104 | + // to easily get the appropriate CodeSigningKey: |
| 105 | + Object appObject = new FieldUtils(); |
| 106 | + CodeSigningKey codeSigningKey = CodeSigningKey.get(appObject); |
| 107 | + Object contents = new ControlledAccess(sPreferences, codeSigningKey); |
| 108 | + sPersistentObject.setContents(contents); |
| 109 | + sPersistentObject.commit(); |
| 110 | + } |
| 111 | + sPreferences = (Hashtable) sPersistentObject.getContents(); |
| 112 | + } |
| 113 | + |
| 114 | + private static Vector getAccounts() { |
| 115 | + Vector accounts = (Vector) sPreferences.get(TABLE_NAME); |
| 116 | + if (accounts == null) { |
| 117 | + accounts = new Vector(10); |
| 118 | + sPreferences.put(TABLE_NAME, accounts); |
| 119 | + sPersistentObject.commit(); |
| 120 | + } |
| 121 | + return accounts; |
| 122 | + } |
| 123 | + |
| 124 | + private static Hashtable getAccount(String email) { |
| 125 | + if (email == null) { |
| 126 | + throw new NullPointerException(); |
| 127 | + } |
| 128 | + Vector accounts = getAccounts(); |
| 129 | + for (int i = 0, n = accounts.size(); i < n; i++) { |
| 130 | + Hashtable account = (Hashtable) accounts.elementAt(i); |
| 131 | + if (email.equals(account.get(EMAIL_COLUMN))) { |
| 132 | + return account; |
| 133 | + } |
| 134 | + } |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + static String[] getNames() { |
| 139 | + Vector accounts = getAccounts(); |
| 140 | + int size = accounts.size(); |
| 141 | + String[] names = new String[size]; |
| 142 | + for (int i = 0; i < size; i++) { |
| 143 | + Hashtable account = (Hashtable) accounts.elementAt(i); |
| 144 | + names[i] = (String) account.get(EMAIL_COLUMN); |
| 145 | + } |
| 146 | + return names; |
| 147 | + } |
| 148 | + |
| 149 | + static boolean nameExists(String email) { |
| 150 | + Hashtable account = getAccount(email); |
| 151 | + return account != null; |
| 152 | + } |
| 153 | + |
| 154 | + static String getSecret(String email) { |
| 155 | + Hashtable account = getAccount(email); |
| 156 | + return account != null ? (String) account.get(SECRET_COLUMN) : null; |
| 157 | + } |
| 158 | + |
| 159 | + static Integer getCounter(String email) { |
| 160 | + Hashtable account = getAccount(email); |
| 161 | + return account != null ? (Integer) account.get(COUNTER_COLUMN) : null; |
| 162 | + } |
| 163 | + |
| 164 | + static void incrementCounter(String email) { |
| 165 | + Hashtable account = getAccount(email); |
| 166 | + if (account != null) { |
| 167 | + Integer counter = (Integer) account.get(COUNTER_COLUMN); |
| 168 | + counter = new Integer(counter.intValue() + 1); |
| 169 | + account.put(COUNTER_COLUMN, counter); |
| 170 | + sPersistentObject.commit(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + static OtpType getType(String user) { |
| 175 | + Hashtable account = getAccount(user); |
| 176 | + if (account != null) { |
| 177 | + Integer value = (Integer) account.get(TYPE_COLUMN); |
| 178 | + return OtpType.getEnum(value); |
| 179 | + } else { |
| 180 | + return null; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + static void delete(String email) { |
| 185 | + Vector accounts = getAccounts(); |
| 186 | + boolean modified = false; |
| 187 | + for (int index = 0; index < accounts.size();) { |
| 188 | + Hashtable account = (Hashtable) accounts.elementAt(index); |
| 189 | + if (email.equals(account.get(EMAIL_COLUMN))) { |
| 190 | + accounts.removeElementAt(index); |
| 191 | + modified = true; |
| 192 | + } else { |
| 193 | + index++; |
| 194 | + } |
| 195 | + } |
| 196 | + if (modified) { |
| 197 | + sPersistentObject.commit(); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Save key to database, creating a new user entry if necessary. |
| 203 | + * @param email the user email address. When editing, the new user email. |
| 204 | + * @param secret the secret key. |
| 205 | + * @param oldEmail If editing, the original user email, otherwise null. |
| 206 | + */ |
| 207 | + static void update(String email, String secret, String oldEmail, |
| 208 | + AccountDb.OtpType type) { |
| 209 | + Hashtable account = oldEmail != null ? getAccount(oldEmail) : null; |
| 210 | + if (account == null) { |
| 211 | + account = new Hashtable(10); |
| 212 | + Vector accounts = getAccounts(); |
| 213 | + accounts.addElement(account); |
| 214 | + } |
| 215 | + account.put(EMAIL_COLUMN, email); |
| 216 | + account.put(SECRET_COLUMN, secret); |
| 217 | + account.put(TYPE_COLUMN, type.value); |
| 218 | + if (!account.containsKey(COUNTER_COLUMN)) { |
| 219 | + account.put(COUNTER_COLUMN, new Integer(0)); |
| 220 | + } |
| 221 | + sPersistentObject.commit(); |
| 222 | + } |
| 223 | +} |
0 commit comments