Skip to content

Commit

Permalink
Ajoute le user-agent à la session (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
niladic authored Oct 29, 2024
1 parent 8128021 commit b3ae953
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/actions/LoginAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import models.{Area, Authorization, Error, EventType, LoginToken, User, UserSess
import models.EventType.{AuthWithDifferentIp, ExpiredToken, ToCGURedirected, TryLoginByKey}
import modules.AppConfig
import play.api.Logger
import play.api.http.HeaderNames.USER_AGENT
import play.api.mvc._
import play.api.mvc.Results.{InternalServerError, TemporaryRedirect}
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -228,6 +229,7 @@ class BaseLoginAction(
UserSession.LoginType.InsecureDemoKey,
loginExpiresAt,
request.remoteAddress,
request.headers.get(USER_AGENT),
)
_ <- EitherT
.right[Error](IO.blocking(userService.recordLogin(user.id)))
Expand Down Expand Up @@ -399,6 +401,7 @@ class BaseLoginAction(
UserSession.LoginType.MagicLink,
loginExpiresAt,
request.remoteAddress,
request.headers.get(USER_AGENT),
)
_ <- EitherT
.right[Error](IO.blocking(userService.recordLogin(user.id)))
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/LoginController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ class LoginController @Inject() (
user.id,
UserSession.LoginType.Password,
expiresAt,
request.remoteAddress
request.remoteAddress,
request.headers.get(USER_AGENT),
)
_ <- EitherT.right[Error](
IO.blocking(
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/SignupController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ case class SignupController @Inject() (
UserSession.LoginType.MagicLink,
loginExpiresAt,
request.remoteAddress,
request.headers.get(USER_AGENT),
)
_ <- EitherT
.right[Error](IO.blocking(userService.recordLogin(user.id)))
Expand Down Expand Up @@ -398,6 +399,7 @@ case class SignupController @Inject() (
UserSession.LoginType.MagicLink,
loginExpiresAt,
request.remoteAddress,
request.headers.get(USER_AGENT),
)
_ <- EitherT
.right[Error](IO.blocking(userService.recordLogin(existingUser.id)))
Expand Down
1 change: 1 addition & 0 deletions app/models/UserSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ case class UserSession(
loginType: UserSession.LoginType,
expiresAt: Instant,
revokedAt: Option[Instant],
userAgent: Option[String],
) {

def isValid(now: Instant): Boolean =
Expand Down
20 changes: 14 additions & 6 deletions app/services/UserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ class UserService @Inject() (
"login_type",
"expires_at",
"revoked_at",
"user_agent",
)

private val qualifiedUserSessionParser = anorm.Macro.parser[UserSession](
Expand All @@ -690,6 +691,7 @@ class UserService @Inject() (
"user_session.login_type",
"user_session.expires_at",
"user_session.revoked_at",
"user_session.user_agent",
)

// Double the recommended minimum 64 bits of entropy
Expand All @@ -705,7 +707,8 @@ class UserService @Inject() (
userId: UUID,
loginType: UserSession.LoginType,
expiresAt: Instant,
ipAddress: String
ipAddress: String,
userAgent: Option[String],
): IO[Either[Error, UserSession]] =
generateNewSessionId
.flatMap(sessionId =>
Expand All @@ -718,7 +721,8 @@ class UserService @Inject() (
lastActivity = now,
loginType = loginType,
expiresAt = expiresAt,
revokedAt = None,
revokedAt = none,
userAgent = userAgent,
)
)
)
Expand All @@ -743,6 +747,7 @@ class UserService @Inject() (
private def saveUserSession(session: UserSession): IO[Either[Error, UserSession]] =
IO.blocking {
val _ = db.withConnection { implicit connection =>
val userAgent = session.userAgent.map(_.take(2048))
SQL"""
INSERT INTO user_session (
id,
Expand All @@ -751,15 +756,17 @@ class UserService @Inject() (
creation_ip_address,
last_activity,
login_type,
expires_at
expires_at,
user_agent
) VALUES (
${session.id},
${session.userId}::uuid,
${session.creationDate},
${session.creationIpAddress}::inet,
${session.lastActivity},
${stringifyLoginType(session.loginType)},
${session.expiresAt}
${session.expiresAt},
$userAgent
)
""".executeUpdate()
}
Expand All @@ -786,10 +793,11 @@ class UserService @Inject() (
userId: UUID,
loginType: UserSession.LoginType,
expiresAt: Instant,
ipAddress: String
ipAddress: String,
userAgent: Option[String],
): EitherT[IO, Error, UserSession] =
for {
session <- EitherT(generateNewUserSession(userId, loginType, expiresAt, ipAddress))
session <- EitherT(generateNewUserSession(userId, loginType, expiresAt, ipAddress, userAgent))
_ <- EitherT(saveUserSession(session))
} yield session

Expand Down
8 changes: 8 additions & 0 deletions conf/evolutions/default/77.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- !Ups

ALTER TABLE user_session ADD user_agent varchar(2048);


-- !Downs

ALTER TABLE user_session DROP user_agent;

0 comments on commit b3ae953

Please sign in to comment.