A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
at lambda-fork/main 95 lines 2.5 kB view raw
1// SiYuan - Refactor your thinking 2// Copyright (c) 2020-present, b3log.org 3// 4// This program is free software: you can redistribute it and/or modify 5// it under the terms of the GNU Affero General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// This program is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU Affero General Public License for more details. 13// 14// You should have received a copy of the GNU Affero General Public License 15// along with this program. If not, see <https://www.gnu.org/licenses/>. 16 17package util 18 19import ( 20 "github.com/88250/gulu" 21 ginSessions "github.com/gin-contrib/sessions" 22 "github.com/gin-gonic/gin" 23 "github.com/siyuan-note/logging" 24) 25 26var WrongAuthCount int 27 28func NeedCaptcha() bool { 29 return 3 < WrongAuthCount 30} 31 32// SessionData represents the session. 33type SessionData struct { 34 Workspaces map[string]*WorkspaceSession // <WorkspacePath, WorkspaceSession> 35} 36 37type WorkspaceSession struct { 38 AccessAuthCode string 39 Captcha string 40} 41 42func (sd *SessionData) Clear(c *gin.Context) { 43 session := ginSessions.Default(c) 44 session.Delete("data") 45 if err := session.Save(); err != nil { 46 logging.LogErrorf("clear session failed: %v", err) 47 } 48} 49 50// Save saves the current session of the specified context. 51func (sd *SessionData) Save(c *gin.Context) error { 52 session := ginSessions.Default(c) 53 sessionDataBytes, err := gulu.JSON.MarshalJSON(sd) 54 if err != nil { 55 return err 56 } 57 session.Set("data", string(sessionDataBytes)) 58 return session.Save() 59} 60 61// GetSession returns session of the specified context. 62func GetSession(c *gin.Context) *SessionData { 63 ret := &SessionData{} 64 65 session := ginSessions.Default(c) 66 sessionDataStr := session.Get("data") 67 if nil == sessionDataStr { 68 return ret 69 } 70 71 err := gulu.JSON.UnmarshalJSON([]byte(sessionDataStr.(string)), ret) 72 if err != nil { 73 return ret 74 } 75 76 c.Set("session", ret) 77 return ret 78} 79 80func GetWorkspaceSession(session *SessionData) (ret *WorkspaceSession) { 81 ret = &WorkspaceSession{} 82 if nil == session.Workspaces { 83 session.Workspaces = map[string]*WorkspaceSession{} 84 } 85 ret = session.Workspaces[WorkspaceDir] 86 if nil == ret { 87 ret = &WorkspaceSession{} 88 session.Workspaces[WorkspaceDir] = ret 89 } 90 return 91} 92 93func RemoveWorkspaceSession(session *SessionData) { 94 delete(session.Workspaces, WorkspaceDir) 95}