package com.konkerlabs.platform.registry.data.security;

import com.konkerlabs.platform.registry.business.repositories.DeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service("deviceDetails")
public class DeviceUserDetailsService implements UserDetailsService {

    @Autowired
    private DeviceRepository deviceRepository;

    @Override
    public UserDetails loadUserByUsername(String apiKey) throws UsernameNotFoundException {
        return Optional
                .ofNullable(deviceRepository.findByApiKey(apiKey))
                .orElseThrow(() -> new UsernameNotFoundException("authentication.credentials.invalid"));
    }
}

@@ -50,16 +50,79 @@
 urity;%0A%0A
+import com.konkerlabs.platform.registry.business.model.Device;%0A
 import c
@@ -193,16 +193,16 @@
 sitory;%0A
-
 import o
@@ -527,36 +527,8 @@
 e;%0A%0A
-import java.util.Optional;%0A%0A
 @Ser
@@ -801,52 +801,24 @@
     
-return Optional%0A                .ofNullable(
+Device device = 
 devi
@@ -850,17 +850,18 @@
 (apiKey)
-)
+;%0A
 %0A       
@@ -865,34 +865,68 @@
     
-        .orElseThrow(() -%3E
+if (device == null %7C%7C device.isActive()) %7B%0A            throw
  new
@@ -993,15 +993,49 @@
 id%22)
-)
 ;%0A
+    
     %7D%0A
+%0A        return device;%0A    %7D%0A%0A
 %7D%0A

check device active