/** * Return a permanent user access token, with ReadOnly permission. * * @param profile a users profile * @param message a message * @return A token granting read-only access to resources. * @throws ObjectStoreException oops */ public String generateReadOnlyAccessToken(Profile profile, String message) throws ObjectStoreException { UserProfile up; if (profile.getUserId() == null) { throw new IllegalArgumentException("This profile does not have an associated " + "user-profile"); } up = (UserProfile) uosw.getObjectById(profile.getUserId()); PermanentToken token = new PermanentToken(); UUID uuid = UUID.randomUUID(); token.setToken(uuid.toString()); token.setLevel("RO"); token.setUserProfile(up); token.setDateCreated(new Date()); if (message != null) { token.setMessage(message); } uosw.store(token); permanentTokens.put(uuid, token); return token.getToken(); }
@Override public boolean equals(Object o) { return (o instanceof PermanentToken && id != null) ? id.equals(((PermanentToken)o).getId()) : this == o; } @Override public int hashCode() { return (id != null) ? id.hashCode() : super.hashCode(); }
/** * @param token permanent user token * @param classKeys class keys * @return permission */ public ApiPermission getPermission(PermanentToken token, Map<String, List<FieldDescriptor>> classKeys) { if (token.getUserProfile() == null) { // Remove it, as it is clearly invalid. removePermanentToken(token); throw new IllegalStateException("All permanent tokens should have users"); } Profile profile = getProfile(token.getUserProfile().getUsername(), classKeys); if (profile == null) { removePermanentToken(token); throw new AuthenticationException("This token is not a valid access key: " + token); } ApiPermission.Level level; try { level = ApiPermission.Level.valueOf(token.getLevel()); } catch (IllegalArgumentException e) { String badLevel = token.getLevel(); removePermanentToken(token); throw new IllegalStateException("Token has illegal level: " + badLevel); } return new ApiPermission(profile, level); }
/** * @param token permanent user token */ public void removePermanentToken(PermanentToken token) { try { permanentTokens.remove(UUID.fromString(token.getToken())); } catch (Exception e) { // Ignore. } try { uosw.delete(token); } catch (ObjectStoreException e) { throw new RuntimeException("Error removing permanent token", e); } }
/** * @param token permanent user token * @param classKeys class keys * @return permission */ public ApiPermission getPermission(PermanentToken token, Map<String, List<FieldDescriptor>> classKeys) { if (token.getUserProfile() == null) { // Remove it, as it is clearly invalid. removePermanentToken(token); throw new IllegalStateException("All permanent tokens should have users"); } Profile profile = getProfile(token.getUserProfile().getUsername(), classKeys); if (profile == null) { removePermanentToken(token); throw new AuthenticationException("This token is not a valid access key: " + token); } ApiPermission.Level level; try { level = ApiPermission.Level.valueOf(token.getLevel()); } catch (IllegalArgumentException e) { String badLevel = token.getLevel(); removePermanentToken(token); throw new IllegalStateException("Token has illegal level: " + badLevel); } return new ApiPermission(profile, level); }
/** * @param token permanent user token */ public void removePermanentToken(PermanentToken token) { try { permanentTokens.remove(UUID.fromString(token.getToken())); } catch (Exception e) { // Ignore. } try { uosw.delete(token); } catch (ObjectStoreException e) { throw new RuntimeException("Error removing permanent token", e); } }
/** * Return a permanent user access token, with ReadOnly permission. * * @param profile a users profile * @param message a message * @return A token granting read-only access to resources. * @throws ObjectStoreException oops */ public String generateReadOnlyAccessToken(Profile profile, String message) throws ObjectStoreException { UserProfile up; if (profile.getUserId() == null) { throw new IllegalArgumentException("This profile does not have an associated " + "user-profile"); } up = (UserProfile) uosw.getObjectById(profile.getUserId()); PermanentToken token = new PermanentToken(); UUID uuid = UUID.randomUUID(); token.setToken(uuid.toString()); token.setLevel("RO"); token.setUserProfile(up); token.setDateCreated(new Date()); if (message != null) { token.setMessage(message); } uosw.store(token); permanentTokens.put(uuid, token); return token.getToken(); }
private Map<UUID, PermanentToken> loadPermanentTokens() { Map<UUID, PermanentToken> map = new HashMap<UUID, PermanentToken>(); try { // Get the current set of permanent tokens from the database. Query q = new Query(); QueryClass tokens = new QueryClass(PermanentToken.class); q.addFrom(tokens); q.addToSelect(tokens); List<?> results = uosw.executeSingleton(q); Set<PermanentToken> badTokens = new HashSet<PermanentToken>(); for (Object o: results) { PermanentToken token = (PermanentToken) o; try { UUID key = UUID.fromString(token.getToken()); map.put(key, token); } catch (IllegalArgumentException e) { badTokens.add(token); } } for (PermanentToken t: badTokens) { LOG.info("Removing bad token: " + t); uosw.delete(t); } } catch (Exception e) { LOG.error("Could not load permanent tokens", e); throw new IllegalStateException("Error loading permanent tokens", e); } return map; }
@Override public boolean equals(Object o) { return (o instanceof PermanentToken && id != null) ? id.equals(((PermanentToken)o).getId()) : this == o; } @Override public int hashCode() { return (id != null) ? id.hashCode() : super.hashCode(); }
private Map<UUID, PermanentToken> loadPermanentTokens() { Map<UUID, PermanentToken> map = new HashMap<UUID, PermanentToken>(); try { // Get the current set of permanent tokens from the database. Query q = new Query(); QueryClass tokens = new QueryClass(PermanentToken.class); q.addFrom(tokens); q.addToSelect(tokens); List<?> results = uosw.executeSingleton(q); Set<PermanentToken> badTokens = new HashSet<PermanentToken>(); for (Object o: results) { PermanentToken token = (PermanentToken) o; try { UUID key = UUID.fromString(token.getToken()); map.put(key, token); } catch (IllegalArgumentException e) { badTokens.add(token); } } for (PermanentToken t: badTokens) { LOG.info("Removing bad token: " + t); uosw.delete(t); } } catch (Exception e) { LOG.error("Could not load permanent tokens", e); throw new IllegalStateException("Error loading permanent tokens", e); } return map; }