em = $em; } public function getUserByAPIKey($api_key) { $user = $this->em->getRepository(User::class)->findOneBy(array('api_key' => $api_key)); return $user; } public function getUsernameForAPIKey($apiKey) { // Look up the username based on the token in the database, via // an API call, or do something entirely different $username = 'test'; return $username; } public function loadUserByUsername($username) { return new User( $username, null, // the roles for the user - you may choose to determine // these dynamically somehow based on the user array('ROLE_API') ); } public function refreshUser(UserInterface $user) { // this is used for storing authentication in the session // but in this example, the token is sent in each request, // so authentication can be stateless. Throwing this exception // is proper to make things stateless throw new UnsupportedUserException(); } public function supportsClass($class) { return User::class === $class; } }