github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/oauth2/file_repository.go (about)

     1  package oauth2
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"sync"
     8  
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  // FileSystemRepository represents a mongodb repository
    13  type FileSystemRepository struct {
    14  	*InMemoryRepository
    15  	sync.Mutex
    16  }
    17  
    18  // NewFileSystemRepository creates a mongo OAuth Server repo
    19  func NewFileSystemRepository(dir string) (*FileSystemRepository, error) {
    20  	repo := &FileSystemRepository{InMemoryRepository: NewInMemoryRepository()}
    21  
    22  	// Grab json files from directory
    23  	files, err := ioutil.ReadDir(dir)
    24  	if nil != err {
    25  		return nil, err
    26  	}
    27  
    28  	for _, f := range files {
    29  		if filepath.Ext(f.Name()) == ".json" {
    30  			filePath := filepath.Join(dir, f.Name())
    31  			oauthServerRaw, err := ioutil.ReadFile(filePath)
    32  			if err != nil {
    33  				log.WithError(err).WithField("path", filePath).Error("Couldn't load the oauth server file")
    34  				return nil, err
    35  			}
    36  
    37  			oauthServer := repo.parseOAuthServer(oauthServerRaw)
    38  			if err = repo.Add(oauthServer); err != nil {
    39  				log.WithError(err).Error("Can't add the definition to the repository")
    40  				return nil, err
    41  			}
    42  		}
    43  	}
    44  
    45  	return repo, nil
    46  }
    47  
    48  func (r *FileSystemRepository) parseOAuthServer(oauthServerRaw []byte) *OAuth {
    49  	oauthServer := NewOAuth()
    50  	if err := json.Unmarshal(oauthServerRaw, oauthServer); err != nil {
    51  		log.WithError(err).Error("[RPC] --> Couldn't unmarshal oauth server configuration")
    52  	}
    53  
    54  	return oauthServer
    55  }