-
Notifications
You must be signed in to change notification settings - Fork 187
/
token.go
73 lines (65 loc) · 1.59 KB
/
token.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
package application
import (
"errors"
"net/textproto"
http_service "github.com/eolinker/eosc/eocontext/http-context"
)
var (
PositionHeader = "header"
PositionQuery = "query"
PositionBody = "body"
)
var validPosition = []string{PositionHeader, PositionQuery, PositionBody}
//func GetToken(ctx http_service.IHttpContext, tokenName string, position string) (string, bool) {
// token, has := getToken(ctx, tokenName, position)
// if has {
// ctx.SetLabel("token", token)
// }
// return token, has
//}
func GetToken(ctx http_service.IHttpContext, tokenName string, position string) (string, bool) {
switch position {
case PositionHeader:
value, has := ctx.Request().Header().Headers()[textproto.CanonicalMIMEHeaderKey(tokenName)]
if has {
return value[0], has
}
return "", false
case PositionQuery:
value := ctx.Request().URI().GetQuery(tokenName)
return value, true
case "":
{
value, has := ctx.Request().Header().Headers()["Authorization"]
if has {
return value[0], has
}
return "", false
}
}
return "", false
}
func HideToken(ctx http_service.IHttpContext, tokenName string, position string) {
switch position {
case PositionHeader:
ctx.Proxy().Header().DelHeader(textproto.CanonicalMIMEHeaderKey(tokenName))
case PositionQuery:
ctx.Proxy().URI().DelQuery(tokenName)
case "":
{
ctx.Proxy().Header().DelHeader("Authorization")
}
}
return
}
func CheckPosition(position string) error {
if position == "" {
return nil
}
for _, p := range validPosition {
if p == position {
return nil
}
}
return errors.New("invalid position: " + position)
}