Last active
December 16, 2019 22:08
-
-
Save ramesh-lingappan/46804f9dff0ff78ee0f6fc31cb5005ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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