-
Notifications
You must be signed in to change notification settings - Fork 2
/
hintegrate.go
100 lines (82 loc) · 2.43 KB
/
hintegrate.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
package hintegrate
import (
"encoding/json"
"fmt"
"math/rand"
"strconv"
"sync"
"time"
auth "github.com/hailo-platform/H2O/login-service/proto/auth"
"github.com/hailo-platform/H2O/hintegrate/request"
"github.com/hailo-platform/H2O/hintegrate/validators"
)
const (
adminUserFallback = "admin"
adminPassFallback = "Password1"
mech = "h2"
application = "ADMIN"
serviceName = "com.hailocab.service.login"
)
// Stores found admin tokens in a map based on callapi_host
// this is a proxyId for the environment this token works for
// I should expire these tokens at some point. Probably just
// do a read and see if it is still valid when I assign.
var (
adminToken map[string]string = make(map[string]string)
tokenLock sync.RWMutex
)
// GetAdminToken retursn an admin token and sets it to the given context
func GetAdminToken(c *request.Context) (string, error) {
tokenLock.Lock()
defer tokenLock.Unlock()
for _, host := range c.Hosts["callapi_host"] {
if len(adminToken[host]) != 0 {
_, err := ReadSession(c, adminToken[host], false)
if err != nil {
break
}
c.Vars.SetVar("admin_token", adminToken[host])
return adminToken[host], nil
}
}
session, err := adminLogin(c, c.Vars.GetVar("admin_user"), c.Vars.GetVar("admin_pass"))
if err != nil {
session, err = adminLogin(c, adminUserFallback, adminPassFallback)
}
for _, host := range c.Hosts["callapi_host"] {
adminToken[host] = session
}
return session, err
}
// adminLogin gets an admin token for the hardcoded admin user (hintegrate)
func adminLogin(c *request.Context, user, pass string) (string, error) {
rand.Seed(int64(time.Now().Nanosecond()))
stringMap := make(map[string]string)
stringMap["Username"] = user
stringMap["Password"] = pass
stringMap["Application"] = application
stringMap["Mech"] = mech
stringMap["DeviceType"] = strconv.Itoa(rand.Intn(1000000))
adminJson, err := json.Marshal(stringMap)
if err != nil {
return "", err
}
postData := map[string]string{
"service": "com.hailocab.service.login",
"endpoint": "auth",
"request": string(adminJson),
}
rsp, err := c.Post().SetHost("callapi_host").
PostDataMap(postData).SetPath("/rpc").
Run(serviceName+".auth", validators.Status2xxValidator())
if err != nil {
return "", err
}
authRsp := &auth.Response{}
err = json.Unmarshal(rsp.Body, authRsp)
if err != nil {
fmt.Println(string(rsp.Body))
return "", err
}
return authRsp.GetSessId(), nil
}