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; 015 016import org.gbif.api.model.common.GbifUser; 017import org.gbif.api.vocabulary.UserRole; 018 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.Map; 022import java.util.Optional; 023import java.util.Set; 024import java.util.stream.Collectors; 025 026import com.fasterxml.jackson.annotation.JsonInclude; 027import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 028import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; 029 030import lombok.Builder; 031import lombok.Data; 032 033/** Class top map responses from token-based (JWT) authentication services. */ 034@Data 035@Builder 036@JsonInclude(JsonInclude.Include.NON_NULL) 037@JsonDeserialize(builder = LoggedUser.LoggedUserBuilder.class) 038public class LoggedUser { 039 040 private Integer key; 041 private String userName; 042 private String firstName; 043 private String lastName; 044 private String email; 045 private String token; 046 047 @Builder.Default private Map<String, String> settings = new HashMap<>(); 048 049 @Builder.Default private Set<String> roles = new HashSet<>(); 050 051 public GbifUser toGbifUser() { 052 GbifUser gbifUser = new GbifUser(); 053 gbifUser.setUserName(userName); 054 gbifUser.setFirstName(firstName); 055 gbifUser.setLastName(lastName); 056 gbifUser.setEmail(email); 057 gbifUser.setSettings(settings); 058 gbifUser.setKey(key); 059 Optional.ofNullable(roles) 060 .map(r -> roles.stream().map(UserRole::valueOf).collect(Collectors.toSet())) 061 .ifPresent(gbifUser::setRoles); 062 return gbifUser; 063 } 064 065 @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") 066 public static class LoggedUserBuilder { 067 // Lombok will add the rest 068 } 069}