Require directory membership to view the directory

This commit is contained in:
Ian Gulliver
2026-08-16 11:27:00 -07:00
parent 366e3bb8a2
commit 4d70d6c2a8
4 changed files with 25 additions and 2 deletions
+14
View File
@@ -28,6 +28,20 @@ type app struct {
mapsKey string mapsKey string
} }
func MemberGate(cache *Cache, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/auth/login" || r.URL.Path == "/auth/logout" || strings.HasPrefix(r.URL.Path, "/static/") {
next.ServeHTTP(w, r)
return
}
if !cache.Model().Member(strings.ToLower(auth.Email(r))) {
http.Error(w, "account is not in the directory", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
func Register(mux *http.ServeMux, cache *Cache, mapsKey string) { func Register(mux *http.ServeMux, cache *Cache, mapsKey string) {
a := app{cache: cache, mapsKey: mapsKey} a := app{cache: cache, mapsKey: mapsKey}
for _, section := range sections { for _, section := range sections {
+9
View File
@@ -58,6 +58,15 @@ type Grade struct {
NextBand string `json:"nextBand,omitempty"` NextBand string `json:"nextBand,omitempty"`
} }
func (m *Model) Member(email string) bool {
for _, p := range m.People {
if p.Email == email {
return true
}
}
return false
}
func (m *Model) DisplayName(email string) string { func (m *Model) DisplayName(email string) string {
for _, p := range m.People { for _, p := range m.People {
if p.Email == email { if p.Email == email {
+1 -1
View File
@@ -118,5 +118,5 @@ func main() {
port = "8080" port = "8080"
} }
log.Printf("listening on http://localhost:%s", port) log.Printf("listening on http://localhost:%s", port)
log.Fatal(http.ListenAndServe(":"+port, noCache(authn.Wrap(mux)))) log.Fatal(http.ListenAndServe(":"+port, noCache(authn.Wrap(directory.MemberGate(cache, mux)))))
} }
+1 -1
View File
@@ -15,7 +15,7 @@ import (
) )
func main() { func main() {
email := flag.String("email", "dev@heliosschool.org", "session email for the minted cookie") email := flag.String("email", "ian.gulliver@heliosschool.org", "session email for the minted cookie")
flag.Parse() flag.Parse()
key := os.Getenv("SESSION_KEY") key := os.Getenv("SESSION_KEY")
if key == "" { if key == "" {