github.com/greenpau/go-authcrunch@v1.1.4/pkg/authproxy/config.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package authproxy
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/pkg/errors"
    19  	cfgutil "github.com/greenpau/go-authcrunch/pkg/util/cfg"
    20  	"strings"
    21  )
    22  
    23  // BasicAuthConfig is a config for basic authentication.
    24  type BasicAuthConfig struct {
    25  	Enabled bool                   `json:"enabled,omitempty" xml:"enabled,omitempty" yaml:"enabled,omitempty"`
    26  	Realms  map[string]interface{} `json:"realms,omitempty" xml:"realms,omitempty" yaml:"realms,omitempty"`
    27  }
    28  
    29  // APIKeyAuthConfig is a config for API key-based authentication.
    30  type APIKeyAuthConfig struct {
    31  	Enabled bool                   `json:"enabled,omitempty" xml:"enabled,omitempty" yaml:"enabled,omitempty"`
    32  	Realms  map[string]interface{} `json:"realms,omitempty" xml:"realms,omitempty" yaml:"realms,omitempty"`
    33  }
    34  
    35  // Config is a config for an identity provider.
    36  type Config struct {
    37  	PortalName string           `json:"portal_name,omitempty" xml:"portal_name,omitempty" yaml:"portal_name,omitempty"`
    38  	BasicAuth  BasicAuthConfig  `json:"basic_auth,omitempty" xml:"basic_auth,omitempty" yaml:"basic_auth,omitempty"`
    39  	APIKeyAuth APIKeyAuthConfig `json:"api_key_auth,omitempty" xml:"api_key_auth,omitempty" yaml:"api_key_auth,omitempty"`
    40  }
    41  
    42  // ParseConfig parses configuration into an identity provider config
    43  func ParseConfig(lines []string) (*Config, error) {
    44  	m := make(map[string]*Config)
    45  	if len(lines) == 0 {
    46  		return nil, errors.ErrAuthProxyConfigInvalid.WithArgs("empty config")
    47  	}
    48  	for _, encodedLine := range lines {
    49  		var portalName string
    50  		realmName := "local"
    51  		var cfg *Config
    52  		arr, err := cfgutil.DecodeArgs(encodedLine)
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  		switch {
    57  		case strings.HasPrefix(encodedLine, "basic auth"):
    58  			arr = arr[2:]
    59  		case strings.HasPrefix(encodedLine, "api key auth"):
    60  			arr = arr[3:]
    61  		default:
    62  			return nil, errors.ErrAuthProxyConfigInvalid.WithArgs(encodedLine)
    63  		}
    64  		if len(arr) > 0 {
    65  			for {
    66  				if len(arr) == 0 {
    67  					break
    68  				}
    69  				if (len(arr) % 2) > 0 {
    70  					return nil, errors.ErrAuthProxyConfigInvalid.WithArgs(encodedLine)
    71  				}
    72  				k := arr[0]
    73  				switch k {
    74  				case "portal":
    75  					portalName = arr[1]
    76  					arr = arr[2:]
    77  				case "realm":
    78  					realmName = arr[1]
    79  					arr = arr[2:]
    80  				default:
    81  					return nil, errors.ErrAuthProxyConfigInvalid.WithArgs(encodedLine)
    82  				}
    83  			}
    84  		}
    85  
    86  		if portalName == "" {
    87  			return nil, errors.ErrAuthProxyConfigInvalid.WithArgs(encodedLine)
    88  		}
    89  
    90  		if _, exists := m[portalName]; exists {
    91  			cfg = m[portalName]
    92  		} else {
    93  			cfg = &Config{
    94  				PortalName: portalName,
    95  			}
    96  			m[portalName] = cfg
    97  		}
    98  
    99  		switch {
   100  		case strings.HasPrefix(encodedLine, "basic auth"):
   101  			cfg.BasicAuth.Enabled = true
   102  			if cfg.BasicAuth.Realms == nil {
   103  				cfg.BasicAuth.Realms = make(map[string]interface{})
   104  			}
   105  			cfg.BasicAuth.Realms[realmName] = true
   106  		case strings.HasPrefix(encodedLine, "api key auth"):
   107  			cfg.APIKeyAuth.Enabled = true
   108  			if cfg.APIKeyAuth.Realms == nil {
   109  				cfg.APIKeyAuth.Realms = make(map[string]interface{})
   110  			}
   111  			cfg.APIKeyAuth.Realms[realmName] = true
   112  		}
   113  	}
   114  
   115  	if len(m) > 1 {
   116  		return nil, errors.ErrAuthProxyConfigInvalid.WithArgs("multiple portals")
   117  	}
   118  	var providers []*Config
   119  	for _, provider := range m {
   120  		providers = append(providers, provider)
   121  	}
   122  	return providers[0], nil
   123  }