forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_Auth.go
77 lines (66 loc) · 1.49 KB
/
context_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
package gramework
import (
"bytes"
"encoding/base64"
"errors"
)
var authHeaderName = []byte("Authorization")
var authSplitter = []byte(":")
var errInvalidAuth = errors.New("invalid auth request")
// GetPass lazy triggers parser and returns
// password or an error. Error will be persistent
// for current context
//
// Common typos: GetPassword
func (a *Auth) GetPass() (string, error) {
// yep, we copying the code, but
// we get one instead of two jumps
if !a.parsed {
a.parse()
}
return a.pass, a.err
}
// GetLogin lazy triggers parser and returns
// login or an error. Error will be persistent
// for current context
//
// Common typos: GetUser, GetUsername
func (a *Auth) GetLogin() (string, error) {
if !a.parsed {
a.parse()
}
return a.login, a.err
}
func (a *Auth) parse() {
if a.err != nil {
return // parsing already failed
}
auth := a.ctx.Request.Header.PeekBytes(authHeaderName)
if len(auth) < 7 {
a.err = errInvalidAuth
return
}
decoded, err := base64.StdEncoding.DecodeString(BytesToString(auth[6:]))
if err != nil {
a.err = err
return
}
splitted := bytes.Split(decoded, authSplitter)
if len(splitted) != 2 {
a.err = errInvalidAuth
return
}
a.login, a.pass = string(splitted[0]), string(splitted[1])
}
// Auth returns struct for simple basic auth handling
//
// useful to develop e.g. stage environment login,
// where high security is not required
func (ctx *Context) Auth() *Auth {
if ctx.auth == nil {
ctx.auth = &Auth{
ctx: ctx,
}
}
return ctx.auth
}