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
backend/files
.history/
.idea/**
conf.yaml
docs/**
**.exe
files/**
**.log
**.sql
\ No newline at end of file
**.sql
mysql
\ No newline at end of file
......@@ -11,6 +11,7 @@ import (
"github.com/spf13/viper"
)
// =======authenticate==========
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
/*
......@@ -26,17 +27,33 @@ func IdGen(n int) string {
return string(b)
}
/*
func IdGen(n int ) string{
id:=newId(n)
var check bool
// 数据库里查找有无sid与id相同
while (!check){
}
}
*/
/*
* 新建一个sid和url的关联,并返回一个bool表示成功与否
* 若无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 == "" {
rsid = IdGen(8)
} else {
rsid = sid
}
stat = model.Createlink(rsid, passwd, url, time)
if isFirst {
model.CreatelinkFirstTime(rsid, url, time)
stat = 1
} else {
stat = model.Createlink(rsid, passwd, url, time)
}
return rsid, stat
}
......@@ -50,32 +67,12 @@ func Autheticate(cookie *http.Cookie, url string, passwd string, time time.Time)
return 1 // 鉴权通过
} else {
var stat uint
sid, stat = newAuthenticate(sid, url, passwd, time)
sid, stat = newAuthenticate(sid, url, passwd, time, false)
cookie.Value = sid
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
func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time_ time.Time) error {
cookie.Name = "User" // 标识为user
......@@ -114,6 +111,7 @@ func overflow(info *File, threshold int) bool {
}
// / ========== DB related==================
func DBupdate(c echo.Context, info *File) (string, string) {
url := "http://pastebin/" + IdGen(8)
cookie, _ := c.Cookie("User")
......@@ -123,8 +121,7 @@ func DBupdate(c echo.Context, info *File) (string, string) {
} else {
sid = cookie.Value
}
sid, _ = newAuthenticate(sid, url, info.Url, info.Time)
model.CreatelinkFirstTime(sid, url, info.Time)
sid, _ = newAuthenticate(sid, url, info.Url, info.Time, true)
if info.MaxAccess == 0 {
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info.MaxAccess = 30 // 设置最大默认可访问次数
......@@ -133,17 +130,6 @@ func DBupdate(c echo.Context, info *File) (string, string) {
return sid, url
}
// 格式化后缀,仿止出错
func TypeComplement(typ string) string {
// 类型判断
if typ == "" {
typ = ".txt"
} else if typ[0] != '.' {
typ = "." + typ
}
return typ
}
/*
* 没有设定过期时间,oriTime 为 0
* 此函数将默认过期时间设为当前时间后半小时
......@@ -156,6 +142,8 @@ func timeAssign(oriTime time.Time) time.Time {
}
}
// ===========sumdry=======================
// 从config读取数据
func GetSetting(key string) (value string) {
// config
viper.SetConfigName("config")
......@@ -169,3 +157,34 @@ func GetSetting(key string) (value string) {
logInfo := viper.GetStringMapString("setting")
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 {
stat := Autheticate(cookie, info.Url, info.Passwd, info.Time) // 包含创建链接Createlink
// response
switch stat {
case 0:
return response.SendResponse(c, http.StatusForbidden, cookieMsg+"error:密码错误", "") //403
case 1: // 鉴权通过
......
......@@ -98,12 +98,12 @@ func Checkt(p Content) bool {
// 这个函数那边有可能需要
// 检查sid_url是否超时
//同时也可以查询sid,_url是否关联
func Find(sid string,_url string) bool {
// 同时也可以查询sid,_url是否关联
func Find(sid string, _url string) bool {
var s Rel
err := DB.Where(&Rel{Sid: sid,Url: _url}).First(&s).Error
if err != nil {
return false
err := DB.Where(&Rel{Sid: sid, Url: _url}).First(&s).Error
if err != nil {
return false
}
t := time.Now()
if t.After(s.Time) {
......@@ -129,18 +129,6 @@ func Find1(_url string, key string) string {
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) {
//_User.SessionId=randStr(8)
......@@ -156,19 +144,19 @@ func Createuser(_User Users) {
*/
func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint {
var p Content
DB.First(&p,"Url1 = ?", _url)
DB.First(&p, "Url1 = ?", _url)
if p.Passwd == _passwd {
if Checkt(p) {
//fmt.Println(sid,_passwd,_url,_Time1)
return 2
}
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 {
rel1 := Rel{
Sid: sid,
Url: _url,
Time: _Time1,
Sid: sid,
Url: _url,
Time: _Time1,
}
DB.Create(&rel1)
} else {
......@@ -182,11 +170,11 @@ func Createlink(sid string, _passwd string, _url string, _Time1 time.Time) uint
// 第一次上传
func CreatelinkFirstTime(sid string, _url string, _Time1 time.Time) {
sid1 := Rel{
sid1 := Rel{
Sid: sid,
Url: _url,
Time: _Time1,
}
}
DB.Create(&sid1)
}
......
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