Commit 7e81b3e8 authored by chenhan wang's avatar chenhan wang
Browse files

fix: solve the authentication problem in first commit

parent 6209decc
...@@ -2,9 +2,9 @@ config.yaml ...@@ -2,9 +2,9 @@ config.yaml
backend/files backend/files
.history/ .history/
.idea/** .idea/**
conf.yaml
docs/** docs/**
**.exe **.exe
files/** files/**
**.log **.log
**.sql **.sql
mysql
\ No newline at end of file
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// =======authenticate==========
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
/* /*
...@@ -26,17 +27,33 @@ func IdGen(n int) string { ...@@ -26,17 +27,33 @@ func IdGen(n int) string {
return string(b) return string(b)
} }
/*
func IdGen(n int ) string{
id:=newId(n)
var check bool
// 数据库里查找有无sid与id相同
while (!check){
}
}
*/
/* /*
* 新建一个sid和url的关联,并返回一个bool表示成功与否 * 新建一个sid和url的关联,并返回一个bool表示成功与否
* 若无sessionId,生成一个八位sid并返回 * 若无sessionId,生成一个八位sid并返回
*/ */
func newAuthenticate(sid string, url string, passwd string, time time.Time) (rsid string, stat uint) { func newAuthenticate(sid string, url string, passwd string, time time.Time, isFirst bool) (rsid string, stat uint) {
if sid == "" { if sid == "" {
rsid = IdGen(8) rsid = IdGen(8)
} else { } else {
rsid = sid rsid = sid
} }
if isFirst {
model.CreatelinkFirstTime(rsid, url, time)
stat = 1
} else {
stat = model.Createlink(rsid, passwd, url, time) stat = model.Createlink(rsid, passwd, url, time)
}
return rsid, stat return rsid, stat
} }
...@@ -50,32 +67,12 @@ func Autheticate(cookie *http.Cookie, url string, passwd string, time time.Time) ...@@ -50,32 +67,12 @@ func Autheticate(cookie *http.Cookie, url string, passwd string, time time.Time)
return 1 // 鉴权通过 return 1 // 鉴权通过
} else { } else {
var stat uint var stat uint
sid, stat = newAuthenticate(sid, url, passwd, time) sid, stat = newAuthenticate(sid, url, passwd, time, false)
cookie.Value = sid cookie.Value = sid
return stat return stat
} }
} }
/*
* 通过文件扩展名获取ContentType
*/
func GetFileContentType(fileType string) string {
var StrRet string = ""
switch fileType {
case ".txt":
StrRet = "text/plain"
case ".csv":
StrRet = "text/csv"
case ".tex":
StrRet = "application/x-tex"
case ".md":
StrRet = "text/x-markdown"
default:
StrRet = "text/plain"
}
return StrRet
}
// 设置cookie name sid, value link // 设置cookie name sid, value link
func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time_ time.Time) error { func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time_ time.Time) error {
cookie.Name = "User" // 标识为user cookie.Name = "User" // 标识为user
...@@ -114,6 +111,7 @@ func overflow(info *File, threshold int) bool { ...@@ -114,6 +111,7 @@ func overflow(info *File, threshold int) bool {
} }
// / ========== DB related==================
func DBupdate(c echo.Context, info *File) (string, string) { func DBupdate(c echo.Context, info *File) (string, string) {
url := "http://pastebin/" + IdGen(8) url := "http://pastebin/" + IdGen(8)
cookie, _ := c.Cookie("User") cookie, _ := c.Cookie("User")
...@@ -123,8 +121,7 @@ func DBupdate(c echo.Context, info *File) (string, string) { ...@@ -123,8 +121,7 @@ func DBupdate(c echo.Context, info *File) (string, string) {
} else { } else {
sid = cookie.Value sid = cookie.Value
} }
sid, _ = newAuthenticate(sid, url, info.Url, info.Time) sid, _ = newAuthenticate(sid, url, info.Url, info.Time, true)
model.CreatelinkFirstTime(sid, url, info.Time)
if info.MaxAccess == 0 { if info.MaxAccess == 0 {
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64) //num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info.MaxAccess = 30 // 设置最大默认可访问次数 info.MaxAccess = 30 // 设置最大默认可访问次数
...@@ -133,17 +130,6 @@ func DBupdate(c echo.Context, info *File) (string, string) { ...@@ -133,17 +130,6 @@ func DBupdate(c echo.Context, info *File) (string, string) {
return sid, url return sid, url
} }
// 格式化后缀,仿止出错
func TypeComplement(typ string) string {
// 类型判断
if typ == "" {
typ = ".txt"
} else if typ[0] != '.' {
typ = "." + typ
}
return typ
}
/* /*
* 没有设定过期时间,oriTime 为 0 * 没有设定过期时间,oriTime 为 0
* 此函数将默认过期时间设为当前时间后半小时 * 此函数将默认过期时间设为当前时间后半小时
...@@ -156,6 +142,8 @@ func timeAssign(oriTime time.Time) time.Time { ...@@ -156,6 +142,8 @@ func timeAssign(oriTime time.Time) time.Time {
} }
} }
// ===========sumdry=======================
// 从config读取数据
func GetSetting(key string) (value string) { func GetSetting(key string) (value string) {
// config // config
viper.SetConfigName("config") viper.SetConfigName("config")
...@@ -169,3 +157,34 @@ func GetSetting(key string) (value string) { ...@@ -169,3 +157,34 @@ func GetSetting(key string) (value string) {
logInfo := viper.GetStringMapString("setting") logInfo := viper.GetStringMapString("setting")
return logInfo[key] return logInfo[key]
} }
/*
* 通过文件扩展名获取ContentType
*/
func GetFileContentType(fileType string) string {
var StrRet string = ""
switch fileType {
case ".txt":
StrRet = "text/plain"
case ".csv":
StrRet = "text/csv"
case ".tex":
StrRet = "application/x-tex"
case ".md":
StrRet = "text/x-markdown"
default:
StrRet = "text/plain"
}
return StrRet
}
// 格式化后缀,仿止出错
func TypeComplement(typ string) string {
// 类型判断
if typ == "" {
typ = ".txt"
} else if typ[0] != '.' {
typ = "." + typ
}
return typ
}
...@@ -74,6 +74,7 @@ func SendFile(c echo.Context) error { ...@@ -74,6 +74,7 @@ func SendFile(c echo.Context) error {
stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink
// response // response
switch stat { switch stat {
case 0: case 0:
return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403 return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403
case 1: // 鉴权通过 case 1: // 鉴权通过
......
...@@ -98,10 +98,10 @@ func Checkt(p Content) bool { ...@@ -98,10 +98,10 @@ func Checkt(p Content) bool {
// 这个函数那边有可能需要 // 这个函数那边有可能需要
// 检查sid_url是否超时 // 检查sid_url是否超时
//同时也可以查询sid,_url是否关联 // 同时也可以查询sid,_url是否关联
func Find(sid string,_url string) bool { func Find(sid string, _url string) bool {
var s Rel var s Rel
err := DB.Where(&Rel{Sid: sid,Url: _url}).First(&s).Error err := DB.Where(&Rel{Sid: sid, Url: _url}).First(&s).Error
if err != nil { if err != nil {
return false return false
} }
...@@ -129,18 +129,6 @@ func Find1(_url string, key string) string { ...@@ -129,18 +129,6 @@ func Find1(_url string, key string) string {
return "" return ""
} }
//var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
/*//*随机生成字符串
func randStr(n int) string {
rand.Seed(time.Now().Unix())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}*/
// 新建用户 // 新建用户
func Createuser(_User Users) { func Createuser(_User Users) {
//_User.SessionId=randStr(8) //_User.SessionId=randStr(8)
...@@ -156,14 +144,14 @@ func Createuser(_User Users) { ...@@ -156,14 +144,14 @@ func Createuser(_User Users) {
*/ */
func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint { func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint {
var p Content var p Content
DB.First(&p,"Url1 = ?", _url) DB.First(&p, "Url1 = ?", _url)
if p.Passwd == _passwd { if p.Passwd == _passwd {
if Checkt(p) { if Checkt(p) {
//fmt.Println(sid,_passwd,_url,_Time1) //fmt.Println(sid,_passwd,_url,_Time1)
return 2 return 2
} }
var p1 Rel var p1 Rel
err := DB.Where(&Rel{Sid: sid,Url: _url}).First(&p1).Error err := DB.Where(&Rel{Sid: sid, Url: _url}).First(&p1).Error
if err != nil { if err != nil {
rel1 := Rel{ rel1 := Rel{
Sid: sid, Sid: sid,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment