Copyright 2019 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package middleware

import (
	
	
	
	

	
)
ValidateIAPHeader checks that the request has a header that proves it arrived via the IAP. See https://cloud.google.com/iap/docs/signed-headers-howto#securing_iap_headers.
func ( string) Middleware {
	return func( http.Handler) http.Handler {
Health checks don't come from the IAP; allow them.
Adapted from https://github.com/GoogleCloudPlatform/golang-samples/blob/master/iap/validate.go
				 := .Header.Get("X-Goog-IAP-JWT-Assertion")
				if  := validateIAPToken(.Context(), , );  != nil {
					http.Error(, .Error(), http.StatusUnauthorized)
					return
				}
			}
			.ServeHTTP(, )
		})
	}
}

func ( context.Context, ,  string) error {
	if  == "" {
		return errors.New("missing IAP token")
	}
	if ,  := idtoken.Validate(, , );  != nil {
		return fmt.Errorf("validating IPA token: %v", )
	}
	return nil