Go API Documentation

github.com/TykTechnologies/tyk/test/goplugins

No package summary is available.

Package

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Functions

func MyAnalyticsPluginDeleteHeader

func MyAnalyticsPluginDeleteHeader(record *analytics.AnalyticsRecord) {
	str, err := base64.StdEncoding.DecodeString(record.RawResponse)
	if err != nil {
		return
	}

	var b = &bytes.Buffer{}
	b.Write(str)

	r := bufio.NewReader(b)
	var resp *http.Response
	resp, err = http.ReadResponse(r, nil)
	if err != nil {
		return
	}
	resp.Header.Del("Server")
	var bNew bytes.Buffer
	_ = resp.Write(&bNew)
	record.RawResponse = base64.StdEncoding.EncodeToString(bNew.Bytes())
}

Cognitive complexity: 5, Cyclomatic complexity: 3

Uses: base64.StdEncoding, bufio.NewReader, bytes.Buffer, http.ReadResponse, http.Response.

func MyAnalyticsPluginMaskJSONLoginBody

func MyAnalyticsPluginMaskJSONLoginBody(record *analytics.AnalyticsRecord) {
	if record.ContentLength < 1 {
		return
	}
	d, err := base64.StdEncoding.DecodeString(record.RawRequest)
	if err != nil {
		return
	}
	var mask = []byte("\"****\"")
	const endOfHeaders = "\r\n\r\n"
	paths := [][]string{
		{"email"},
		{"password"},
		{"data", "email"},
		{"data", "password"},
	}
	if i := bytes.Index(d, []byte(endOfHeaders)); i > 0 || (i+4) < len(d) {
		body := d[i+4:]
		jsonparser.EachKey(body, func(idx int, _ []byte, _ jsonparser.ValueType, _ error) {
			body, _ = jsonparser.Set(body, mask, paths[idx]...)
		}, paths...)

		record.RawRequest = base64.StdEncoding.EncodeToString(append(d[:i+4], body...))
	}
}

Cognitive complexity: 12, Cyclomatic complexity: 5

Uses: base64.StdEncoding, bytes.Index, jsonparser.EachKey, jsonparser.Set, jsonparser.ValueType.

func MyPluginAccessingOASAPI

func MyPluginAccessingOASAPI(rw http.ResponseWriter, r *http.Request) {
	oas := ctx.GetOASDefinition(r)
	rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
	rw.Header().Add("X-My-Plugin-Accessing-OAS-API", oas.Info.Title)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: ctx.GetOASDefinition.

func MyPluginApplyingPolicy

func MyPluginApplyingPolicy(rw http.ResponseWriter, r *http.Request) {
	session := &user.SessionState{
		KeyID:		"my-key",
		ApplyPolicies:	[]string{"my-pol"},
	}

	ctx.SetSession(r, session, true)
}

Cognitive complexity: 2, Cyclomatic complexity: 1

Uses: ctx.SetSession, user.SessionState.

func MyPluginAuthCheck

MyPluginAuthCheck does custom auth and will be used as "auth_check" custom MW

func MyPluginAuthCheck(rw http.ResponseWriter, r *http.Request) {
	// perform auth (only one token "abc" is allowed)
	token := r.Header.Get(header.Authorization)
	if token != "abc" {
		rw.Header().Add(header.XAuthResult, "failed")
		rw.WriteHeader(http.StatusForbidden)
		_, _ = rw.Write([]byte("auth failed"))
		return
	}

	// create session
	session := &user.SessionState{
		OrgID:	"default",
		Alias:	"abc-session",
		KeyID:	token,
	}

	ctx.SetSession(r, session, true, true)
	rw.Header().Add(header.XAuthResult, "OK")
}

Cognitive complexity: 3, Cyclomatic complexity: 2

Uses: ctx.SetSession, header.Authorization, header.XAuthResult, http.StatusForbidden, user.SessionState.

func MyPluginPerPathBar

func MyPluginPerPathBar(rw http.ResponseWriter, r *http.Request) {
	rw.Header().Add("X-bar", "bar")

}

Cognitive complexity: 0, Cyclomatic complexity: 1

func MyPluginPerPathFoo

func MyPluginPerPathFoo(rw http.ResponseWriter, r *http.Request) {

	rw.Header().Add("X-foo", "foo")

}

Cognitive complexity: 0, Cyclomatic complexity: 1

func MyPluginPerPathResp

func MyPluginPerPathResp(rw http.ResponseWriter, r *http.Request) {
	// prepare data to send
	replyData := map[string]string{
		"current_time": "now",
	}

	jsonData, err := json.Marshal(replyData)
	if err != nil {
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	// send HTTP response from Golang plugin
	rw.Header().Set("Content-Type", "application/json")
	rw.WriteHeader(http.StatusOK)
	rw.Write(jsonData)
}

Cognitive complexity: 3, Cyclomatic complexity: 2

Uses: http.StatusInternalServerError, http.StatusOK, json.Marshal.

func MyPluginPost

MyPluginPost prepares and sends reply, will be used as "post" custom MW

func MyPluginPost(rw http.ResponseWriter, r *http.Request) {

	replyData := map[string]interface{}{
		"message": "post message",
	}

	jsonData, err := json.Marshal(replyData)
	if err != nil {
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	apiDefinition := ctx.GetDefinition(r)
	if apiDefinition == nil {
		rw.Header().Add("X-Plugin-Data", "null")
	} else {
		pluginConfig, ok := apiDefinition.ConfigData["my-context-data"].(string)
		if !ok || pluginConfig == "" {
			rw.Header().Add("X-Plugin-Data", "null")
		} else {
			rw.Header().Add("X-Plugin-Data", pluginConfig)
		}

	}
	rw.Header().Set(header.ContentType, header.ApplicationJSON)
	rw.WriteHeader(http.StatusOK)
	rw.Write(jsonData)
}

Cognitive complexity: 12, Cyclomatic complexity: 5

Uses: ctx.GetDefinition, header.ApplicationJSON, header.ContentType, http.StatusInternalServerError, http.StatusOK, json.Marshal.

func MyPluginPostKeyAuth

MyPluginPostKeyAuth checks if session is present, adds custom header with session-alias and will be used as "post_key_auth" custom MW

func MyPluginPostKeyAuth(rw http.ResponseWriter, r *http.Request) {
	session := ctx.GetSession(r)
	if session == nil {
		rw.Header().Add(header.XSessionAlias, "not found")
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	rw.Header().Add(header.XSessionAlias, session.Alias)
}

Cognitive complexity: 2, Cyclomatic complexity: 2

Uses: ctx.GetSession, header.XSessionAlias, http.StatusInternalServerError.

func MyPluginPre

MyPluginPre checks if session is NOT present, adds custom header with initial URI path and will be used as "pre" custom MW

func MyPluginPre(rw http.ResponseWriter, r *http.Request) {
	session := ctx.GetSession(r)
	if session != nil {
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	rw.Header().Add(header.XInitialURI, r.URL.RequestURI())
}

Cognitive complexity: 2, Cyclomatic complexity: 2

Uses: ctx.GetSession, header.XInitialURI, http.StatusInternalServerError.

func MyPluginResponse

MyPluginResponse intercepts response from upstream which we can then manipulate

func MyPluginResponse(rw http.ResponseWriter, res *http.Response, req *http.Request) {

	res.Header.Add("X-Response-Added", "resp-added")

	var buf bytes.Buffer

	buf.Write([]byte(`{"message":"response injected message"}`))

	res.Body = ioutil.NopCloser(&buf)

	apiDefinition := ctx.GetDefinition(req)
	if apiDefinition == nil {
		res.Header.Add("X-Plugin-Data", "null")
		return
	}
	pluginConfig, ok := apiDefinition.ConfigData["my-context-data"].(string)
	if !ok || pluginConfig == "" {
		res.Header.Add("X-Plugin-Data", "null")
		return
	}
	res.Header.Add("X-Plugin-Data", pluginConfig)
}

Cognitive complexity: 5, Cyclomatic complexity: 4

Uses: bytes.Buffer, ctx.GetDefinition, ioutil.NopCloser.

func MyPluginReturningError

func MyPluginReturningError(rw http.ResponseWriter, r *http.Request) {
	rw.WriteHeader(http.StatusTeapot)
	_, _ = rw.Write([]byte(http.StatusText(http.StatusTeapot)))
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: http.StatusTeapot, http.StatusText.

func MyResponsePluginAccessingOASAPI

MyResponsePluginAccessingOASAPI fake plugin which modifies data

func MyResponsePluginAccessingOASAPI(rw http.ResponseWriter, _ *http.Response, req *http.Request) {
	oas := ctx.GetOASDefinition(req)
	rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
	rw.Header().Add("X-My-Response-Plugin-Accessing-OAS-API", oas.Info.Title)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: ctx.GetOASDefinition.

func MyResponsePluginAccessingOASAPI2

MyResponsePluginAccessingOASAPI2 fake plugin which modifies headers

func MyResponsePluginAccessingOASAPI2(rw http.ResponseWriter, _ *http.Response, req *http.Request) {
	oas := ctx.GetOASDefinition(req)
	rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
	rw.Header().Add("X-My-Response-Plugin-Accessing-OAS-API-2", oas.Info.Title)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: ctx.GetOASDefinition.

Tests

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.