-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth.go
More file actions
63 lines (58 loc) · 1.69 KB
/
Copy pathauth.go
File metadata and controls
63 lines (58 loc) · 1.69 KB
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
package goubus
import (
"encoding/json"
"errors"
"strings"
"time"
)
// UbusAuthData represents the Data response from auth module
type UbusAuthData struct {
UbusRPCSession string `json:"ubus_rpc_session"`
Timeout int
Expires int
ExpireTime time.Time
ACLs UbusAuthACLS `json:"acls"`
Data map[string]string
}
// UbusAuthACLS represents the ACL from user on Authentication
type UbusAuthACLS struct {
AccessGroup map[string][]string `json:"access-group"`
Ubus map[string][]string
Uci map[string][]string
}
// AuthLogin Call JSON-RPC method to Router Authentication
func (u *Ubus) AuthLogin() (UbusResponse, error) {
var jsonStr = []byte(`
{
"jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": [
"00000000000000000000000000000000",
"session",
"login",
{
"username": "` + u.Username + `",
"password": "` + u.Password + `"
}
]
}`)
call, err := u.Call(jsonStr)
if err != nil {
if strings.Compare(err.Error(), "404 Not Found") == 0 {
return UbusResponse{}, errors.New("Ubus module not installed, try 'opkg update && opkg install uhttpd-mod-ubus && service uhttpd restart'")
}
return UbusResponse{}, err
}
ubusData := &UbusAuthData{}
ubusDataByte, err := json.Marshal(call.Result.([]interface{})[1])
if err != nil {
return UbusResponse{}, errors.New("Error Parsing Login Data")
}
json.Unmarshal(ubusDataByte, &ubusData)
//Set AuthData to Ubus struct
u.AuthData = ubusData
//Editing expire to current time + expire time to check in functions if the session has expired
u.AuthData.ExpireTime = time.Now().Add(time.Second * time.Duration(u.AuthData.Expires))
return call, nil
}