@Test(expected = IllegalArgumentException.class) public void testServiceTokenNotTiedToUser() { AccessToken token = service.tokens.serviceIssue(); service.getUserId(token); }
@VisibleForTesting protected short getUserId(AccessToken token) { lock.readLock().lock(); try { ByteBuffer username = tokens.identify(token); User user = getUser(username); if(user != null) { return user.id(); } else { String hex = ByteBuffers.encodeAsHex(tokens.identify(token)); if(hex.equals(SERVICE_USERNAME_HEX)) { throw new IllegalArgumentException( "The specified token is associated with a service and not a user"); } else { throw new IllegalStateException( "An valid access token was presented for a user that does not exist"); } } } finally { lock.readLock().unlock(); } }
UserService { @VisibleForTesting protected short getUserId(AccessToken token) { lock.readLock().lock(); try { ByteBuffer username = tokens.identify(token); User user = getUser(username); if(user != null) { return user.id(); } else { String hex = ByteBuffers.encodeAsHex(tokens.identify(token)); if(hex.equals(SERVICE_USERNAME_HEX)) { throw new IllegalArgumentException( "The specified token is associated with a service and not a user"); } else { throw new IllegalStateException( "An valid access token was presented for a user that does not exist"); } } } finally { lock.readLock().unlock(); } } }
UserService { @VisibleForTesting protected short getUserId(AccessToken token) { lock.readLock().lock(); try { ByteBuffer username = tokens.identify(token); User user = getUser(username); if(user != null) { return user.id(); } else { String hex = ByteBuffers.encodeAsHex(tokens.identify(token)); if(hex.equals(SERVICE_USERNAME_HEX)) { throw new IllegalArgumentException( "The specified token is associated with a service and not a user"); } else { throw new IllegalStateException( "An valid access token was presented for a user that does not exist"); } } } finally { lock.readLock().unlock(); } } @SuppressWarnings("unchecked") private  UserService(String backingStore, int accessTokenTtl,
            TimeUnit accessTokenTtlUnit); }
UserService { @VisibleForTesting protected short getUserId(AccessToken token) { lock.readLock().lock(); try { ByteBuffer username = tokens.identify(token); User user = getUser(username); if(user != null) { return user.id(); } else { String hex = ByteBuffers.encodeAsHex(tokens.identify(token)); if(hex.equals(SERVICE_USERNAME_HEX)) { throw new IllegalArgumentException( "The specified token is associated with a service and not a user"); } else { throw new IllegalStateException( "An valid access token was presented for a user that does not exist"); } } } finally { lock.readLock().unlock(); } } @SuppressWarnings("unchecked") private  UserService(String backingStore, int accessTokenTtl,
            TimeUnit accessTokenTtlUnit); static UserService create(String backingStore); boolean authenticate(ByteBuffer username, ByteBuffer password); boolean can(ByteBuffer username, Permission permission,
            String environment); void create(ByteBuffer username, ByteBuffer password, Role role); void delete(ByteBuffer username); void disable(ByteBuffer username); void enable(ByteBuffer username); boolean exists(ByteBuffer username); void forEachUser(Consumer<ByteBuffer> consumer); Role getRole(ByteBuffer username); void grant(ByteBuffer username, Permission permission,
            String environment); boolean isEnabled(ByteBuffer username); void revoke(ByteBuffer username, String environment); void setPassword(ByteBuffer username, ByteBuffer password); void setRole(ByteBuffer username, Role role); }
UserService { @VisibleForTesting protected short getUserId(AccessToken token) { lock.readLock().lock(); try { ByteBuffer username = tokens.identify(token); User user = getUser(username); if(user != null) { return user.id(); } else { String hex = ByteBuffers.encodeAsHex(tokens.identify(token)); if(hex.equals(SERVICE_USERNAME_HEX)) { throw new IllegalArgumentException( "The specified token is associated with a service and not a user"); } else { throw new IllegalStateException( "An valid access token was presented for a user that does not exist"); } } } finally { lock.readLock().unlock(); } } @SuppressWarnings("unchecked") private  UserService(String backingStore, int accessTokenTtl,
            TimeUnit accessTokenTtlUnit); static UserService create(String backingStore); boolean authenticate(ByteBuffer username, ByteBuffer password); boolean can(ByteBuffer username, Permission permission,
            String environment); void create(ByteBuffer username, ByteBuffer password, Role role); void delete(ByteBuffer username); void disable(ByteBuffer username); void enable(ByteBuffer username); boolean exists(ByteBuffer username); void forEachUser(Consumer<ByteBuffer> consumer); Role getRole(ByteBuffer username); void grant(ByteBuffer username, Permission permission,
            String environment); boolean isEnabled(ByteBuffer username); void revoke(ByteBuffer username, String environment); void setPassword(ByteBuffer username, ByteBuffer password); void setRole(ByteBuffer username, Role role); final AccessTokenManager tokens; }