MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / Secrets

Class Secrets

CodenameOne/src/com/codename1/security/Secrets.java:33–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31/// On the simulator or a local (non-cloud) build there is no build key, so
32/// [#get(String)] returns the cached value if any and otherwise `null`.
33public final class Secrets {
34
35 private static final String CACHE_PREFIX = "cn1secret.";
36 private static final String PROP_ENDPOINT = "secrets.cloud.endpoint";
37 private static final String PROP_BUILD_KEY = "build_key";
38 private static final String PROP_PACKAGE = "package_name";
39 private static final String DEFAULT_ENDPOINT = "https://cloud.codenameone.com/api/v2/secrets";
40
41 private Secrets() {
42 }
43
44 /// The secret named `name` -- from the keychain cache when present,
45 /// otherwise fetched from the cloud vault and cached. Returns `null` when
46 /// it is unavailable (undefined, not app-readable, or offline with no
47 /// cached copy). The first fetch blocks on the network, so call this off
48 /// the EDT.
49 public static String get(String name) {
50 String cached = SecureStorage.getInstance().get(CACHE_PREFIX + name);
51 if (cached != null) {
52 return cached;
53 }
54 return refresh(name);
55 }
56
57 /// As [#get(String)] but returns `defaultValue` when the secret is absent.
58 public static String get(String name, String defaultValue) {
59 String v = get(name);
60 return v == null ? defaultValue : v;
61 }
62
63 /// Force a fresh fetch from the cloud vault, replacing the keychain cache.
64 /// Returns the new value, or `null` when unavailable.
65 public static String refresh(String name) {
66 String v = fetch(name);
67 if (v != null) {
68 SecureStorage.getInstance().set(CACHE_PREFIX + name, v);
69 }
70 return v;
71 }
72
73 /// Drop the cached copy of `name` from the keychain; the next [#get(String)]
74 /// re-fetches it.
75 public static void clear(String name) {
76 SecureStorage.getInstance().remove(CACHE_PREFIX + name);
77 }
78
79 private static String fetch(String name) {
80 String key = Display.getInstance().getProperty(PROP_BUILD_KEY, "");
81 if (key == null || key.length() == 0) {
82 return null; // simulator / local build: no cloud secrets
83 }
84 String url = Display.getInstance().getProperty(PROP_ENDPOINT, DEFAULT_ENDPOINT);
85 if (url == null || url.length() == 0) {
86 return null;
87 }
88 try {
89 StringBuilder sb = new StringBuilder();
90 sb.append('{');

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected