0
我用兩個結構來保存用戶的信息用戶的cookie驗證失敗隨機
// SecureDevice holds a user's device's infos
type SecureDevice struct {
Name string // Defined by the user
DeviceIP string
Token struct {
Token string
StartingDate time.Time // The token is supposed to last only a week before becoming invalid
}
}
// GlobalUser is a struct defining all user's infos registered inside the server
type GlobalUser struct {
Username string
Password string
Salt string
Mail string
ValidationToken string // Used to validate the user's mail adress
Lang string
ConversationsID []int // The private messages the user has part in
SecureDevicesList []SecureDevice
}
而且我使用一個函數來檢查,如果用戶在
// IsLoggedIn checks if client's token is valid
func IsLoggedIn(r *http.Request) string {
ips := strings.Split(r.Header.Get("X-Forwarded-For"), ", ")
ip := ips[0]
cookie, err := r.Cookie("auth")
if err != nil {
return "ERR$" + "not_connected"
}
cookieValue := strings.Split(cookie.Value, "$")
println(cookie.Value)
user := GetUser(cookieValue[0])
userToken := cookieValue[1]
if user.Username == "" {
return "ERR$" + "error"
}
for _, SecureDevice := range user.SecureDevicesList {
if SecureDevice.DeviceIP == ip && SecureDevice.Token.Token == userToken { // We make sure that the token provided is actually the user's token
if time.Since(SecureDevice.Token.StartingDate)*time.Hour >= 168 { // If token is older than 1 week, we throw it away
return "ERR$" + "error_token_expired"
} else if time.Since(SecureDevice.Token.StartingDate)*time.Second >= 30 { // If it's age is between 1 hour and one week, we renew it
db, err := scribble.New("./brony/db", nil)
if err != nil {
return "ERR$" + "error_internal"
}
tokenBytes, err := GenerateRandomBytes(64) // Generates a salt
if err != nil {
return "ERR$" + "error_internal"
}
token := base64.URLEncoding.EncodeToString(tokenBytes)
SecureDevice.Token.Token = token
SecureDevice.Token.StartingDate = time.Now()
errr := db.Write("users", user.Username, user)
if errr != nil {
return "ERR$" + "error_internal"
}
return "TOK$" + user.Username + "$" + SecureDevice.Token.Token
} else if time.Since(SecureDevice.Token.StartingDate)*time.Hour <= 1 {
return "NIL$"
}
} else if SecureDevice.DeviceIP == ip {
return "ERR$" + "error_bad_token"
}
}
return "ERR$" + "error_device_not_registered"
}
但幾乎總是記錄當我加載頁面
status := IsLoggedIn(r)
println(status)
它經常給我一個錯誤,因爲我打印「IsLoggedIn」來了解哪裏pr oblem來自
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
NIL$
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
ERR$error_token_expired
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
NIL$
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
ERR$error_token_expired
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
ERR$error_token_expired
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
ERR$error_token_expired
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
ERR$error_token_expired
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
ERR$error_token_expired
test$ppDXRggtztyA9OBbdZh1t1ESqRo2XvuOBt4xlDai9kVxwq-_3zlWyvgNgA7AZcSpasJ_YnXZvoG qlz1syF9X8g==
NIL$
起初我還以爲是我的令牌的更新代碼是錯誤的,但同時beeing故障,未完成的,它似乎並沒有被它的,因爲一些F5故障後,它說,餅乾是好的。我真的不明白錯在哪裏,而且它開始變得非常煩人,因爲我不能讓它成爲現實,至少對於用戶來說每次都要做f5會非常煩人,並且希望當頁面會刷新,它會奇蹟般地工作。代碼在debian服務器上運行
哇,似乎現在的工作每次。我仍然不明白爲什麼它有時會起作用,其他的則沒有。非常感謝你! –
不客氣。打印'elapsedHours'和先前計算的值'time.Since(SecureDevice.Token.StartingDate)* time.Hour'來查看差異。 – jeevatkm