-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
163 lines (128 loc) · 4.26 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
//ProductionHosts should be configured to include all hosts the mobile can connect to
var ProductionHosts Hosts = []Host{
{name: "zwe", url: "https://zwe.withsempo.com"},
{name: "demo", url: "https://demo.withsempo.com"},
{name: "dev", url: "https://dev.withsempo.com"},
{name: "mwk", url: "https://mwk.withsempo.com"},
}
//Host provides the structure for user-configured hosts
type Host struct {
name string
url string
}
//Hosts is a list of all hosts that the mobile app can connect to
type Hosts []Host
//CustomResponse allows for a customisable JSON response to the server
type CustomResponse struct {
Status string `json:"status"`
Message string `json:"message"`
}
//CheckForValidAuth checks the provided credentials against a provided host, returning the host-provided json
// if auth is valid, or 'false' if the credentials are not valid or if there's a server error (or any other issue)
func CheckForValidAuth(host Host, inputBody string) (json []byte, ok bool) {
var jsonStr = []byte(inputBody)
endpoint := fmt.Sprintf("%s%s", host.url, "/api/v1/auth/request_api_token/")
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, false
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
return nil, false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, false
}
return body, true
}
//ConcurrentRespnse adds concurrency capacity to check for valid auth, using the channel respCh to return the final json body
// Or setting the waitgroup to done if there is an error
func ConcurrentRespnse(host Host, inputBody string, respCh chan []byte, wg *sync.WaitGroup) {
respbody, ok := CheckForValidAuth(host, inputBody)
if !ok {
wg.Done()
return
}
var f interface{}
err := json.Unmarshal(respbody, &f)
if err != nil {
wg.Done()
return
}
m := f.(map[string]interface{})
m["host_name"] = host.name
jsonBod, err := json.Marshal(m)
if err != nil {
wg.Done()
return
}
respCh <- jsonBod
}
//HandleLambdaEvent is the main event handler for each auth request
func HandleLambdaEvent(
event events.APIGatewayProxyRequest,
hosts Hosts) (events.APIGatewayProxyResponse, error) {
headers := make(map[string]string)
headers["Access-Control-Allow-Origin"] = "https://www.withsempo.com"
headers["Access-Control-Allow-Headers"] = "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"
// The WaitGroup will trigger the waitchannel if all host requests fail
wg := sync.WaitGroup{}
waitCh := make(chan struct{})
// respchan will take the first successful host response
respCh := make(chan []byte)
// Launch all host requests concurrently
for _, h := range hosts {
wg.Add(1)
go ConcurrentRespnse(h, event.Body, respCh, &wg)
}
// Concurrent goroutine that blocks until all hosts report failure, and then triggers the waitchannel
go func() {
wg.Wait()
close(waitCh)
}()
// Either one of the hosts will send a response to the respCh, in which case return that response
// or waitCh will trigger from all the failed hosts, in which case return a failure
select {
case jsonBod := <-respCh:
// Return the first host that reports a success
return events.APIGatewayProxyResponse{
Body: string(jsonBod),
StatusCode: 200,
Headers: headers,
}, nil
case <-waitCh:
// Wait channel closed, which means all hosts reported a failure
failResp := CustomResponse{Status: "fail", Message: "Invalid username or password"}
authFailedBody, _ := json.Marshal(failResp)
return events.APIGatewayProxyResponse{
Body: string(authFailedBody),
StatusCode: 200,
Headers: headers,
}, nil
}
}
//PartialedHandleLambdaEvent shapes the event handler by injecting a host list and providing a context argument
func PartialedHandleLambdaEvent(
ctx context.Context,
event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return HandleLambdaEvent(event, ProductionHosts)
}
func main() {
lambda.Start(PartialedHandleLambdaEvent)
}