github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/auth/converter.go (about)

     1  package auth
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/model"
     7  	"github.com/kyma-incubator/compass/components/director/internal/tokens"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // TokenConverter missing godoc
    14  //go:generate mockery --name=TokenConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    15  type TokenConverter interface {
    16  	ToGraphQLForApplication(model model.OneTimeToken) (graphql.OneTimeTokenForApplication, error)
    17  }
    18  
    19  type converter struct {
    20  }
    21  
    22  type converterOTT struct {
    23  	*converter
    24  	tokenConverter TokenConverter
    25  }
    26  
    27  // NewConverterWithOTT is meant to be used for converting system auth with the one time token also converted
    28  func NewConverterWithOTT(tokenConverter TokenConverter) *converterOTT {
    29  	return &converterOTT{
    30  		converter:      &converter{},
    31  		tokenConverter: tokenConverter,
    32  	}
    33  }
    34  
    35  // NewConverter missing godoc
    36  func NewConverter() *converter {
    37  	return &converter{}
    38  }
    39  
    40  // ToGraphQL missing godoc
    41  func (c *converter) ToGraphQL(in *model.Auth) (*graphql.Auth, error) {
    42  	if in == nil {
    43  		return nil, nil
    44  	}
    45  
    46  	var headers graphql.HTTPHeaders
    47  	var headersSerialized *graphql.HTTPHeadersSerialized
    48  	if len(in.AdditionalHeaders) != 0 {
    49  		headers = in.AdditionalHeaders
    50  
    51  		serialized, err := graphql.NewHTTPHeadersSerialized(in.AdditionalHeaders)
    52  		if err != nil {
    53  			return nil, errors.Wrap(err, "while marshaling AdditionalHeaders")
    54  		}
    55  		headersSerialized = &serialized
    56  	}
    57  
    58  	var params graphql.QueryParams
    59  	var paramsSerialized *graphql.QueryParamsSerialized
    60  	if len(in.AdditionalQueryParams) != 0 {
    61  		params = in.AdditionalQueryParams
    62  
    63  		serialized, err := graphql.NewQueryParamsSerialized(in.AdditionalQueryParams)
    64  		if err != nil {
    65  			return nil, errors.Wrap(err, "while marshaling AdditionalQueryParams")
    66  		}
    67  		paramsSerialized = &serialized
    68  	}
    69  
    70  	return &graphql.Auth{
    71  		Credential:                      c.credentialToGraphQL(in.Credential),
    72  		AccessStrategy:                  in.AccessStrategy,
    73  		AdditionalHeaders:               headers,
    74  		AdditionalHeadersSerialized:     headersSerialized,
    75  		AdditionalQueryParams:           params,
    76  		AdditionalQueryParamsSerialized: paramsSerialized,
    77  		RequestAuth:                     c.requestAuthToGraphQL(in.RequestAuth),
    78  		CertCommonName:                  &in.CertCommonName,
    79  	}, nil
    80  }
    81  
    82  func (c *converterOTT) ToGraphQL(in *model.Auth) (*graphql.Auth, error) {
    83  	auth, err := c.converter.ToGraphQL(in)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	if auth == nil {
    88  		return nil, nil
    89  	}
    90  
    91  	if in.OneTimeToken != nil && in.OneTimeToken.Type == tokens.ApplicationToken {
    92  		oneTimeToken, err := c.tokenConverter.ToGraphQLForApplication(*in.OneTimeToken)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		auth.OneTimeToken = &oneTimeToken
    97  	}
    98  
    99  	return auth, nil
   100  }
   101  
   102  // InputFromGraphQL missing godoc
   103  func (c *converter) InputFromGraphQL(in *graphql.AuthInput) (*model.AuthInput, error) {
   104  	if in == nil {
   105  		return nil, nil
   106  	}
   107  
   108  	credential := c.credentialInputFromGraphQL(in.Credential)
   109  
   110  	additionalHeaders, err := c.headersFromGraphQL(in.AdditionalHeaders, in.AdditionalHeadersSerialized)
   111  	if err != nil {
   112  		return nil, errors.Wrap(err, "while converting AdditionalHeaders from GraphQL input")
   113  	}
   114  
   115  	additionalQueryParams, err := c.queryParamsFromGraphQL(in.AdditionalQueryParams, in.AdditionalQueryParamsSerialized)
   116  	if err != nil {
   117  		return nil, errors.Wrap(err, "while converting AdditionalQueryParams from GraphQL input")
   118  	}
   119  
   120  	reqAuth, err := c.requestAuthInputFromGraphQL(in.RequestAuth)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	return &model.AuthInput{
   126  		Credential:            credential,
   127  		AccessStrategy:        in.AccessStrategy,
   128  		AdditionalHeaders:     additionalHeaders,
   129  		AdditionalQueryParams: additionalQueryParams,
   130  		RequestAuth:           reqAuth,
   131  	}, nil
   132  }
   133  
   134  // ModelFromGraphQLTokenInput converts a graphql.OneTimeTokenInput to model.OneTimeToken
   135  func (c *converter) ModelFromGraphQLTokenInput(in *graphql.OneTimeTokenInput) *model.OneTimeToken {
   136  	if in == nil {
   137  		return nil
   138  	}
   139  
   140  	var connectorURL string
   141  	if in.ConnectorURL != nil {
   142  		connectorURL = *in.ConnectorURL
   143  	}
   144  
   145  	var tokenType tokens.TokenType
   146  	if in.Type != nil {
   147  		tokenType = tokens.TokenType(*in.Type)
   148  	}
   149  
   150  	return &model.OneTimeToken{
   151  		Token:        in.Token,
   152  		ConnectorURL: connectorURL,
   153  		Type:         tokenType,
   154  		CreatedAt:    timestampToTime(in.CreatedAt),
   155  		UsedAt:       timestampToTime(in.UsedAt),
   156  		ExpiresAt:    timestampToTime(in.ExpiresAt),
   157  		Used:         in.Used,
   158  	}
   159  }
   160  
   161  // ModelFromGraphQLInput converts a graphql.AuthInput to model.Auth
   162  func (c *converter) ModelFromGraphQLInput(in graphql.AuthInput) (*model.Auth, error) {
   163  	credential := c.credentialModelFromGraphQL(in.Credential)
   164  
   165  	additionalHeaders, err := c.headersFromGraphQL(in.AdditionalHeaders, in.AdditionalHeadersSerialized)
   166  	if err != nil {
   167  		return nil, errors.Wrap(err, "while converting AdditionalHeaders from GraphQL input")
   168  	}
   169  
   170  	additionalQueryParams, err := c.queryParamsFromGraphQL(in.AdditionalQueryParams, in.AdditionalQueryParamsSerialized)
   171  	if err != nil {
   172  		return nil, errors.Wrap(err, "while converting AdditionalQueryParams from GraphQL input")
   173  	}
   174  
   175  	reqAuth, err := c.requestAuthFromGraphQL(in.RequestAuth)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	return &model.Auth{
   181  		OneTimeToken:          c.ModelFromGraphQLTokenInput(in.OneTimeToken),
   182  		Credential:            credential,
   183  		AccessStrategy:        in.AccessStrategy,
   184  		AdditionalHeaders:     additionalHeaders,
   185  		AdditionalQueryParams: additionalQueryParams,
   186  		RequestAuth:           reqAuth,
   187  		CertCommonName:        str.PtrStrToStr(in.CertCommonName),
   188  	}, nil
   189  }
   190  
   191  func (c *converter) requestAuthToGraphQL(in *model.CredentialRequestAuth) *graphql.CredentialRequestAuth {
   192  	if in == nil {
   193  		return nil
   194  	}
   195  
   196  	var csrf *graphql.CSRFTokenCredentialRequestAuth
   197  	if in.Csrf != nil {
   198  		var headers graphql.HTTPHeaders
   199  		if len(in.Csrf.AdditionalHeaders) != 0 {
   200  			headers = in.Csrf.AdditionalHeaders
   201  		}
   202  
   203  		var params graphql.QueryParams
   204  		if len(in.Csrf.AdditionalQueryParams) != 0 {
   205  			params = in.Csrf.AdditionalQueryParams
   206  		}
   207  
   208  		csrf = &graphql.CSRFTokenCredentialRequestAuth{
   209  			TokenEndpointURL:      in.Csrf.TokenEndpointURL,
   210  			AdditionalQueryParams: params,
   211  			AdditionalHeaders:     headers,
   212  			Credential:            c.credentialToGraphQL(in.Csrf.Credential),
   213  		}
   214  	}
   215  
   216  	return &graphql.CredentialRequestAuth{
   217  		Csrf: csrf,
   218  	}
   219  }
   220  
   221  func (c *converter) requestAuthInputFromGraphQL(in *graphql.CredentialRequestAuthInput) (*model.CredentialRequestAuthInput, error) {
   222  	if in == nil {
   223  		return nil, nil
   224  	}
   225  
   226  	var csrf *model.CSRFTokenCredentialRequestAuthInput
   227  	if in.Csrf != nil {
   228  		additionalHeaders, err := c.headersFromGraphQL(in.Csrf.AdditionalHeaders, in.Csrf.AdditionalHeadersSerialized)
   229  		if err != nil {
   230  			return nil, errors.Wrap(err, "while converting CSRF AdditionalHeaders from GraphQL input")
   231  		}
   232  
   233  		additionalQueryParams, err := c.queryParamsFromGraphQL(in.Csrf.AdditionalQueryParams, in.Csrf.AdditionalQueryParamsSerialized)
   234  		if err != nil {
   235  			return nil, errors.Wrap(err, "while converting CSRF AdditionalQueryParams from GraphQL input")
   236  		}
   237  
   238  		csrf = &model.CSRFTokenCredentialRequestAuthInput{
   239  			TokenEndpointURL:      in.Csrf.TokenEndpointURL,
   240  			AdditionalQueryParams: additionalQueryParams,
   241  			AdditionalHeaders:     additionalHeaders,
   242  			Credential:            c.credentialInputFromGraphQL(in.Csrf.Credential),
   243  		}
   244  	}
   245  
   246  	return &model.CredentialRequestAuthInput{
   247  		Csrf: csrf,
   248  	}, nil
   249  }
   250  
   251  func (c *converter) requestAuthFromGraphQL(in *graphql.CredentialRequestAuthInput) (*model.CredentialRequestAuth, error) {
   252  	if in == nil {
   253  		return nil, nil
   254  	}
   255  
   256  	var csrf *model.CSRFTokenCredentialRequestAuth
   257  	if in.Csrf != nil {
   258  		additionalHeaders, err := c.headersFromGraphQL(in.Csrf.AdditionalHeaders, in.Csrf.AdditionalHeadersSerialized)
   259  		if err != nil {
   260  			return nil, errors.Wrap(err, "while converting CSRF AdditionalHeaders from GraphQL input")
   261  		}
   262  
   263  		additionalQueryParams, err := c.queryParamsFromGraphQL(in.Csrf.AdditionalQueryParams, in.Csrf.AdditionalQueryParamsSerialized)
   264  		if err != nil {
   265  			return nil, errors.Wrap(err, "while converting CSRF AdditionalQueryParams from GraphQL input")
   266  		}
   267  
   268  		csrf = &model.CSRFTokenCredentialRequestAuth{
   269  			TokenEndpointURL:      in.Csrf.TokenEndpointURL,
   270  			AdditionalQueryParams: additionalQueryParams,
   271  			AdditionalHeaders:     additionalHeaders,
   272  			Credential:            c.credentialModelFromGraphQL(in.Csrf.Credential),
   273  		}
   274  	}
   275  
   276  	return &model.CredentialRequestAuth{
   277  		Csrf: csrf,
   278  	}, nil
   279  }
   280  
   281  func (c *converter) headersFromGraphQL(headers graphql.HTTPHeaders, headersSerialized *graphql.HTTPHeadersSerialized) (map[string][]string, error) {
   282  	var h map[string][]string
   283  
   284  	if headersSerialized != nil {
   285  		return headersSerialized.Unmarshal()
   286  	} else if headers != nil {
   287  		h = headers
   288  	}
   289  
   290  	return h, nil
   291  }
   292  
   293  func (c *converter) queryParamsFromGraphQL(params graphql.QueryParams, paramsSerialized *graphql.QueryParamsSerialized) (map[string][]string, error) {
   294  	var p map[string][]string
   295  
   296  	if paramsSerialized != nil {
   297  		return paramsSerialized.Unmarshal()
   298  	} else if params != nil {
   299  		p = params
   300  	}
   301  
   302  	return p, nil
   303  }
   304  
   305  func (c *converter) credentialInputFromGraphQL(in *graphql.CredentialDataInput) *model.CredentialDataInput {
   306  	if in == nil {
   307  		return nil
   308  	}
   309  
   310  	var basic *model.BasicCredentialDataInput
   311  	var oauth *model.OAuthCredentialDataInput
   312  	var certOAuth *model.CertificateOAuthCredentialDataInput
   313  
   314  	if in.Basic != nil {
   315  		basic = &model.BasicCredentialDataInput{
   316  			Username: in.Basic.Username,
   317  			Password: in.Basic.Password,
   318  		}
   319  	} else if in.Oauth != nil {
   320  		oauth = &model.OAuthCredentialDataInput{
   321  			URL:          in.Oauth.URL,
   322  			ClientID:     in.Oauth.ClientID,
   323  			ClientSecret: in.Oauth.ClientSecret,
   324  		}
   325  	} else if in.CertificateOAuth != nil {
   326  		certOAuth = &model.CertificateOAuthCredentialDataInput{
   327  			ClientID:    in.CertificateOAuth.ClientID,
   328  			Certificate: in.CertificateOAuth.Certificate,
   329  			URL:         in.CertificateOAuth.URL,
   330  		}
   331  	}
   332  
   333  	return &model.CredentialDataInput{
   334  		Basic:            basic,
   335  		Oauth:            oauth,
   336  		CertificateOAuth: certOAuth,
   337  	}
   338  }
   339  
   340  func (c *converter) credentialModelFromGraphQL(in *graphql.CredentialDataInput) model.CredentialData {
   341  	if in == nil {
   342  		return model.CredentialData{}
   343  	}
   344  
   345  	var basic *model.BasicCredentialData
   346  	var oauth *model.OAuthCredentialData
   347  	var certOAuth *model.CertificateOAuthCredentialData
   348  
   349  	if in.Basic != nil {
   350  		basic = &model.BasicCredentialData{
   351  			Username: in.Basic.Username,
   352  			Password: in.Basic.Password,
   353  		}
   354  	} else if in.Oauth != nil {
   355  		oauth = &model.OAuthCredentialData{
   356  			URL:          in.Oauth.URL,
   357  			ClientID:     in.Oauth.ClientID,
   358  			ClientSecret: in.Oauth.ClientSecret,
   359  		}
   360  	} else if in.CertificateOAuth != nil {
   361  		certOAuth = &model.CertificateOAuthCredentialData{
   362  			ClientID:    in.CertificateOAuth.ClientID,
   363  			Certificate: in.CertificateOAuth.Certificate,
   364  			URL:         in.CertificateOAuth.URL,
   365  		}
   366  	}
   367  
   368  	return model.CredentialData{
   369  		Basic:            basic,
   370  		Oauth:            oauth,
   371  		CertificateOAuth: certOAuth,
   372  	}
   373  }
   374  
   375  func (c *converter) credentialToGraphQL(in model.CredentialData) graphql.CredentialData {
   376  	var credential graphql.CredentialData
   377  	if in.Basic != nil {
   378  		credential = graphql.BasicCredentialData{
   379  			Username: in.Basic.Username,
   380  			Password: in.Basic.Password,
   381  		}
   382  	} else if in.Oauth != nil {
   383  		credential = graphql.OAuthCredentialData{
   384  			URL:          in.Oauth.URL,
   385  			ClientID:     in.Oauth.ClientID,
   386  			ClientSecret: in.Oauth.ClientSecret,
   387  		}
   388  	} else if in.CertificateOAuth != nil {
   389  		credential = graphql.CertificateOAuthCredentialData{
   390  			ClientID:    in.CertificateOAuth.ClientID,
   391  			Certificate: in.CertificateOAuth.Certificate,
   392  			URL:         in.CertificateOAuth.URL,
   393  		}
   394  	}
   395  
   396  	return credential
   397  }
   398  
   399  func timestampToTime(timestamp graphql.Timestamp) time.Time {
   400  	return time.Time(timestamp)
   401  }