-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe6cdad
commit b2f39a1
Showing
15 changed files
with
390 additions
and
10 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,3 +1,4 @@ | ||
tmp | ||
imap-sync | ||
LOG_imapsync | ||
db.db |
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 |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package internal | ||
|
||
import "github.com/gin-gonic/gin" | ||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
ginsession "github.com/go-session/gin-session" | ||
) | ||
|
||
func handleAdmin(ctx *gin.Context) { | ||
store := ginsession.FromContext(ctx) | ||
_, ok := store.Get("user") | ||
if !ok { | ||
// User is not logged in, redirect them to the login page | ||
ctx.Redirect(http.StatusTemporaryRedirect, "/login") | ||
return | ||
} | ||
|
||
ctx.HTML(200, "admin.html", nil) | ||
} |
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,52 @@ | ||
package internal | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
ginsession "github.com/go-session/gin-session" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type user struct { | ||
Name string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func handleLogin(ctx *gin.Context) { | ||
store := ginsession.FromContext(ctx) | ||
_, ok := store.Get("user") | ||
if ok { | ||
// User is not logged in, redirect them to the login page | ||
ctx.Redirect(http.StatusTemporaryRedirect, "/") | ||
return | ||
} | ||
ctx.HTML(200, "login.html", nil) | ||
} | ||
|
||
func login(ctx *gin.Context) { | ||
var data user | ||
data.Name = ctx.PostForm("username") | ||
data.Password = ctx.PostForm("password") | ||
|
||
pass, err := getPassword(data.Name) | ||
if err != nil { | ||
ctx.IndentedJSON(http.StatusUnauthorized, gin.H{"message": "user not found"}) | ||
return | ||
} | ||
|
||
err = bcrypt.CompareHashAndPassword([]byte(pass), []byte(data.Password)) | ||
if err != nil { | ||
ctx.IndentedJSON(http.StatusUnauthorized, gin.H{"message": "wrong password"}) | ||
return | ||
} | ||
store := ginsession.FromContext(ctx) | ||
|
||
store.Set("user", data.Name) | ||
if err := store.Save(); err != nil { | ||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) | ||
return | ||
} | ||
ctx.Redirect(http.StatusMovedPermanently, "/admin") | ||
|
||
} |
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,102 @@ | ||
package internal | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
var db *sql.DB | ||
|
||
func InitDb() error { | ||
var err error | ||
db, err = sql.Open("sqlite3", "./db.db") | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
initStmt := ` | ||
CREATE TABLE IF NOT EXISTS Users ( | ||
id INTEGER PRIMARY KEY, | ||
username VARCHAR(64) NULL, | ||
password VARCHAR(64) NULL | ||
); | ||
` | ||
_, err = db.Exec(initStmt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var exists bool | ||
err = db.QueryRow("SELECT exists (SELECT 1 FROM users WHERE username = ?)", admin_name).Scan(&exists) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
password, err := bcrypt.GenerateFromPassword([]byte(*admin_pass), 14) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !exists { | ||
_, err = db.Exec("INSERT INTO users(username, password) VALUES(?, ?)", admin_name, password) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
dbPass, err := getPassword(*admin_name) | ||
if err != nil { | ||
return err | ||
} | ||
if !reflect.DeepEqual(password, []byte(dbPass)) { | ||
changePassword(*admin_name, string(password)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func changePassword(username string, newPassword string) error { | ||
var err error | ||
db, err = sql.Open("sqlite3", "./db.db") | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
stmt, err := db.Prepare("UPDATE users SET password = ? WHERE username = ?") | ||
if err != nil { | ||
return err | ||
} | ||
defer stmt.Close() | ||
|
||
_, err = stmt.Exec(newPassword, username) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getPassword(username string) (string, error) { | ||
var err error | ||
db, err = sql.Open("sqlite3", "./db.db") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer db.Close() | ||
var password string | ||
err = db.QueryRow("SELECT password FROM users WHERE username = ?", username).Scan(&password) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return "", fmt.Errorf("no user found with username %s", username) | ||
} | ||
return "", err | ||
} | ||
return password, nil | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,12 +6,12 @@ | |
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>imapsync-web-admin</title> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/styles.css" /> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
<script src="https://unpkg.com/htmx.[email protected]" | ||
<link rel="stylesheet" href="/static/css/styles.css" /> | ||
<script src="/static/js/tailwind.js"></script> | ||
<script src="/static/js/htmx.min.js" | ||
integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO" | ||
crossorigin="anonymous"></script> | ||
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script> | ||
<script defer src="/static/js/cdn.min.js"></script> | ||
|
||
<style> | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,9 @@ | |
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>imapsync-web</title> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/styles.css" /> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
<script src="https://unpkg.com/htmx.[email protected]" | ||
<link rel="stylesheet" href="/static/css/styles.css" /> | ||
<script src="/static/js/tailwind.js"></script> | ||
<script src="/static/js/htmx.min.js" | ||
integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO" | ||
crossorigin="anonymous"></script> | ||
|
||
|
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,65 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Login</title> | ||
<style> | ||
body { | ||
font-family: Arial, Helvetica, sans-serif; | ||
} | ||
.container { | ||
width: 300px; | ||
padding: 16px; | ||
background-color: white; | ||
margin: 0 auto; | ||
margin-top: 100px; | ||
border: 1px solid black; | ||
border-radius: 4px; | ||
} | ||
input[type="text"], | ||
input[type="password"] { | ||
width: 90%; | ||
padding: 15px; | ||
margin: 5px 0 22px 0; | ||
display: inline-block; | ||
border: none; | ||
background: #f1f1f1; | ||
} | ||
button { | ||
background-color: #4caf50; | ||
color: white; | ||
padding: 14px 20px; | ||
margin: 8px 0; | ||
border: none; | ||
cursor: pointer; | ||
width: 100%; | ||
opacity: 0.9; | ||
} | ||
button:hover { | ||
opacity: 1; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<form action="/auth/login" method="post" class="container"> | ||
<label for="uname"><b>Username</b></label> | ||
<input | ||
type="text" | ||
placeholder="Enter Username" | ||
name="username" | ||
required | ||
/> | ||
|
||
<label for="psw"><b>Password</b></label> | ||
<input | ||
type="password" | ||
placeholder="Enter Password" | ||
name="password" | ||
required | ||
/> | ||
|
||
<button type="submit">Login</button> | ||
</form> | ||
</body> | ||
</html> |