-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (133 loc) · 3.01 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
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"cloud.google.com/go/bigquery"
"github.com/joho/godotenv"
"github.com/pkg/profile"
"google.golang.org/api/iterator"
)
// EnvLoad ...
func EnvLoad() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
// UserInfo ...
type UserInfo struct {
Password string `json:"password"`
}
// Request ...
type Request struct {
Username string `json:"username"`
RequestPWD string `json:"password"`
}
// Iterator ...
type Iterator interface {
Next(interface{}) error
}
func getPasswordIter(ctx context.Context, username string) (Iterator, error) {
projectID := os.Getenv("GCP_PROJECT_ID")
dataset := os.Getenv("BQ_DATASET_NAME")
table := os.Getenv("BQ_AUTH_TABLE_NAME")
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
log.Println("[Error]: ", err)
return nil, err
}
defer client.Close()
query := fmt.Sprintf(
"SELECT password FROM `%s.%s.%s` WHERE user_id='%s'",
projectID,
dataset,
table,
username,
)
q := client.Query(query)
job, err := q.Run(ctx)
if err != nil {
log.Println("[Query Error]: ", err)
return nil, err
}
status, err := job.Wait(ctx)
if err != nil {
log.Println("[Job Error]: ", err)
return nil, err
}
if err := status.Err(); err != nil {
log.Println("[Status Error]: ", err)
return nil, err
}
it, err := job.Read(ctx)
if err != nil {
log.Println("[Read Error]: ", err)
return nil, err
}
return it, nil
}
// GetHashedPWD ...
func GetHashedPWD(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", os.Getenv("ALLOW_ORIGIN"))
w.Header().Set(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
)
if r.Method == "OPTIONS" {
w.WriteHeader(200)
return
}
var req = new(Request)
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
log.Println("[Request Error]: ", err)
return
}
if req.RequestPWD != os.Getenv("REQUEST_PWD") {
log.Println("[Permission Error] Status Code 400")
fmt.Fprintf(w, "Permission denied\n")
return
}
username := req.Username
ctx := context.Background()
it, err := getPasswordIter(ctx, username)
if err != nil {
return
}
for {
var row []bigquery.Value
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
log.Println("[Iterator Error]: ", err)
return
}
if password, ok := row[0].(string); ok {
hashedPwd := sha256.Sum256([]byte(password))
hashedPwdStr := hex.EncodeToString(hashedPwd[:])
resp := UserInfo{hashedPwdStr}
jresp, err := json.Marshal(resp)
if err != nil {
log.Println("[JSON Error]: ", err)
return
}
w.Write(jresp)
break
}
}
log.Println("[Request]: Status Code 200")
}
func main() {
defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
EnvLoad()
http.HandleFunc("/login", GetHashedPWD)
http.ListenAndServe(":8080", nil)
}