// InjectTokenManager is a middleware responsible for setting up tokenManager in the context for every request.
427669
func InjectTokenManager(tokenManager token.Manager) goa.Middleware {
	return func(h goa.Handler) goa.Handler {
		return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
			ctxWithTM := tokencontext.ContextWithTokenManager(ctx, tokenManager)
			return h(ctxWithTM, rw, req)
		}
	}
}
1317817
func (a *Authenticator) authTokenInjector(req *http.Request) error {
	switch tok, err := a.GetAccessToken(minAcceptedLifetime); {
	case err == ErrLoginRequired && a.effectiveLoginMode() == OptionalLogin:
		return nil // skip auth, no need for modifications
	case err != nil:
		return err
	default:
		tok.SetAuthHeader(req)
		return nil
	}
}
1040479
func (em *EventManager) AttachManager(esm EventManagers) {
	//incase of stupid loops, are we attached to the supplied manager? if so duck this
	if esm.HasManager(em) {
		return
	}

	//if we have it already we skip
	if em.HasManager(esm) {
		return
	}

	//its not found so we add it
	em.wo.Lock()
	em.attaches[esm] = true
	em.wo.Unlock()

	//do we already have a dom attached?, then notify this manager immediately
	if em.dom != nil {
		esm.LoadDOM(em.dom)
	}
}
35173
func (im *IndexMappingImpl) AddCustomTokenFilter(name string, config map[string]interface{}) error {
	_, err := im.cache.DefineTokenFilter(name, config)
	if err != nil {
		return err
	}
	im.CustomAnalysis.TokenFilters[name] = config
	return nil
}
260930
func (e *InjectableEndpoint) Inject(protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) {
	e.dispatcher.DeliverNetworkPacket(e, "" /* remote */, "" /* local */, protocol, vv)
}
625511
func (p *TextMapPropagator) Inject(
	sc SpanContext,
	abstractCarrier interface{},
) error {
	textMapWriter, ok := abstractCarrier.(opentracing.TextMapWriter)
	if !ok {
		return opentracing.ErrInvalidCarrier
	}

	// Do not encode the string with trace context to avoid accidental double-encoding
	// if people are using opentracing < 0.10.0. Our colon-separated representation
	// of the trace context is already safe for HTTP headers.
	textMapWriter.Set(p.headerKeys.TraceContextHeaderName, sc.String())
	for k, v := range sc.baggage {
		safeKey := p.addBaggageKeyPrefix(k)
		safeVal := p.encodeValue(v)
		textMapWriter.Set(safeKey, safeVal)
	}
	return nil
}
92561
func Inject(script string, handler http.Handler) http.Handler {
	hfunc := func(w http.ResponseWriter, r *http.Request) {
		// TODO: accommodate encodings
		r.Header.Del("Accept-Encoding")

		iw := &injectWrapper{
			buf:        bytes.NewBuffer([]byte{}),
			status:     0,
			wr:         w,
			firstWrite: true,
			hijacked:   false,
		}

		// check for hijacker
		var wr http.ResponseWriter
		if hijacker, ok := w.(http.Hijacker); ok {
			wr = &injectWrapperHijacker{iw, hijacker}
		} else {
			wr = iw
		}

		// server request
		handler.ServeHTTP(wr, r)
		if iw.hijacked {
			return
		}

		// default to just writing the request
		b := iw.buf.Bytes()
		writeBody := func() { iw.wr.Write(b) }

		err := func() error {
			// only attempt to parse plain HTML
			get := func(key string) string { return iw.Header().Get(key) }
			if get("Content-Type") != "text/html" || get("Content-Encoding") != "" {
				return nil
			}

			// make a copy of the body
			bcp := make([]byte, len(b))
			copy(bcp, b)

			// attempt to inject the script
			root, err := injectScript(bytes.NewReader(bcp), script)
			if err != nil {
				return fmt.Errorf("middleware: error injecting script: %v", err)
			}

			// render the new html
			buf := bytes.NewBuffer([]byte{})
			if err = html.Render(buf, root); err != nil {
				return fmt.Errorf("middleware: could not render injected html: %v", err)
			}
			iw.Header().Set("Content-Length", strconv.Itoa(len(buf.Bytes())))

			// change the writeBody function to write the altered HTML
			writeBody = func() { io.Copy(iw.wr, buf) }
			return nil
		}()
		// print errors, don't return 500
		if err != nil {
			log.Println(err)
		}

		iw.WriteHeader(iw.status)
		writeBody()
	}
	return http.HandlerFunc(hfunc)
}
352770
func OAuth2TokenRevocationFactory(config *Config, storage interface{}, strategy interface{}) interface{} {
	return &oauth2.TokenRevocationHandler{
		TokenRevocationStorage: storage.(oauth2.TokenRevocationStorage),
		AccessTokenStrategy:    strategy.(oauth2.AccessTokenStrategy),
		RefreshTokenStrategy:   strategy.(oauth2.RefreshTokenStrategy),
	}
}
1183153
func (spt *ServicePrincipalToken) InvokeRefreshCallbacks(token Token) error {
	if spt.refreshCallbacks != nil {
		for _, callback := range spt.refreshCallbacks {
			err := callback(spt.inner.Token)
			if err != nil {
				return fmt.Errorf("adal: TokenRefreshCallback handler failed. Error = '%v'", err)
			}
		}
	}
	return nil
}
938954
func (pc *MockProviderContext) IdentityManager(orgName string) (msp.IdentityManager, bool) {
	mgr, ok := pc.identityManager[strings.ToLower(orgName)]
	return mgr, ok
}
929215
func NewEncrypterManager(key []byte, iv []byte) (manager *EncrypterManager, err error) {
	var encrypter Encrypter
	manager = new(EncrypterManager)
	switch len(iv) {
	case 12:
		// ChaCha20
		encrypter, err = crypto.NewChaChaEncrypter(key, iv)
	case 16:
		// AES
		encrypter, err = crypto.NewAESEncrypter(key, iv)
	default:
		return nil, ErrUnsupportedEncrypterType
	}
	manager.Encrypter = encrypter
	return
}
160529
func (o Middleware) SubscribeInterceptor(opts pubsub.HandlerOptions, next pubsub.MsgHandler) pubsub.MsgHandler {
	return func(ctx context.Context, m pubsub.Msg) error {
		ctx = context.WithValue(ctx, "x-audit-user", m.Metadata["x-audit-user"])
		ctx = metadata.NewIncomingContext(ctx, metadata.New(m.Metadata))
		return next(ctx, m)
	}
}
1129190
func WithIdentityManagerProvider(provider msp.IdentityManagerProvider) SDKContextParams {
	return func(ctx *Provider) {
		ctx.idMgmtProvider = provider
	}
}
1083300
func (r *Registry) OptionManager() *OptionManager {
	return r.Get(r.content().Setting.Reference()).(*OptionManager)
}
251873
func (c *BaseCommand) TokenHelper() (token.TokenHelper, error) {
	if c.tokenHelper != nil {
		return c.tokenHelper, nil
	}

	helper, err := DefaultTokenHelper()
	if err != nil {
		return nil, err
	}
	return helper, nil
}
115190
func (self *authenticateeProcess) installHandlers(ctx context.Context) error {

	type handlerFn func(ctx context.Context, from *upid.UPID, pbMsg proto.Message)

	withContext := func(f handlerFn) messenger.MessageHandler {
		return func(from *upid.UPID, m proto.Message) {
			status := (&self.status).get()
			if self.from != nil && !self.from.Equal(from) {
				self.terminate(status, statusError, UnexpectedAuthenticatorPid)
			} else {
				f(withStatus(ctx, status), from, m)
			}
		}
	}

	// Anticipate mechanisms and steps from the server
	handlers := []struct {
		f handlerFn
		m proto.Message
	}{
		{self.mechanisms, &mesos.AuthenticationMechanismsMessage{}},
		{self.step, &mesos.AuthenticationStepMessage{}},
		{self.completed, &mesos.AuthenticationCompletedMessage{}},
		{self.failed, &mesos.AuthenticationFailedMessage{}},
		{self.errored, &mesos.AuthenticationErrorMessage{}},
	}
	for _, h := range handlers {
		if err := self.transport.Install(withContext(h.f), h.m); err != nil {
			return err
		}
	}
	return nil
}
750046
func TokenHandler(cb AuthTokenHandler) Option {
	return func(o *Options) error {
		if o.Token != "" {
			return ErrTokenAlreadySet
		}
		o.TokenHandler = cb
		return nil
	}
}