Skip to content

Instantly share code, notes, and snippets.

@ramesh-lingappan
Last active December 16, 2019 22:08
Show Gist options
  • Save ramesh-lingappan/46804f9dff0ff78ee0f6fc31cb5005ca to your computer and use it in GitHub Desktop.
Save ramesh-lingappan/46804f9dff0ff78ee0f6fc31cb5005ca to your computer and use it in GitHub Desktop.
class ApiKey {
// id will be {prefix}.{hash_of_api_key}
private String id;
private String name;
private Set<String> scopes;
@JsonProperty("prefix")
public String getPrefix() {
if (id == null) return null;
String[] parts = id.split("\\.");
return parts.length == 2 ? parts[0] : null;
}
}
// sudo code
public void generateApiKey() {
String prefix = randomGenerator(length: 8);
String key = randomGenerator(length: 35);
// this will not be stored in database
String apiKey = prefix+ "." + key;
// datastore primary key can be like {prefix}.{hash_of_api_key},
// this way no seperate prefix property is required
String id = prefix + "."+ sha1Hash(apiKey)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment