github.com/louisevanderlith/droxolite@v1.20.2/open/ghost.go (about)

     1  package open
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  	"golang.org/x/oauth2/clientcredentials"
     6  	"net/http"
     7  )
     8  
     9  func NewGhostware(cfg *clientcredentials.Config) ghostprotector {
    10  	return ghostprotector{cfg: cfg}
    11  }
    12  
    13  type ghostprotector struct {
    14  	cfg *clientcredentials.Config
    15  }
    16  
    17  func (g ghostprotector) GhostMiddleware(next http.HandlerFunc) http.HandlerFunc {
    18  	return func(w http.ResponseWriter, r *http.Request) {
    19  		tkn, err := g.cfg.Token(r.Context())
    20  
    21  		if err != nil {
    22  			panic(err)
    23  		}
    24  
    25  		acc := context.WithValue(r.Context(), "Token", *tkn)
    26  		next.ServeHTTP(w, r.WithContext(acc))
    27  	}
    28  }