Skip to content

Commit

Permalink
feat: P4ADEV-510 storingUsers (#35)
Browse files Browse the repository at this point in the history
* P4ADEV-510 storingUsers

* setting collection name as collections

* P4ADEV-510 storing users when login

* fix unit tests
  • Loading branch information
antonioT90 authored Jun 25, 2024
1 parent d50cd57 commit 09a4dda
Show file tree
Hide file tree
Showing 22 changed files with 537 additions and 16 deletions.
33 changes: 33 additions & 0 deletions src/main/java/it/gov/pagopa/payhub/auth/config/MongoConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package it.gov.pagopa.payhub.auth.config;

import it.gov.pagopa.payhub.auth.utils.Constants;
import lombok.Setter;
import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Configuration
Expand Down Expand Up @@ -43,5 +51,30 @@ public MongoClientSettingsBuilderCustomizer customizer(MongoDbCustomProperties m
connectionPool.maxConnecting(mongoDbCustomProperties.connectionPool.maxConnecting);
});
}

@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(Arrays.asList(
// LocalDateTime support
new LocalDateTimeWriteConverter(),
new LocalDateTimeReadConverter()
));
}

@WritingConverter
public static class LocalDateTimeWriteConverter implements Converter<LocalDateTime, Date> {
@Override
public Date convert(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(Constants.ZONEID).toInstant());
}
}

@ReadingConverter
public static class LocalDateTimeReadConverter implements Converter<Date, LocalDateTime> {
@Override
public LocalDateTime convert(Date date) {
return date.toInstant().atZone(Constants.ZONEID).toLocalDateTime();
}
}
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package it.gov.pagopa.payhub.auth.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoId;

import java.util.Set;

@Data
@Document
public class Operators {
@Document("operators")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@FieldNameConstants
public class Operator {

@MongoId
private String operatorId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package it.gov.pagopa.payhub.auth.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoId;

import java.time.LocalDateTime;

@Data
@Document
public class Users {
@Document("users")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@FieldNameConstants
public class User {

@MongoId
private String userId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.gov.pagopa.payhub.auth.repository;

import it.gov.pagopa.payhub.auth.model.Operator;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface OperatorsRepository extends MongoRepository<Operator, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.gov.pagopa.payhub.auth.repository;

import it.gov.pagopa.payhub.auth.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UsersRepository extends UsersRepositoryExt, MongoRepository<User, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.gov.pagopa.payhub.auth.repository;

import it.gov.pagopa.payhub.auth.model.User;

public interface UsersRepositoryExt {
User registerUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.gov.pagopa.payhub.auth.repository;

import it.gov.pagopa.payhub.auth.model.User;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.time.LocalDateTime;

public class UsersRepositoryExtImpl implements UsersRepositoryExt{

private final MongoTemplate mongoTemplate;

public UsersRepositoryExtImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

@Override
public User registerUser(User user) {
return mongoTemplate.findAndModify(
Query.query(Criteria.where(User.Fields.mappedExternalUserId).is(user.getMappedExternalUserId())),
new Update()
.setOnInsert(User.Fields.userCode, user.getUserCode())
.setOnInsert(User.Fields.iamIssuer, user.getIamIssuer())
.setOnInsert(User.Fields.tosAccepted, false)
.set(User.Fields.lastLogin, LocalDateTime.now()),
FindAndModifyOptions.options()
.returnNew(true)
.upsert(true),
User.class
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.auth0.jwt.interfaces.Claim;
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.model.generated.AccessToken;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

Expand All @@ -16,12 +17,19 @@ public class ExchangeTokenServiceImpl implements ExchangeTokenService{
private final AccessTokenBuilderService accessTokenBuilderService;
private final TokenStoreService tokenStoreService;
private final IDTokenClaims2UserInfoMapper idTokenClaimsMapper;
private final IamUserRegistrationService iamUserRegistrationService;

public ExchangeTokenServiceImpl(ValidateExternalTokenService validateExternalTokenService, AccessTokenBuilderService accessTokenBuilderService, TokenStoreService tokenStoreService, IDTokenClaims2UserInfoMapper idTokenClaimsMapper) {
public ExchangeTokenServiceImpl(
ValidateExternalTokenService validateExternalTokenService,
AccessTokenBuilderService accessTokenBuilderService,
TokenStoreService tokenStoreService,
IDTokenClaims2UserInfoMapper idTokenClaimsMapper,
IamUserRegistrationService iamUserRegistrationService) {
this.validateExternalTokenService = validateExternalTokenService;
this.accessTokenBuilderService = accessTokenBuilderService;
this.tokenStoreService = tokenStoreService;
this.idTokenClaimsMapper = idTokenClaimsMapper;
this.iamUserRegistrationService = iamUserRegistrationService;
}

@Override
Expand All @@ -30,7 +38,9 @@ public AccessToken postToken(String clientId, String grantType, String subjectTo
clientId, subjectTokenType, subjectIssuer, grantType, scope);
Map<String, Claim> claims = validateExternalTokenService.validate(clientId, grantType, subjectToken, subjectIssuer, subjectTokenType, scope);
AccessToken accessToken = accessTokenBuilderService.build();
tokenStoreService.save(accessToken.getAccessToken(), idTokenClaimsMapper.apply(claims));
UserInfo user = idTokenClaimsMapper.apply(claims);
iamUserRegistrationService.registerUser(user);
tokenStoreService.save(accessToken.getAccessToken(), user);
return accessToken;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.gov.pagopa.payhub.auth.service.exchange;

import it.gov.pagopa.payhub.auth.service.user.UserService;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class IamUserRegistrationService {

private final boolean organizationAccessMode;

private final UserService userService;

public IamUserRegistrationService(
@Value("${app.enable-access-organization-mode}") boolean organizationAccessMode,

UserService userService
) {
this.organizationAccessMode = organizationAccessMode;
this.userService = userService;
}

void registerUser(UserInfo userInfo){
userService.registerUser(userInfo.getUserId(), userInfo.getFiscalCode(), userInfo.getIssuer());

if(organizationAccessMode){
//store Operators
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package it.gov.pagopa.payhub.auth.service.user;

import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.model.generated.UserInfo;

public interface UserService {
User registerUser(String externalUserId, String fiscalCode, String iamIssuer);
UserInfo getUserInfo(String accessToken);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package it.gov.pagopa.payhub.auth.service.user;

import it.gov.pagopa.payhub.auth.exception.custom.InvalidAccessTokenException;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.service.user.registration.UserRegistrationService;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{

private final TokenStoreService tokenStoreService;
public UserServiceImpl(TokenStoreService tokenStoreService) {
private final UserRegistrationService userRegistrationService;

public UserServiceImpl(TokenStoreService tokenStoreService, UserRegistrationService userRegistrationService) {
this.tokenStoreService = tokenStoreService;
this.userRegistrationService = userRegistrationService;
}

@Override
public User registerUser(String externalUserId, String fiscalCode, String iamIssuer) {
return userRegistrationService.registerUser(externalUserId, fiscalCode, iamIssuer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.payhub.auth.service.user.registration;

import org.springframework.stereotype.Service;

@Service
public class ExternalUserIdObfuscatorService {
public String obfuscate(String externalUserId){
return externalUserId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.payhub.auth.service.user.registration;

import org.springframework.stereotype.Service;

@Service
public class FiscalCodeObfuscatorService {
public String obfuscate(String fiscalCode){
return fiscalCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package it.gov.pagopa.payhub.auth.service.user.registration;

import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import org.springframework.stereotype.Service;

@Service
public class UserRegistrationService {

private final ExternalUserIdObfuscatorService externalUserIdObfuscatorService;
private final FiscalCodeObfuscatorService fiscalCodeObfuscatorService;
private final UsersRepository usersRepository;

public UserRegistrationService(ExternalUserIdObfuscatorService externalUserIdObfuscatorService, FiscalCodeObfuscatorService fiscalCodeObfuscatorService, UsersRepository usersRepository) {
this.externalUserIdObfuscatorService = externalUserIdObfuscatorService;
this.fiscalCodeObfuscatorService = fiscalCodeObfuscatorService;
this.usersRepository = usersRepository;
}

public User registerUser(String externalUserId, String fiscalCode, String iamIssuer){
User user = buildUser(externalUserId, fiscalCode, iamIssuer);
return usersRepository.registerUser(user);
}

private User buildUser(String externalUserId, String fiscalCode, String iamIssuer){
return User.builder()
.iamIssuer(iamIssuer)
.mappedExternalUserId(externalUserIdObfuscatorService.obfuscate(externalUserId))
.userCode(fiscalCodeObfuscatorService.obfuscate(fiscalCode))
.build();
}
}
9 changes: 9 additions & 0 deletions src/main/java/it/gov/pagopa/payhub/auth/utils/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.gov.pagopa.payhub.auth.utils;

import java.time.ZoneId;

public class Constants {
private Constants(){}

public static final ZoneId ZONEID = ZoneId.of("Europe/Rome");
}
Loading

0 comments on commit 09a4dda

Please sign in to comment.