feat: middle save
This commit is contained in:
parent
5727d39eb8
commit
83a5ef9bf6
6 changed files with 129 additions and 44 deletions
|
@ -9,17 +9,31 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BasicAuth(ctx *gin.Context) {
|
func WorkerRoute(ctx *gin.Context) {
|
||||||
var matches = false
|
if !strings.HasPrefix(ctx.Request.URL.Path, "/api/worker") {
|
||||||
var list = []string{"/settings"}
|
ctx.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, i := range list {
|
var err error
|
||||||
if !strings.HasPrefix(ctx.Request.URL.Path, i) {
|
var dirs []service.PrivDir
|
||||||
|
auth := service.NewAuthService()
|
||||||
|
privdir := service.NewPrivDirService(nil)
|
||||||
|
dirs = privdir.Query()
|
||||||
|
if len(dirs) == 0 {
|
||||||
|
ctx.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var target string
|
||||||
|
var matches = false
|
||||||
|
for _, dir := range dirs {
|
||||||
|
if !strings.HasSuffix(ctx.Request.URL.Path, dir.DirName) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target = dir.DirName
|
||||||
matches = true
|
matches = true
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !matches {
|
if !matches {
|
||||||
|
@ -27,22 +41,67 @@ func BasicAuth(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := service.NewAuthService()
|
|
||||||
username, password, ok := ctx.Request.BasicAuth()
|
username, password, ok := ctx.Request.BasicAuth()
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.Status(401)
|
ctx.JSON(401, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "Unauthorized",
|
||||||
|
})
|
||||||
|
ctx.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ok, err := auth.VerifyToken(username, password)
|
ok, err = auth.VerifyToken(username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Status(500)
|
|
||||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||||
|
ctx.JSON(401, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "Unauthorized",
|
||||||
|
})
|
||||||
|
ctx.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var acc *service.Account
|
||||||
|
acc, err = auth.Read(username)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(500, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "Internal Server Error",
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
privdir = service.NewPrivDirService(acc)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.Status(401)
|
ctx.JSON(401, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "Unauthorized",
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var d *service.PrivDir
|
||||||
|
d, err = privdir.Read(target)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(500, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "Internal Server Error",
|
||||||
|
})
|
||||||
|
ctx.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if d == nil {
|
||||||
|
ctx.JSON(401, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "Unauthorized",
|
||||||
|
})
|
||||||
|
ctx.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,35 +8,6 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func login(ctx *gin.Context) {
|
|
||||||
auth := service.NewAuthService()
|
|
||||||
username := ctx.PostForm("username")
|
|
||||||
password := ctx.PostForm("password")
|
|
||||||
|
|
||||||
acc, err := auth.Read(username)
|
|
||||||
if err != nil {
|
|
||||||
ctx.JSON(401, gin.H{
|
|
||||||
"ok": 0,
|
|
||||||
"errno": "username or password not invalid",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ok, err := auth.Verify(username, password)
|
|
||||||
if err != nil || !ok {
|
|
||||||
ctx.JSON(401, gin.H{
|
|
||||||
"ok": 0,
|
|
||||||
"errno": "username or password not invalid",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.JSON(200, gin.H{
|
|
||||||
"ok": 1,
|
|
||||||
"token": auth.Token(acc.Username, acc.Password),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func readAcc(ctx *gin.Context) {
|
func readAcc(ctx *gin.Context) {
|
||||||
auth := service.NewAuthService()
|
auth := service.NewAuthService()
|
||||||
username, password, ok := ctx.Request.BasicAuth()
|
username, password, ok := ctx.Request.BasicAuth()
|
||||||
|
@ -120,3 +91,49 @@ func deleteAcc(ctx *gin.Context) {
|
||||||
|
|
||||||
ctx.Status(200)
|
ctx.Status(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func login(ctx *gin.Context) {
|
||||||
|
auth := service.NewAuthService()
|
||||||
|
username := ctx.PostForm("username")
|
||||||
|
password := ctx.PostForm("password")
|
||||||
|
|
||||||
|
acc, err := auth.Read(username)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(401, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "username or password not invalid",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ok, err := auth.Verify(username, password)
|
||||||
|
if err != nil || !ok {
|
||||||
|
ctx.JSON(401, gin.H{
|
||||||
|
"ok": 0,
|
||||||
|
"errno": "username or password not invalid",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(200, gin.H{
|
||||||
|
"ok": 1,
|
||||||
|
"token": auth.Token(acc.Username, acc.Password),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(ctx *gin.Context) {
|
||||||
|
auth := service.NewAuthService()
|
||||||
|
username, password, ok := ctx.Request.BasicAuth()
|
||||||
|
if !ok {
|
||||||
|
ctx.Status(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ok, err := auth.VerifyToken(username, password)
|
||||||
|
if err != nil || !ok {
|
||||||
|
ctx.Status(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.Status(200)
|
||||||
|
}
|
||||||
|
|
|
@ -10,14 +10,21 @@ import (
|
||||||
func New(app *gin.Engine, version *service.Version, apiOnly bool) {
|
func New(app *gin.Engine, version *service.Version, apiOnly bool) {
|
||||||
app.Use(middleware.CORS)
|
app.Use(middleware.CORS)
|
||||||
app.Use(middleware.Header)
|
app.Use(middleware.Header)
|
||||||
app.Use(middleware.BasicAuth)
|
app.Use(middleware.WorkerRoute)
|
||||||
|
|
||||||
api := app.Group("/api")
|
api := app.Group("/api")
|
||||||
api.GET("/path/*path", discoverPath)
|
api.GET("/path/*path", discoverPath)
|
||||||
api.GET("/download/*path", downloadPath)
|
api.GET("/download/*path", downloadPath)
|
||||||
|
|
||||||
|
w := api.Group("/worker")
|
||||||
|
{
|
||||||
|
w.GET("/discover/*path", discoverPath)
|
||||||
|
w.GET("/download/*path", downloadPath)
|
||||||
|
}
|
||||||
|
|
||||||
auth := api.Group("/auth")
|
auth := api.Group("/auth")
|
||||||
{
|
{
|
||||||
|
auth.GET("/check", check)
|
||||||
auth.POST("/login", login)
|
auth.POST("/login", login)
|
||||||
auth.GET("/read", readAcc)
|
auth.GET("/read", readAcc)
|
||||||
auth.PATCH("/update", updateAcc)
|
auth.PATCH("/update", updateAcc)
|
||||||
|
|
|
@ -10,13 +10,13 @@ function Settings() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (auth.token === null) {
|
if (auth.token === null) {
|
||||||
document.location.href = "/";
|
// document.location.href = "/";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auth.checkToken(auth.token).then((ok) => {
|
auth.checkToken(auth.token).then((ok) => {
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
document.location.href = "/";
|
// document.location.href = "/";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,8 @@ export const useAuthStore = create<AuthState>()(
|
||||||
clearToken: () => set({ token: null }),
|
clearToken: () => set({ token: null }),
|
||||||
checkToken: async (token: string) => {
|
checkToken: async (token: string) => {
|
||||||
const res = await fetch("/api/auth/check", {
|
const res = await fetch("/api/auth/check", {
|
||||||
|
method: "GET",
|
||||||
|
mode: "same-origin",
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Basic ${token}`
|
"Authorization": `Basic ${token}`
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ export interface DirEntry {
|
||||||
export const usePath = create<PathState>((set) => ({
|
export const usePath = create<PathState>((set) => ({
|
||||||
data: undefined,
|
data: undefined,
|
||||||
update: async (path: string) => {
|
update: async (path: string) => {
|
||||||
const res = await fetch(`/api/path/${path}`);
|
const res = await fetch(`/api/worker/discover/${path}`);
|
||||||
if (res.status !== 200 && res.status !== 304) {
|
if (res.status !== 200 && res.status !== 304) {
|
||||||
set({ data: undefined });
|
set({ data: undefined });
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue