|
| 1 | +// Copyright 2014 beego Author. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package session |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/aes" |
| 19 | + "crypto/cipher" |
| 20 | + "encoding/json" |
| 21 | + "net/http" |
| 22 | + "net/url" |
| 23 | + "sync" |
| 24 | +) |
| 25 | + |
| 26 | +var cookiepder = &CookieProvider{} |
| 27 | + |
| 28 | +// CookieSessionStore Cookie SessionStore |
| 29 | +type CookieSessionStore struct { |
| 30 | + sid string |
| 31 | + values map[interface{}]interface{} // session data |
| 32 | + lock sync.RWMutex |
| 33 | +} |
| 34 | + |
| 35 | +// Set value to cookie session. |
| 36 | +// the value are encoded as gob with hash block string. |
| 37 | +func (st *CookieSessionStore) Set(key, value interface{}) error { |
| 38 | + st.lock.Lock() |
| 39 | + defer st.lock.Unlock() |
| 40 | + st.values[key] = value |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// Get value from cookie session |
| 45 | +func (st *CookieSessionStore) Get(key interface{}) interface{} { |
| 46 | + st.lock.RLock() |
| 47 | + defer st.lock.RUnlock() |
| 48 | + if v, ok := st.values[key]; ok { |
| 49 | + return v |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// Delete value in cookie session |
| 55 | +func (st *CookieSessionStore) Delete(key interface{}) error { |
| 56 | + st.lock.Lock() |
| 57 | + defer st.lock.Unlock() |
| 58 | + delete(st.values, key) |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Flush Clean all values in cookie session |
| 63 | +func (st *CookieSessionStore) Flush() error { |
| 64 | + st.lock.Lock() |
| 65 | + defer st.lock.Unlock() |
| 66 | + st.values = make(map[interface{}]interface{}) |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// SessionID Return id of this cookie session |
| 71 | +func (st *CookieSessionStore) SessionID() string { |
| 72 | + return st.sid |
| 73 | +} |
| 74 | + |
| 75 | +// SessionRelease Write cookie session to http response cookie |
| 76 | +func (st *CookieSessionStore) SessionRelease(w http.ResponseWriter) { |
| 77 | + str, err := encodeCookie(cookiepder.block, |
| 78 | + cookiepder.config.SecurityKey, |
| 79 | + cookiepder.config.SecurityName, |
| 80 | + st.values) |
| 81 | + if err != nil { |
| 82 | + return |
| 83 | + } |
| 84 | + cookie := &http.Cookie{Name: cookiepder.config.CookieName, |
| 85 | + Value: url.QueryEscape(str), |
| 86 | + Path: "/", |
| 87 | + HttpOnly: true, |
| 88 | + Secure: cookiepder.config.Secure, |
| 89 | + MaxAge: cookiepder.config.Maxage} |
| 90 | + http.SetCookie(w, cookie) |
| 91 | + return |
| 92 | +} |
| 93 | + |
| 94 | +type cookieConfig struct { |
| 95 | + SecurityKey string `json:"securityKey"` |
| 96 | + BlockKey string `json:"blockKey"` |
| 97 | + SecurityName string `json:"securityName"` |
| 98 | + CookieName string `json:"cookieName"` |
| 99 | + Secure bool `json:"secure"` |
| 100 | + Maxage int `json:"maxage"` |
| 101 | +} |
| 102 | + |
| 103 | +// CookieProvider Cookie session provider |
| 104 | +type CookieProvider struct { |
| 105 | + maxlifetime int64 |
| 106 | + config *cookieConfig |
| 107 | + block cipher.Block |
| 108 | +} |
| 109 | + |
| 110 | +// SessionInit Init cookie session provider with max lifetime and config json. |
| 111 | +// maxlifetime is ignored. |
| 112 | +// json config: |
| 113 | +// securityKey - hash string |
| 114 | +// blockKey - gob encode hash string. it's saved as aes crypto. |
| 115 | +// securityName - recognized name in encoded cookie string |
| 116 | +// cookieName - cookie name |
| 117 | +// maxage - cookie max life time. |
| 118 | +func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error { |
| 119 | + pder.config = &cookieConfig{} |
| 120 | + err := json.Unmarshal([]byte(config), pder.config) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + if pder.config.BlockKey == "" { |
| 125 | + pder.config.BlockKey = string(generateRandomKey(16)) |
| 126 | + } |
| 127 | + if pder.config.SecurityName == "" { |
| 128 | + pder.config.SecurityName = string(generateRandomKey(20)) |
| 129 | + } |
| 130 | + pder.block, err = aes.NewCipher([]byte(pder.config.BlockKey)) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + pder.maxlifetime = maxlifetime |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// SessionRead Get SessionStore in cooke. |
| 139 | +// decode cooke string to map and put into SessionStore with sid. |
| 140 | +func (pder *CookieProvider) SessionRead(sid string) (Store, error) { |
| 141 | + maps, _ := decodeCookie(pder.block, |
| 142 | + pder.config.SecurityKey, |
| 143 | + pder.config.SecurityName, |
| 144 | + sid, pder.maxlifetime) |
| 145 | + if maps == nil { |
| 146 | + maps = make(map[interface{}]interface{}) |
| 147 | + } |
| 148 | + rs := &CookieSessionStore{sid: sid, values: maps} |
| 149 | + return rs, nil |
| 150 | +} |
| 151 | + |
| 152 | +// SessionExist Cookie session is always existed |
| 153 | +func (pder *CookieProvider) SessionExist(sid string) bool { |
| 154 | + return true |
| 155 | +} |
| 156 | + |
| 157 | +// SessionRegenerate Implement method, no used. |
| 158 | +func (pder *CookieProvider) SessionRegenerate(oldsid, sid string) (Store, error) { |
| 159 | + return nil, nil |
| 160 | +} |
| 161 | + |
| 162 | +// SessionDestroy Implement method, no used. |
| 163 | +func (pder *CookieProvider) SessionDestroy(sid string) error { |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// SessionGC Implement method, no used. |
| 168 | +func (pder *CookieProvider) SessionGC() { |
| 169 | + return |
| 170 | +} |
| 171 | + |
| 172 | +// SessionAll Implement method, return 0. |
| 173 | +func (pder *CookieProvider) SessionAll() int { |
| 174 | + return 0 |
| 175 | +} |
| 176 | + |
| 177 | +// SessionUpdate Implement method, no used. |
| 178 | +func (pder *CookieProvider) SessionUpdate(sid string) error { |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func init() { |
| 183 | + Register("cookie", cookiepder) |
| 184 | +} |
0 commit comments