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.remoteauth.jwt;
015
016import java.util.Objects;
017
018import org.springframework.security.authentication.AbstractAuthenticationToken;
019
020/**
021 * JWT {@link org.springframework.security.core.Authentication} that contains a jwt token and
022 * information about the user.
023 */
024public class JwtAuthentication extends AbstractAuthenticationToken {
025
026  private String username;
027  private String token;
028
029  public JwtAuthentication(String token) {
030    super(null);
031    this.token = token;
032    super.setAuthenticated(false);
033  }
034
035  @Override
036  public Object getCredentials() {
037    return null;
038  }
039
040  @Override
041  public Object getPrincipal() {
042    return username;
043  }
044
045  public String getToken() {
046    return token;
047  }
048
049  @Override
050  public boolean equals(Object o) {
051    if (this == o) {
052      return true;
053    }
054    if (!(o instanceof JwtAuthentication)) {
055      return false;
056    }
057    if (!super.equals(o)) {
058      return false;
059    }
060    JwtAuthentication that = (JwtAuthentication) o;
061    return Objects.equals(username, that.username) && Objects.equals(token, that.token);
062  }
063
064  @Override
065  public int hashCode() {
066    return Objects.hash(super.hashCode(), username, token);
067  }
068}