You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: introspection_response_writer.go
    path of file: ./repos/fosite
    
    the code of the file until where you have to start completion: // Copyright Â© 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package fosite

import (
	"context"
	"encoding/json"
	"net/http"
	"strings"

	"github.com/pkg/errors"
)

// WriteIntrospectionError responds with token metadata discovered by token introspection as defined in
// https://tools.ietf.org/search/rfc7662#section-2.2
//
// If the protected resource uses OAuth 2.0 client credentials to
// authenticate to the introspection endpoint and its credentials are
// invalid, the authorization server responds with an HTTP 401
// (Unauthorized) as described in Section 5.2 of OAuth 2.0 [RFC6749].
//
// If the protected resource uses an OAuth 2.0 bearer token to authorize
// its call to the introspection endpoint and the token used for
// authorization does not contain s
    
func (f *Fosite) WriteIntrospectionResponse(ctx context.Context, rw http.ResponseWriter, r IntrospectionResponder) {
	if !r.IsActive() {
		_ = json.NewEncoder(rw).Encode(&struct {
			Active bool `json:"active"`
		}{Active: false})
		return
	}

	response := map[string]interface{}{
		"active": true,
	}

	extraClaimsSession, ok := r.GetAccessRequester().GetSession().(ExtraClaimsSession)
	if ok {
		extraClaims := extraClaimsSession.GetExtraClaims()
		for name, value := range extraClaims {
			switch name {
			// We do not allow these to be set through extra claims.
			case "exp", "client_id", "scope", "iat", "sub", "aud", "username":
				continue
			default:
				response[name] = value
			}
		}
	}

	if !r.GetAccessRequester().GetSession().GetExpiresAt(AccessToken).IsZero() {
		response["exp"] = r.GetAccessRequester().GetSession().GetExpiresAt(AccessToken).Unix()
	}
	if r.GetAccessRequester().GetClient().GetID() != "" {
		response["client_id"] = r.GetAccessRequester().GetClient().GetID()
	}
	if len(r.GetAccessRequester().GetGrantedScopes()) > 0 {
		response["scope"] = strings.Join(r.GetAccessRequester().GetGrantedScopes(), " ")
	}
	if !r.GetAccessRequester().GetRequestedAt().IsZero() {
		response["iat"] = r.GetAccessRequester().GetRequestedAt().Unix()
	}
	if r.GetAccessRequester().GetSession().GetSubject() != "" {
		response["sub"] = r.GetAccessRequester().GetSession().GetSubject()
	}
	if len(r.GetAccessRequester().GetGrantedAudience()) > 0 {
		response["aud"] = r.GetAccessRequester().GetGrantedAudience()
	}
	if r.GetAccessRequester().GetSession().GetUsername() != "" {
		response["username"] = r.GetAccessRequester().GetSession().GetUsername()
	}

	rw.Header().Set("Content-Type", "application/json;charset=UTF-8")
	rw.Header().Set("Cache-Control", "no-store")
	rw.Header().Set("Pragma", "no-cache")
	_ = json.NewEncoder(rw).Encode(response)
}