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 org.gbif.api.model.common.GbifUser;
017
018import java.util.Collection;
019import java.util.Objects;
020import java.util.stream.Collectors;
021
022import javax.validation.constraints.NotNull;
023
024import org.springframework.security.core.GrantedAuthority;
025import org.springframework.security.core.authority.SimpleGrantedAuthority;
026import org.springframework.security.core.userdetails.UserDetails;
027
028/**
029 * @see org.gbif.api.model.common.GbifUserPrincipal
030 */
031public class GbifUserPrincipal implements UserDetails {
032
033  @NotNull private final GbifUser user;
034
035  public GbifUserPrincipal(GbifUser user) {
036    Objects.requireNonNull(user, "user shall be provided");
037    this.user = user;
038  }
039
040  @Override
041  public Collection<? extends GrantedAuthority> getAuthorities() {
042    return user.getRoles().stream()
043        .map(p -> new SimpleGrantedAuthority(p.toString()))
044        .collect(Collectors.toList());
045  }
046
047  @Override
048  public String getPassword() {
049    return user.getPasswordHash();
050  }
051
052  @Override
053  public String getUsername() {
054    return user.getUserName();
055  }
056
057  @Override
058  public boolean isAccountNonExpired() {
059    return true;
060  }
061
062  @Override
063  public boolean isAccountNonLocked() {
064    return true;
065  }
066
067  @Override
068  public boolean isCredentialsNonExpired() {
069    return true;
070  }
071
072  @Override
073  public boolean isEnabled() {
074    return true;
075  }
076}