-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add audit log with email notification (#26)
- Loading branch information
1 parent
4010ee2
commit 9121239
Showing
51 changed files
with
944 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
PUBLIC_APP_URL=http://localhost | ||
TRUST_PROXY=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #f0f0f0; | ||
color: #333; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
.container { | ||
background-color: #fff; | ||
color: #333; | ||
padding: 32px; | ||
border-radius: 10px; | ||
max-width: 600px; | ||
margin: 40px auto; | ||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | ||
} | ||
.header { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
margin-bottom: 24px; | ||
} | ||
.header .logo { | ||
display: flex; | ||
align-items: center; | ||
gap: 8px; | ||
} | ||
.header .logo img { | ||
width: 32px; | ||
height: 32px; | ||
object-fit: cover; | ||
} | ||
.header h1 { | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
} | ||
.warning { | ||
background-color: #ffd966; | ||
color: #7f6000; | ||
padding: 4px 12px; | ||
border-radius: 50px; | ||
font-size: 0.875rem; | ||
} | ||
.content { | ||
background-color: #fafafa; | ||
color: #333; | ||
padding: 24px; | ||
border-radius: 10px; | ||
} | ||
.content h2 { | ||
font-size: 1.25rem; | ||
font-weight: bold; | ||
margin-bottom: 16px; | ||
} | ||
.grid { | ||
display: grid; | ||
grid-template-columns: 1fr 1fr; | ||
gap: 16px; | ||
margin-bottom: 16px; | ||
} | ||
.grid div { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
.grid p { | ||
margin: 0; | ||
} | ||
.label { | ||
color: #888; | ||
font-size: 0.875rem; | ||
margin-bottom: 4px; | ||
} | ||
.message { | ||
font-size: 1rem; | ||
line-height: 1.5; | ||
} | ||
</style> | ||
<title>Pocket ID</title> | ||
</head> | ||
<body> | ||
|
||
<div class="container"> | ||
<div class="header"> | ||
<div class="logo"> | ||
<img src="{{appUrl}}/api/application-configuration/logo" alt="Pocket ID" /> | ||
<h1>{{appName}}</h1> | ||
</div> | ||
<div class="warning">Warning</div> | ||
</div> | ||
<div class="content"> | ||
<h2>New Sign-In Detected</h2> | ||
<div class="grid"> | ||
<div> | ||
<p class="label">IP Address</p> | ||
<p>{{ipAddress}}</p> | ||
</div> | ||
<div> | ||
<p class="label">Device</p> | ||
<p>{{device}}</p> | ||
</div> | ||
<div> | ||
<p class="label">Sign-In Time</p> | ||
<p>{{dateTimeString}}</p> | ||
</div> | ||
</div> | ||
<p class="message"> | ||
This sign-in was detected from a new device or location. If you recognize this activity, you can safely ignore | ||
this message. If not, please review your account and security settings. | ||
</p> | ||
</div> | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/stonith404/pocket-id/backend/internal/dto" | ||
"github.com/stonith404/pocket-id/backend/internal/middleware" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stonith404/pocket-id/backend/internal/service" | ||
"github.com/stonith404/pocket-id/backend/internal/utils" | ||
) | ||
|
||
func NewAuditLogController(group *gin.RouterGroup, auditLogService *service.AuditLogService, jwtAuthMiddleware *middleware.JwtAuthMiddleware) { | ||
alc := AuditLogController{ | ||
auditLogService: auditLogService, | ||
} | ||
|
||
group.GET("/audit-logs", jwtAuthMiddleware.Add(false), alc.listAuditLogsForUserHandler) | ||
} | ||
|
||
type AuditLogController struct { | ||
auditLogService *service.AuditLogService | ||
} | ||
|
||
func (alc *AuditLogController) listAuditLogsForUserHandler(c *gin.Context) { | ||
userID := c.GetString("userID") | ||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) | ||
pageSize, _ := strconv.Atoi(c.DefaultQuery("limit", "10")) | ||
|
||
// Fetch audit logs for the user | ||
logs, pagination, err := alc.auditLogService.ListAuditLogsForUser(userID, page, pageSize) | ||
if err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
// Map the audit logs to DTOs | ||
var logsDtos []dto.AuditLogDto | ||
err = dto.MapStructList(logs, &logsDtos) | ||
if err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
// Add device information to the logs | ||
for i, logsDto := range logsDtos { | ||
logsDto.Device = alc.auditLogService.DeviceStringFromUserAgent(logs[i].UserAgent) | ||
logsDtos[i] = logsDto | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"data": logsDtos, | ||
"pagination": pagination, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.