001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.ws.security;
015
016import java.util.Collection;
017import java.util.Collections;
018
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021import org.springframework.security.core.GrantedAuthority;
022import org.springframework.security.core.userdetails.UserDetails;
023
024public class GbifAuthenticationToken implements GbifAuthentication {
025
026  private static final Logger LOG = LoggerFactory.getLogger(GbifAuthenticationToken.class);
027
028  private boolean authenticated = false;
029
030  /**
031   * User authorities (e.g. REGISTRY_ADMIN or APP).
032   */
033  private final Collection<? extends GrantedAuthority> authorities;
034
035  /**
036   * User information (can be either {@link GbifUserPrincipal} or {@link AppPrincipal}).
037   */
038  private final UserDetails principal;
039
040  /**
041   * Authentication scheme (e.g. 'GBIF').
042   */
043  private final String authenticationScheme;
044
045  /**
046   * JWT token in case it's used.
047   */
048  private String jwtToken;
049
050  public GbifAuthenticationToken(UserDetails principal) {
051    this.principal = principal;
052    this.authenticationScheme = "";
053    this.authorities = Collections.emptyList();
054    setAuthenticated(true);
055  }
056
057  public GbifAuthenticationToken(UserDetails principal, String authenticationScheme) {
058    this.principal = principal;
059    this.authenticationScheme = authenticationScheme;
060    this.authorities = Collections.emptyList();
061    setAuthenticated(true);
062  }
063
064  public GbifAuthenticationToken(
065      UserDetails principal, Collection<? extends GrantedAuthority> authorities) {
066    this.principal = principal;
067    this.authenticationScheme = "";
068    this.authorities = authorities;
069    setAuthenticated(true);
070  }
071
072  public GbifAuthenticationToken(
073      UserDetails principal,
074      String authenticationScheme,
075      Collection<? extends GrantedAuthority> authorities) {
076    this.principal = principal;
077    this.authenticationScheme = authenticationScheme;
078    this.authorities = authorities;
079    setAuthenticated(true);
080  }
081
082  public GbifAuthenticationToken(
083      UserDetails principal, Collection<? extends GrantedAuthority> authorities, String jwtToken) {
084    this.principal = principal;
085    this.authenticationScheme = "";
086    this.authorities = authorities;
087    setAuthenticated(true);
088    this.jwtToken = jwtToken;
089  }
090
091  public static GbifAuthentication anonymous() {
092    return new GbifAuthenticationToken(new AnonymousUserPrincipal());
093  }
094
095  @Override
096  public String getAuthenticationScheme() {
097    return authenticationScheme;
098  }
099
100  @Override
101  public Collection<? extends GrantedAuthority> getAuthorities() {
102    return authorities;
103  }
104
105  @Override
106  public Object getCredentials() {
107    LOG.warn("GbifAuthenticationToken#getCredentials is not used");
108    return null;
109  }
110
111  @Override
112  public Object getDetails() {
113    LOG.warn("GbifAuthenticationToken#getDetails is not used");
114    return null;
115  }
116
117  @Override
118  public UserDetails getPrincipal() {
119    return principal;
120  }
121
122  @Override
123  public boolean isAuthenticated() {
124    return authenticated;
125  }
126
127  @Override
128  public void setAuthenticated(boolean isAuthenticated) {
129    this.authenticated = isAuthenticated;
130  }
131
132  @Override
133  public String getName() {
134    return principal.getUsername();
135  }
136
137  public String getJwtToken() {
138    return jwtToken;
139  }
140}