/
authentication.go
138 lines (112 loc) · 3.28 KB
/
authentication.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package api
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/kimitzu/kimitzu-services/configs"
"github.com/levigross/grequests"
)
var ServiceConfig configs.Daemon
type AuthPayload struct {
Username string `json:"username"`
Password string `json:"password"`
NewUsername string `json:"newUsername"`
NewPassword string `json:"newPassword"`
}
type KimitzuInfoP struct {
Repo string `json:"repoPath"`
Cookie string `json:"cookie"`
Username string `json:"username"`
Password string `json:"password"`
Authenticated bool `json:"authenticated"`
OBVersion string `json:"obVersion"`
KimitzuServiceVersion string `json:"kimitzuVersion"`
}
func GetInfo() (KimitzuInfoP, error) {
res, err := grequests.Get("http://127.0.0.1:8100/kimitzu/info", &grequests.RequestOptions{RequestTimeout: time.Second * 10})
if err != nil {
fmt.Println("Error", err)
return KimitzuInfoP{}, fmt.Errorf("Can't resolve node, probably offline")
}
info := KimitzuInfoP{}
info.KimitzuServiceVersion = ServiceConfig.Version
json.Unmarshal(res.Bytes(), &info)
return info, nil
}
func patchConfig(username, password string, authenticate bool) (KimitzuInfoP, error) {
res, err := grequests.Post("http://127.0.0.1:8100/kimitzu/config", &grequests.RequestOptions{
RequestTimeout: time.Second * 10,
JSON: map[string]interface{}{
"username": username,
"password": password,
"authenticated": authenticate,
},
})
if err != nil {
fmt.Println("Error", err)
return KimitzuInfoP{}, fmt.Errorf("Can't resolve node, probably offline")
}
info := KimitzuInfoP{}
json.Unmarshal(res.Bytes(), &info)
return info, nil
}
func Authenticate(w http.ResponseWriter, r *http.Request) {
setupResponse(&w, r)
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
b, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
payload := &AuthPayload{}
err = json.Unmarshal(b, &payload)
h := sha256.Sum256([]byte(payload.Password))
password := hex.EncodeToString(h[:])
info, err := GetInfo()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if r.Method == "GET" {
fmt.Fprintf(w, `{"authentication": %v}`, info.Authenticated)
return
}
if (!(info.Username == payload.Username) || !(info.Password == password)) && info.Authenticated {
http.Error(w, `{"error": "Invalid credentials"}`, 403)
return
}
if r.Method == "POST" {
fmt.Fprintf(w, `{"method": "POST", "success": "%v"}`, info.Cookie)
return
}
if r.Method == "PATCH" {
if payload.NewUsername == "" || payload.NewPassword == "" {
fmt.Fprintf(w, `{"method": "POST", "error": "new username or password is empty"}`)
return
}
d, err := patchConfig(payload.NewUsername, payload.NewPassword, true)
if err != nil {
fmt.Fprintf(w, `{"method": "POST", "error": "%v"}`, err)
return
}
fmt.Fprintf(w, `{"method": "PATCH", "success": "%v"}`, d)
return
}
if r.Method == "DELETE" {
d, err := patchConfig(payload.Username, payload.Password, false)
if err != nil {
fmt.Fprintf(w, `{"method": "DELETE", "error": "%v"}`, err)
return
}
fmt.Fprintf(w, `{"method": "DELETE", "success": "%v"}`, d)
return
}
}