This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
query.go
146 lines (117 loc) · 3.55 KB
/
query.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
package handlers
import (
"crypto"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/openfaas/openfaas-cloud/sdk"
)
// MakeQueryHandler returns whether a client can access a resource
func MakeQueryHandler(config *Config, protected []string, restrictedPrefix []string) func(http.ResponseWriter, *http.Request) {
keydata, err := ioutil.ReadFile(config.PublicKeyPath)
if err != nil {
log.Fatalf("unable to read path: %s, error: %s", config.PublicKeyPath, err.Error())
}
publicKey, keyErr := jwt.ParseECPublicKeyFromPEM(keydata)
if keyErr != nil {
log.Fatalf("unable to parse public key: %s", keyErr.Error())
}
customersPath := os.Getenv("customers_path")
customersURL := os.Getenv("customers_url")
customers := sdk.NewCustomers(customersPath, customersURL)
if err := customers.Fetch(); err != nil {
log.Printf("Unable to fetch customers from configured source: %s", err)
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
}
return func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
resource := query.Get("r")
status := http.StatusOK
if len(resource) == 0 {
status = http.StatusBadRequest
} else if isProtected(resource, restrictedPrefix) {
status = http.StatusUnauthorized
} else if isProtected(resource, protected) {
started := time.Now()
cookieStatus := validCookie(r, cookieName, publicKey, customers, config.Debug)
log.Printf("Cookie verified: %fs [%d]", time.Since(started).Seconds(), cookieStatus)
switch cookieStatus {
case http.StatusOK:
status = http.StatusOK
break
case http.StatusNetworkAuthenticationRequired:
status = http.StatusTemporaryRedirect
log.Printf("No cookie or an invalid cookie was found.\n")
break
default:
log.Printf("A valid cookie was not found.\n")
status = http.StatusUnauthorized
break
}
}
log.Printf("Validate %s => %d\n", resource, status)
if status == http.StatusTemporaryRedirect {
var redirect *url.URL
switch config.OAuthProvider {
case gitlabName:
redirect = buildGitLabURL(config)
break
case githubName:
redirect = buildGitHubURL(config, "", config.Scope)
break
}
http.Redirect(w, r, redirect.String(), http.StatusTemporaryRedirect)
return
}
w.WriteHeader(status)
}
}
func isProtected(resource string, protected []string) bool {
for _, prefix := range protected {
if strings.HasPrefix(resource, prefix) {
return true
}
}
return false
}
func validCookie(r *http.Request, cookieName string, publicKey crypto.PublicKey, customers *sdk.Customers, debug bool) int {
cookie, err := r.Cookie(cookieName)
if err != nil {
return http.StatusNetworkAuthenticationRequired
}
claims := OpenFaaSCloudClaims{}
if len(cookie.Value) > 0 {
if debug {
log.Println("Cookie value: ", cookie.Value)
}
parsed, parseErr := jwt.ParseWithClaims(cookie.Value, &claims, func(token *jwt.Token) (interface{}, error) {
return publicKey, nil
})
if parseErr != nil {
log.Println(parseErr)
return http.StatusUnauthorized
}
if parsed.Valid {
if debug {
log.Println("Claims", claims)
log.Printf("Validated JWT for (%s) %s", claims.Subject, claims.Name)
}
if found, _ := customers.Get(claims.Subject); found == false {
log.Printf("user [%s] was not a valid customer", claims.Subject)
return http.StatusUnauthorized
}
if debug {
log.Printf("valid customer [%s]", claims.Subject)
}
return http.StatusOK
}
}
return http.StatusUnauthorized
}