content: /*
   * Copyright (C) 2019 Stichting Akvo (Akvo Foundation)
   *
   * This file is part of Akvo FLOW.
   *
   * Akvo FLOW is free software: you can redistribute it and modify it under the terms of
   * the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
   * either version 3 of the License or any later version.
   *
   * Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU Affero General Public License included below for more details.
   *
   * The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
   */
  
  package org.akvo.flow.rest.security;
  
  import java.util.Collection;
  import java.util.HashSet;
  
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.GrantedAuthority;
  import org.akvo.flow.rest.security.user.GaeUser;
  
  /**
   * Authentication object representing a fully-authenticated user.
   *
   * @author Luke Taylor
   */
  public class GaeUserAuthentication implements Authentication {
      private static final long serialVersionUID = 7819156935996796844L;
      private final GaeUser principal;
      private final Object details;
      private boolean authenticated;
      private Long userId;
  
      public GaeUserAuthentication(GaeUser principal, Object details) {
          this.principal = principal;
          this.details = details;
          this.userId = principal.getUserId();
          authenticated = true;
      }
  
      @Override
      public Collection<GrantedAuthority> getAuthorities() {
          return new HashSet<GrantedAuthority>(principal.getAuthorities());
      }
  
      @Override
      public Object getCredentials() {
          return userId;
      }
  
      @Override
      public Object getDetails() {
          return null;
      }
  
      @Override
      public Object getPrincipal() {
          return principal;
      }
  
      @Override
      public boolean isAuthenticated() {
          return authenticated;
      }
  
      @Override
      public void setAuthenticated(boolean isAuthenticated) {
          authenticated = isAuthenticated;
      }
  
      @Override
      public String getName() {
          return principal.getUserName();
      }
  
      @Override
      public String toString() {
          return "GaeUserAuthentication{" +
                  "principal=" + principal +
                  ", details=" + details +
                  ", authenticated=" + authenticated +
                  '}';
      }
  }
  