package org.gbif.ws.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.Collections;
import java.util.UUID;

/**
 * Class providing temporary authorization for legacy web service requests (GBRDS/IPT).
 */
public class LegacyRequestAuthorization implements Authentication {

  private static final Logger LOG = LoggerFactory.getLogger(LegacyRequestAuthorization.class);

  private boolean authenticated = false;
  private final UUID userKey;
  private final UUID organizationKey;

  public LegacyRequestAuthorization(UUID userKey, UUID organizationKey) {
    this.userKey = userKey;
    this.organizationKey = organizationKey;
    setAuthenticated(true);
  }

  public UUID getUserKey() {
    return userKey;
  }

  public UUID getOrganizationKey() {
    return organizationKey;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return Collections.emptyList();
  }

  @Override
  public Object getCredentials() {
    LOG.warn("LegacyRequestAuthorization#getCredentials is not used");
    return null;
  }

  @Override
  public Object getDetails() {
    LOG.warn("LegacyRequestAuthorization#getDetails is not used");
    return null;
  }

  @Override
  public Object getPrincipal() {
    return new BasicUserPrincipal(userKey.toString());
  }

  @Override
  public boolean isAuthenticated() {
    return authenticated;
  }

  @Override
  public void setAuthenticated(boolean isAuthenticated) {
    this.authenticated = isAuthenticated;
  }

  @Override
  public String getName() {
    return userKey.toString();
  }
}

@@ -20,24 +20,146 @@
 .security;%0A%0A
+import java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.UUID;%0Aimport org.gbif.api.vocabulary.AppRole;%0A
 import org.s
@@ -171,16 +171,16 @@
 Logger;%0A
-
 import o
@@ -323,89 +323,81 @@
 ty;%0A
-%0A
 import 
-java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.UUID
+org.springframework.security.core.authority.SimpleGrantedAuthority
 ;%0A%0A/
@@ -764,16 +764,74 @@
 tionKey;
+%0A  private final Collection%3CGrantedAuthority%3E authorities;
 %0A%0A  publ
@@ -966,24 +966,118 @@
 izationKey;%0A
+    this.authorities = Collections.singleton(new SimpleGrantedAuthority(AppRole.IPT.name()));%0A
     setAuthe
@@ -1315,31 +1315,19 @@
 urn 
-Collections.emptyList()
+authorities
 ;%0A  

Add authorities to LegacyRequestAuthorization