forked from coduno/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (125 loc) · 5.21 KB
/
main.go
File metadata and controls
155 lines (125 loc) · 5.21 KB
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
package main
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/coduno/api/controllers"
"github.com/coduno/api/util/passenger"
"github.com/coduno/api/ws"
"github.com/gorilla/mux"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
// ContextHandlerFunc is similar to a http.HandlerFunc, but also gets passed
// the current context.
// To ease error handling, a ContextHandleFunc must return a HTTP status
// code and an error. Still, the handler is allowed to write a response.
type ContextHandlerFunc func(context.Context, http.ResponseWriter, *http.Request) (int, error)
func main() {
go http.ListenAndServe(":8090", http.HandlerFunc(ws.Handle))
http.HandleFunc("/_ah/mail/", controllers.ReceiveMail)
http.HandleFunc("/cert", hsts(controllers.Certificate))
http.HandleFunc("/status", hsts(controllers.Status))
r := mux.NewRouter()
r.HandleFunc("/subscriptions", hsts(controllers.Subscriptions))
r.HandleFunc("/code/download", hsts(guard(controllers.Template)))
r.HandleFunc("/invitations", setup(controllers.Invitation))
r.HandleFunc("/tokens", setup(controllers.Tokens))
r.HandleFunc("/tokens/collect", setup(controllers.CollectTokens))
r.HandleFunc("/challenges", setup(controllers.CreateChallenge))
r.HandleFunc("/challenges/{key}", setup(controllers.ChallengeByKey))
r.HandleFunc("/challenges/{key}/results", setup(controllers.GetResultsByChallenge))
r.HandleFunc("/companies", setup(controllers.PostCompany))
r.HandleFunc("/companies/{key}/challenges", setup(controllers.GetChallengesForCompany))
r.HandleFunc("/companies/{key}/users", setup(controllers.GetUsersByCompany))
r.HandleFunc("/mock", controllers.Mock)
r.HandleFunc("/profiles/{key}", setup(controllers.GetProfileByKey))
r.HandleFunc("/profiles/{key}", setup(controllers.DeleteProfile))
r.HandleFunc("/profiles/{key}/challenges", setup(controllers.GetChallengesForProfile))
r.HandleFunc("/results", setup(controllers.CreateResult))
r.HandleFunc("/results/{resultKey}/tasks/{taskKey}/submissions", setup(controllers.PostSubmission))
r.HandleFunc("/results/{resultKey}/finalSubmissions/{index}", setup(controllers.FinalSubmission))
r.HandleFunc("/results/{resultKey}", setup(controllers.GetResult))
r.HandleFunc("/user/company", setup(controllers.GetCompanyByUser))
r.HandleFunc("/user", setup(controllers.WhoAmI))
r.HandleFunc("/users", setup(controllers.User))
r.HandleFunc("/users/{key}", setup(controllers.GetUser))
r.HandleFunc("/users/{key}/profile", setup(controllers.GetProfileForUser))
r.HandleFunc("/tasks/{key}", setup(controllers.TaskByKey))
r.HandleFunc("/tasks", setup(controllers.Tasks))
r.HandleFunc("/whoami", setup(controllers.WhoAmI))
http.Handle("/", r)
appengine.Main()
}
// hsts is a basic wrapper that takes care of tightly timed HSTS for all requests.
// All outbound flows should be wrapped.
func hsts(h http.HandlerFunc) http.HandlerFunc {
if appengine.IsDevAppServer() {
return h
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Protect against HTTP downgrade attacks by explicitly telling
// clients to use HTTPS.
// max-age is computed to match the expiration date of our TLS
// certificate.
// https://developer.mozilla.org/docs/Web/Security/HTTP_strict_transport_security
// This is only set on production.
invalidity := time.Date(2017, time.July, 15, 8, 30, 21, 0, time.UTC)
maxAge := invalidity.Sub(time.Now()).Seconds()
w.Header().Set("Strict-Transport-Security", fmt.Sprintf("max-age=%d", int(maxAge)))
h(w, r)
})
}
// Rudimentary CORS checking. See
// https://developer.mozilla.org/docs/Web/HTTP/Access_control_CORS
func cors(h http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if !appengine.IsDevAppServer() {
if origin == "" {
h(w, r)
return
}
if !strings.HasPrefix(origin, "https://app.cod.uno") {
http.Error(w, "Invalid CORS request", http.StatusUnauthorized)
return
}
}
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" {
w.Write([]byte("OK"))
return
}
h(w, r)
})
}
// auth is there to associate a user with the incoming request.
func auth(h ContextHandlerFunc) ContextHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
ctx, err = passenger.NewContextFromRequest(ctx, r)
if err != nil {
log.Warningf(ctx, "auth: "+err.Error())
}
return h(ctx, w, r)
}
}
func guard(h ContextHandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, err := h(appengine.NewContext(r), w, r)
if err != nil {
http.Error(w, err.Error(), status)
} else if status >= 400 {
http.Error(w, http.StatusText(status), status)
}
})
}
// setup is the default wrapper for any ContextHandlerFunc that talks to
// the outside. It will wrap h in scure, cors and auth.
func setup(h ContextHandlerFunc) http.HandlerFunc {
return hsts(cors(guard(auth(h))))
}