-
Notifications
You must be signed in to change notification settings - Fork 5
/
authCheck.go
65 lines (58 loc) · 1.2 KB
/
authCheck.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
package main
import (
"encoding/json"
"github.com/mijia/sweb/log"
"io/ioutil"
"net/http"
"os"
"strings"
)
type ConsoleResponse struct {
Role MaintainerRole `json:"role"`
}
type MaintainerRole struct {
Role string `json:"role"`
}
func checkMaintainerAuth(req *http.Request) bool {
// return true
var ap string
if len(req.Form[APPNAME]) > 0 {
ap = req.Form[APPNAME][0]
} else {
return false
}
actk := req.Header.Get("access-token")
log.Debug(actk)
if strings.EqualFold(actk, "") {
actk = req.Header.Get("Access-Token")
log.Debug(actk)
if actk == "" {
return false
}
}
domain := os.Getenv("LAIN_DOMAIN")
url := "http://console." + domain + "/api/v1/repos/" + ap + "/roles/"
conReq, _ := http.NewRequest("GET", url, nil)
conReq.Header.Set("access-token", actk)
resp, err := http.DefaultClient.Do(conReq)
if err != nil {
log.Error(err)
return false
} else {
defer resp.Body.Close()
var tmp ConsoleResponse
resBody, _ := ioutil.ReadAll(resp.Body)
log.Debug(string(resBody))
err = json.Unmarshal(resBody, &tmp)
if err != nil {
log.Error(err)
return false
}
log.Debug(tmp)
if strings.EqualFold(tmp.Role.Role, "") {
return false
} else {
return true
}
}
}