<commit_before>package oas

import (
	"reflect"

	"github.com/lonelycode/osin"

	"github.com/TykTechnologies/tyk/apidef"
)

type Authentication struct {
	Enabled                bool   `bson:"enabled" json:"enabled"` // required
	StripAuthorizationData bool   `bson:"stripAuthorizationData,omitempty" json:"stripAuthorizationData,omitempty"`
	Token                  *Token `bson:"token,omitempty" json:"token,omitempty"`
	JWT                    *JWT   `bson:"jwt,omitempty" json:"jwt,omitempty"`
	Basic                  *Basic `bson:"basic,omitempty" json:"basic,omitempty"`
	OAuth                  *OAuth `bson:"oauth,omitempty" json:"oauth,omitempty"`
	HMAC                   *HMAC  `bson:"hmac,omitempty" json:"hmac,omitempty"`
}

func (a *Authentication) Fill(api apidef.APIDefinition) {
	a.Enabled = !api.UseKeylessAccess
	a.StripAuthorizationData = api.StripAuthData

	if api.AuthConfigs == nil || len(api.AuthConfigs) == 0 {
		return
	}

	if authToken, ok := api.AuthConfigs["authToken"]; ok {
		if a.Token == nil {
			a.Token = &Token{}
		}

		a.Token.Fill(api.UseStandardAuth, authToken)
	}

	if reflect.DeepEqual(a.Token, &Token{}) {
		a.Token = nil
	}

	if _, ok := api.AuthConfigs["jwt"]; ok {
		if a.JWT == nil {
			a.JWT = &JWT{}
		}

		a.JWT.Fill(api)
	}

	if reflect.DeepEqual(a.JWT, &JWT{}) {
		a.JWT = nil
	}

	if _, ok := api.AuthConfigs["basic"]; ok {
		if a.Basic == nil {
			a.Basic = &Basic{}
		}

		a.Basic.Fill(api)
	}

	if reflect.DeepEqual(a.Basic, &Basic{}) {
		a.Basic = nil
	}

	if _, ok := api.AuthConfigs["oauth"]; ok {
		if a.OAuth == nil {
			a.OAuth = &OAuth{}
		}

		a.OAuth.Fill(api)
	}

	if reflect.DeepEqual(a.OAuth, &OAuth{}) {
		a.OAuth = nil
	}

	if _, ok := api.AuthConfigs["hmac"]; ok {
		if a.HMAC == nil {
			a.HMAC = &HMAC{}
		}

		a.HMAC.Fill(api)
	}

	if reflect.DeepEqual(a.HMAC, &HMAC{}) {
		a.HMAC = nil
	}
}

func (a *Authentication) ExtractTo(api *apidef.APIDefinition) {
	api.UseKeylessAccess = !a.Enabled
	api.StripAuthData = a.StripAuthorizationData

	if a.Token != nil {
		a.Token.ExtractTo(api)
	}

	if a.JWT != nil {
		a.JWT.ExtractTo(api)
	}
<commit_msg>Implement multi auth OAS conversion (#3711)<commit_after>package oas

import (
	"reflect"

	"github.com/lonelycode/osin"

	"github.com/TykTechnologies/tyk/apidef"
)

type Authentication struct {
	Enabled                bool                `bson:"enabled" json:"enabled"` // required
	StripAuthorizationData bool                `bson:"stripAuthorizationData,omitempty" json:"stripAuthorizationData,omitempty"`
	BaseIdentityProvider   apidef.AuthTypeEnum `bson:"baseIdentityProvider,omitempty" json:"baseIdentityProvider,omitempty"`
	Token                  *Token              `bson:"token,omitempty" json:"token,omitempty"`
	JWT                    *JWT                `bson:"jwt,omitempty" json:"jwt,omitempty"`
	Basic                  *Basic              `bson:"basic,omitempty" json:"basic,omitempty"`
	OAuth                  *OAuth              `bson:"oauth,omitempty" json:"oauth,omitempty"`
	HMAC                   *HMAC               `bson:"hmac,omitempty" json:"hmac,omitempty"`
}

func (a *Authentication) Fill(api apidef.APIDefinition) {
	a.Enabled = !api.UseKeylessAccess
	a.StripAuthorizationData = api.StripAuthData
	a.BaseIdentityProvider = api.BaseIdentityProvidedBy

	if api.AuthConfigs == nil || len(api.AuthConfigs) == 0 {
		return
	}

	if authToken, ok := api.AuthConfigs["authToken"]; ok {
		if a.Token == nil {
			a.Token = &Token{}
		}

		a.Token.Fill(api.UseStandardAuth, authToken)
	}

	if reflect.DeepEqual(a.Token, &Token{}) {
		a.Token = nil
	}

	if _, ok := api.AuthConfigs["jwt"]; ok {
		if a.JWT == nil {
			a.JWT = &JWT{}
		}

		a.JWT.Fill(api)
	}

	if reflect.DeepEqual(a.JWT, &JWT{}) {
		a.JWT = nil
	}

	if _, ok := api.AuthConfigs["basic"]; ok {
		if a.Basic == nil {
			a.Basic = &Basic{}
		}

		a.Basic.Fill(api)
	}

	if reflect.DeepEqual(a.Basic, &Basic{}) {
		a.Basic = nil
	}

	if _, ok := api.AuthConfigs["oauth"]; ok {
		if a.OAuth == nil {
			a.OAuth = &OAuth{}
		}

		a.OAuth.Fill(api)
	}

	if reflect.DeepEqual(a.OAuth, &OAuth{}) {
		a.OAuth = nil
	}

	if _, ok := api.AuthConfigs["hmac"]; ok {
		if a.HMAC == nil {
			a.HMAC = &HMAC{}
		}

		a.HMAC.Fill(api)
	}

	if reflect.DeepEqual(a.HMAC, &HMAC{}) {
		a.HMAC = nil
	}
}

func (a *Authentication) ExtractTo(api *apidef.APIDefinition) {
	api.UseKeylessAccess = !a.Enabled
	api.StripAuthData = a.StripAuthorizationData
	api.BaseIdentityProvidedBy = a.BaseIdentityProvider

	if a.Token != nil {
		a.Token.ExtractTo(api)
	}

	if a.JWT != nil {
		a.JWT.ExtractTo(api)
	}

{'fraction_non_alphanumeric': 0.15381254096569805, 'fraction_numerical': 0.0013109023377758358, 'mean_word_length': 2.909479077711358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7458831', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1719, 'n_words': 307}