AzureAdService.java

  1. package no.nav.data.common.security.azure;

  2. import com.microsoft.graph.models.odataerrors.ODataError;
  3. import com.microsoft.kiota.ApiException;
  4. import lombok.RequiredArgsConstructor;
  5. import lombok.extern.slf4j.Slf4j;
  6. import no.nav.data.common.exceptions.TechnicalException;
  7. import no.nav.data.common.exceptions.TimeoutException;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.util.StreamUtils;

  10. import java.io.IOException;
  11. import java.net.SocketTimeoutException;

  12. @Slf4j
  13. @Service
  14. @RequiredArgsConstructor
  15. public class AzureAdService {

  16.     private final AzureTokenProvider azureTokenProvider;

  17.     public byte[] lookupProfilePictureByNavIdent(String navIdent) {
  18.         String userId = lookupUserIdForNavIdent(navIdent);
  19.         return lookupUserProfilePicture(userId);
  20.     }

  21.     private String lookupUserIdForNavIdent(String navIdent) {
  22.         var res = azureTokenProvider.getGraphClient()
  23.                 .users()
  24.                 .get(requestConfiguration -> {
  25.                         requestConfiguration.queryParameters.select = new String[] {"id"};
  26.                         requestConfiguration.queryParameters.filter = "mailNickname eq '" + navIdent + "'";
  27.                 })
  28.                 .getValue();
  29.         if (res.size() != 1) {
  30.             log.info("Did not find single user for navIdent {} ({})", navIdent, res.size());
  31.             return null;
  32.         }
  33.         return res.get(0).getId();
  34.     }

  35.     private byte[] lookupUserProfilePicture(String id) {
  36.         try {
  37.             var photo = azureTokenProvider.getGraphClient()
  38.                     .users()
  39.                     .byUserId(id)
  40.                     .photo().content()
  41.                     .get();
  42.             return StreamUtils.copyToByteArray(photo);
  43.         } catch (ODataError e) {
  44.             if (e.getError() != null && "ImageNotFound".equals(e.getError().getCode())) {
  45.                 log.info("No profile picture found for user {}", id);
  46.                 throw new TechnicalException("No profile picture found for user " + id);
  47.             }
  48.             log.error("ODataError with azure: {}", e.getError() == null ? e.getMessage() : e.getError().getCode());
  49.             throw new TechnicalException("error with azure", e);
  50.         }catch (ApiException e) {
  51.             log.error("error with azure", e);
  52.             throw new TechnicalException("error with azure", e);
  53.         } catch (IOException e) {
  54.             if (e.getCause() instanceof SocketTimeoutException) {
  55.                 log.error("Azure request timed out", e);
  56.                 throw new TimeoutException("Azure request timed out", e);
  57.             }
  58.             log.error("io error with azure", e);
  59.             throw new TechnicalException("io error with azure", e);
  60.         }
  61.     }
  62. }