/
auth.go
235 lines (166 loc) · 5.39 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package ginAuth
// this package is a secure cookie authentication middleware for the the Gin Web Framework
// https://github.com/gin-gonic/gin
import (
"github.com/gin-gonic/gin"
"github.com/astaxie/beego/config"
"github.com/gorilla/securecookie"
"time"
"strconv"
"strings"
"crypto/md5"
"encoding/hex"
"errors"
"io"
"net/http"
)
const VERSION = "0.0.1"
// set our global package variables
var (
CookieName string // the name of the cookie that will be used, default: "token"
ConfigPath string // path to config file, default: ""
ConfigType string // type of config file, default: "ini"
Prefix string // the key in ctx.Keys[] to use, default: ""
HashKey []byte // hash key for securecookie
BlockKey []byte // block key for securecookie
Expiration int64 // time until the cookie expires in seconds, default: 604800
Unauthorized func(ctx *gin.Context) // function called if user is not authorized
Authorized func(ctx *gin.Context) // function called if user is authorized
SecureCookie *securecookie.SecureCookie // global secure cookie object
)
func init() {
ConfigPath = ""
ConfigType = "ini"
CookieName = "token"
Prefix = ""
Expiration = 604800 // 7 days
}
// gin middleware handler
// call this on your groups that require authentication
func Use(ctx *gin.Context) {
err := Check(ctx)
if err == nil {
loggedIn, _ := ctx.Get(Prefix + "loggedIn")
if loggedIn == true {
if Authorized != nil {
Authorized(ctx)
}
} else {
if Unauthorized != nil {
Unauthorized(ctx)
}
}
}
}
// this function loads your specified configuration file and it's values
func LoadConfig() error {
if ConfigPath != "" {
conf, err := config.NewConfig(ConfigType, ConfigPath)
if err != nil {
return err
}
if cookiename := conf.String("cookiename"); cookiename != "" {
CookieName = cookiename
}
if prefix := conf.String("prefix"); prefix != "" {
Prefix = prefix
}
if hashkey := conf.String("hashkey"); hashkey != "" {
val, err := hex.DecodeString(hashkey)
if err != nil {
return err
}
HashKey = val
}
if blockkey := conf.String("blockkey"); blockkey != "" {
val, err := hex.DecodeString(blockkey)
if err != nil {
return err
}
BlockKey = val
}
if expiration := conf.String("expiration"); expiration != "" {
val, err := strconv.ParseInt(expiration, 10, 64)
if err != nil {
return err
}
Expiration = val
}
}
return nil
}
// a private function that simply saves the log in status of the user to the current context
func saveLogin(ctx *gin.Context, status bool) {
ctx.Set(Prefix+"loggedIn", status)
}
// a private function that returns the ip from the current request
func ip(ctx *gin.Context) string {
return strings.Split(ctx.Request.RemoteAddr, ":")[0]
}
// checks for our token cookie, decodes it, and determines if it is valid
// the encrypted cookie data set in Login() will be set to the current context as well
func Check(ctx *gin.Context) error {
// get the encrypted cookie value
cookie, err := ctx.Request.Cookie(CookieName)
if err == nil {
data := make(map[string]string)
SecureCookie = securecookie.New(HashKey, BlockKey)
if err := SecureCookie.Decode(CookieName, cookie.Value, &data); err == nil {
// save the login cookie data to the context
ctx.Set(Prefix+"cookieData", data)
hash := hashHeader(ctx)
expiration, err := strconv.ParseInt(data["expiration"], 10, 64)
if err != nil {
return err
}
if hash == data["hash"] && ip(ctx) == data["ip"] && time.Now().Before(time.Unix(expiration, 0)) {
saveLogin(ctx, true)
} else {
//call the full logout because it'll remove the cookie as well
Logout(ctx)
}
} else {
return err
}
}
return nil
}
// handles the login process
// the first param is a map of strings that will be added to the cookie data before encryption and will be
// able to be recovered when Check() is called
func Login(ctx *gin.Context, extra map[string]string) error {
data := make(map[string]string)
for key, value := range extra {
if key == "ip" || key == "hash" || key == "experation" {
return errors.New("The key '" + key + "' is reserved.")
}
data[key] = value
}
// our current time + our expiration time, converted to a unix time stamp
data["expiration"] = strconv.FormatInt(time.Now().Add(time.Duration(Expiration)*time.Second).Unix(), 10)
data["ip"] = ip(ctx)
data["hash"] = hashHeader(ctx)
// encode our cookie data securely
SecureCookie = securecookie.New(HashKey, BlockKey)
if encoded, err := SecureCookie.Encode(CookieName, data); err == nil {
//set our cookie
cookie := http.Cookie{Name: CookieName, Value: encoded, Path: "/", MaxAge: int(Expiration)}
http.SetCookie(ctx.Writer, &cookie)
} else {
return err
}
return nil
}
// removes our token cookie, sets the context to: not logged in
func Logout(ctx *gin.Context) {
cookie := http.Cookie{Name: CookieName, Path: "/", MaxAge: -1}
http.SetCookie(ctx.Writer, &cookie)
saveLogin(ctx, false)
}
// this function returns and md5 hash (string) for a few common request headers
func hashHeader(ctx *gin.Context) string {
h := md5.New()
io.WriteString(h, ctx.Request.Header.Get("User-Agent"))
io.WriteString(h, ctx.Request.Header.Get("Accept-Language"))
return hex.EncodeToString(h.Sum(nil))
}