store extra data in user sessions
All checks were successful
Deploy API / deploy (ubuntu-latest, 2.44.0) (push) Successful in 42s

This commit is contained in:
Braydon 2024-09-19 04:12:07 -04:00
parent dfbc705feb
commit 07dbc1fca8
7 changed files with 67 additions and 19 deletions

@ -1,7 +1,7 @@
package cc.pulseapp.api.config;
import cc.pulseapp.api.model.IGenericResponse;
import cc.pulseapp.api.model.user.Session;
import cc.pulseapp.api.model.user.session.Session;
import cc.pulseapp.api.repository.SessionRepository;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;

@ -16,8 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Iterator;
/**
* This controller is responsible for handling
* {@link User} authentication requests.
@ -60,11 +58,6 @@ public final class AuthController {
*/
@PostMapping("/login") @ResponseBody @NonNull
public ResponseEntity<UserAuthResponse> login(@NonNull HttpServletRequest request, UserLoginInput input) throws BadRequestException {
Iterator<String> iterator = request.getHeaderNames().asIterator();
while (iterator.hasNext()) {
String name = iterator.next();
System.out.println("header: " + name + " = " + request.getHeader(name));
}
return ResponseEntity.ok(authService.loginUser(request, input));
}
}

@ -1,6 +1,6 @@
package cc.pulseapp.api.model.user.response;
import cc.pulseapp.api.model.user.Session;
import cc.pulseapp.api.model.user.session.Session;
import cc.pulseapp.api.model.user.UserDTO;
import lombok.AllArgsConstructor;
import lombok.Getter;

@ -1,5 +1,6 @@
package cc.pulseapp.api.model.user;
package cc.pulseapp.api.model.user.session;
import cc.pulseapp.api.model.user.User;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -37,14 +38,9 @@ public final class Session {
@Indexed @NonNull private final String refreshToken;
/**
* The IP address of the user that created this session.
* The location this session originated from.
*/
@NonNull @JsonIgnore private final String ipAddress;
/**
* The user agent of the user that created this session.
*/
@NonNull @JsonIgnore private final String userAgent;
@NonNull @JsonIgnore private final SessionLocation location;
/**
* The unix timestamp of when this token expires.

@ -0,0 +1,57 @@
package cc.pulseapp.api.model.user.session;
import cc.pulseapp.api.common.RequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.*;
/**
* The location a {@link Session} originated from.
*
* @author Braydon
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE) @Getter @ToString
public final class SessionLocation {
/**
* The IP address that created the session.
*/
@NonNull private final String ip;
/**
* The country of the person that
* created the session, if known.
*/
private final String country;
/**
* The region of the person that
* created the session, if known.
*/
private final String region;
/**
* The city of the person that
* created the session, if known.
*/
private final String city;
/**
* The user agent of the person
* that created the session.
*/
@NonNull private final String userAgent;
/**
* Build a location from the given request.
*
* @param request the request to build from
* @return the session location
*/
@NonNull
public static SessionLocation buildFromRequest(@NonNull HttpServletRequest request) {
return new SessionLocation(
RequestUtils.getRealIp(request), request.getHeader("CF-IPCountry"),
request.getHeader("CF-Region"), request.getHeader("CF-IPCity"),
RequestUtils.getUserAgent(request)
);
}
}

@ -1,6 +1,6 @@
package cc.pulseapp.api.repository;
import cc.pulseapp.api.model.user.Session;
import cc.pulseapp.api.model.user.session.Session;
import lombok.NonNull;
import org.springframework.data.repository.CrudRepository;

@ -11,6 +11,8 @@ import cc.pulseapp.api.model.user.*;
import cc.pulseapp.api.model.user.input.UserLoginInput;
import cc.pulseapp.api.model.user.input.UserRegistrationInput;
import cc.pulseapp.api.model.user.response.UserAuthResponse;
import cc.pulseapp.api.model.user.session.Session;
import cc.pulseapp.api.model.user.session.SessionLocation;
import cc.pulseapp.api.repository.SessionRepository;
import cc.pulseapp.api.repository.UserRepository;
import jakarta.servlet.http.HttpServletRequest;
@ -166,7 +168,7 @@ public final class AuthService {
snowflakeService.generateSnowflake(), user.getSnowflake(),
StringUtils.generateRandom(128, true, true, false),
StringUtils.generateRandom(128, true, true, false),
RequestUtils.getRealIp(request), RequestUtils.getUserAgent(request),
SessionLocation.buildFromRequest(request),
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(30L)
));
}