-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlrun.go
60 lines (43 loc) · 1.03 KB
/
controlrun.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
package controlrun
import (
"crypto/sha1"
"encoding/hex"
"errors"
"net/http"
)
var (
firehost string = "" // Firebase Realtime database
token string = "" // Project/token access
)
// Setup the access endpoint
func Set(firebase_server string, project_token string) string {
_hash := sha1.New()
firehost = firebase_server
token = project_token
_hash.Write([]byte(firehost + token))
_sha := hex.EncodeToString(_hash.Sum(nil))
return _sha
}
// Check if have access
func Run(hash ...string) error {
if len(firehost) == 0 || len(token) == 0 {
return errors.New("Setup missing!")
}
if len(hash) > 0 {
_hash := sha1.New()
_hash.Write([]byte(firehost + token))
_sha := hex.EncodeToString(_hash.Sum(nil))
if _sha != hash[0] {
return errors.New("Permission denied!")
}
}
resp, err := http.Get("https://" + firehost + ".firebaseio.com/" + token + ".json")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
return errors.New("Permission denied!")
}