go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/oauth/option.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package oauth
     9  
    10  // Option is an option for oauth managers.
    11  type Option func(*Manager) error
    12  
    13  // OptConfig sets a manager based on a config.
    14  func OptConfig(cfg Config) Option {
    15  	return func(m *Manager) error {
    16  		secret, err := cfg.DecodeSecret()
    17  		if err != nil {
    18  			return err
    19  		}
    20  		m.oauth2.ClientID = cfg.ClientID
    21  		m.oauth2.ClientSecret = cfg.ClientSecret
    22  		m.oauth2.RedirectURL = cfg.RedirectURL
    23  		m.oauth2.Scopes = cfg.ScopesOrDefault()
    24  		m.Secret = secret
    25  		m.HostedDomain = cfg.HostedDomain
    26  		m.AllowedDomains = cfg.AllowedDomains
    27  		return nil
    28  	}
    29  }
    30  
    31  // OptClientID sets the manager cliendID.
    32  func OptClientID(cliendID string) Option {
    33  	return func(m *Manager) error {
    34  		m.oauth2.ClientID = cliendID
    35  		return nil
    36  	}
    37  }
    38  
    39  // OptClientSecret sets the manager clientSecret.
    40  func OptClientSecret(clientSecret string) Option {
    41  	return func(m *Manager) error {
    42  		m.oauth2.ClientSecret = clientSecret
    43  		return nil
    44  	}
    45  }
    46  
    47  // OptRedirectURL sets the manager redirectURI.
    48  func OptRedirectURL(redirectURL string) Option {
    49  	return func(m *Manager) error {
    50  		m.oauth2.RedirectURL = redirectURL
    51  		return nil
    52  	}
    53  }
    54  
    55  // OptScopes sets the manager scopes.
    56  func OptScopes(scopes ...string) Option {
    57  	return func(m *Manager) error {
    58  		m.oauth2.Scopes = scopes
    59  		return nil
    60  	}
    61  }
    62  
    63  // OptSecret sets the manager secret.
    64  func OptSecret(secret []byte) Option {
    65  	return func(m *Manager) error {
    66  		m.Secret = secret
    67  		return nil
    68  	}
    69  }
    70  
    71  // OptHostedDomain sets the manager hostedDomain.
    72  func OptHostedDomain(hostedDomain string) Option {
    73  	return func(m *Manager) error {
    74  		m.HostedDomain = hostedDomain
    75  		return nil
    76  	}
    77  }
    78  
    79  // OptAllowedDomains sets the manager allowedDomains.
    80  func OptAllowedDomains(allowedDomains ...string) Option {
    81  	return func(m *Manager) error {
    82  		m.AllowedDomains = allowedDomains
    83  		return nil
    84  	}
    85  }