-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (132 loc) · 4.08 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"context"
"log"
"os"
firebase "firebase.google.com/go"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
bcrypt "golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2/google"
"encoding/base64"
)
func setupRouter(app *firebase.App) *gin.Engine {
r := gin.Default()
firestore, err := app.Firestore(context.Background())
if err != nil {
ErrorLogger.Printf("error initializing firestore: %v\n", err)
return nil
}
r.POST("/authorize-mqtt-device", func(c *gin.Context) {
var json struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
if c.Bind(&json) == nil {
documents := firestore.Collection("devices").Where("id", "==", json.Username).Documents(context.Background())
doc, err := documents.Next()
if err != nil {
ErrorLogger.Printf("error getting document: %v\n", err)
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
passwordHash, ok := doc.Data()["passwordHash"].(string)
if !ok {
ErrorLogger.Println("error getting password hash")
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
if err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(json.Password)); err != nil {
ErrorLogger.Printf("error comparing password: %v\n", err)
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"authorized": true})
return
}
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
})
r.POST("/authorize-mqtt-superuser", func(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
})
r.POST("/acls", func(c *gin.Context) {
var json struct {
Username string `json:"username" binding:"required"`
Topic string `json:"topic" binding:"required"`
ClientId string `json:"clientId" binding:"required"`
Acc int `json:"acc" binding:"required"`
}
if c.Bind(&json) == nil {
documents := firestore.Collection("devices").Where("id", "==", json.Username).Documents(context.Background())
doc, err := documents.Next()
if err == iterator.Done {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
if err != nil {
ErrorLogger.Printf("error getting document: %v\n", err)
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
id := doc.Ref.ID
if id != strings.Split(json.Topic, "/")[1] {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"authorized": true})
}
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
})
return r
}
var (
WarningLogger *log.Logger
InfoLogger *log.Logger
ErrorLogger *log.Logger
)
func init() {
InfoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
WarningLogger = log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile)
ErrorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
base64Str, found := os.LookupEnv("FIREBASE_CONFIG_BASE64")
if !found {
ErrorLogger.Println("error getting firebase config")
os.Exit(1)
}
decoded, err := base64.StdEncoding.DecodeString(base64Str)
if err != nil {
ErrorLogger.Printf("error decoding firebase config: %v\n", err)
os.Exit(1)
}
credentials, err := google.CredentialsFromJSON(
context.Background(),
decoded,
"https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/cloud-platform",
)
if err != nil {
ErrorLogger.Printf("error getting firebase credentials: %v\n", err)
os.Exit(1)
}
app, err := firebase.NewApp(context.Background(), nil, option.WithCredentials(credentials))
if err != nil {
ErrorLogger.Printf("error initializing app: %v\n", err)
os.Exit(1)
}
r := setupRouter(app)
port, found := os.LookupEnv("PORT")
if found {
port = ":" + port
} else {
port = ":8080"
}
if err := r.Run(port); err != nil {
ErrorLogger.Printf("error running server: %v\n", err)
os.Exit(1)
}
}