github.com/companieshouse/insolvency-api@v0.0.0-20231024103413-440c973d9e9b/service/efs_submission_service.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"regexp"
     7  
     8  	"github.com/companieshouse/chs.go/log"
     9  	"github.com/companieshouse/go-sdk-manager/manager"
    10  	"github.com/companieshouse/insolvency-api/config"
    11  )
    12  
    13  // IsUserOnEfsAllowList uses the sdk to call the EFS api and return a boolean depending on whether or not the email
    14  // address is on the allow list
    15  func IsUserOnEfsAllowList(emailAddress string, req *http.Request) (bool, error) {
    16  	api, err := manager.GetPrivateSDK(req)
    17  	if err != nil {
    18  		return false, fmt.Errorf("error creating private SDK to call transaction api: [%v]", err.Error())
    19  	}
    20  
    21  	// Get environment config - only required whilst feature flag to disable EFS lookup exists
    22  	cfg, err := config.Get()
    23  	if err != nil {
    24  		return false, fmt.Errorf("error configuring service: %w. Exiting", err)
    25  	}
    26  
    27  	// Check from Env Var or Command Line Flag if EFS Allow List Auth has been disabled AND email address contains
    28  	// 'magic string' in which case the API call is bypassed and a 'true' value is returned to parent
    29  	if cfg.IsEfsAllowListAuthDisabled {
    30  		// Our 'magic string' to bypass EFS Allow List if it is in email address is 'ip-test'
    31  		isMatch, err := regexp.MatchString("ip-test", emailAddress)
    32  		if err != nil {
    33  			return false, fmt.Errorf("EFS Allow List API call disabled by environment variable, but unable to check email address for regex match")
    34  		}
    35  		log.InfoR(req, fmt.Sprintf("EFS Allow List API call disabled by environment variable for email address: %s. Mocked API response: %t", emailAddress, isMatch))
    36  		return isMatch, nil
    37  	}
    38  
    39  	isUserAllowed, err := api.Efs.IsUserOnAllowList(emailAddress).Do()
    40  
    41  	if err != nil {
    42  		return false, fmt.Errorf("error communicating with the EFS submission api: [%s]", err)
    43  	}
    44  
    45  	if isUserAllowed == nil {
    46  		return false, fmt.Errorf("error communicating with the EFS submission API: no response received")
    47  	}
    48  
    49  	return isUserAllowed.UserAllowed, nil
    50  }