1{{> partials.copyHeader }} 2 3package okta 4 5import ( 6 "context" 7 "fmt" 8 "io/ioutil" 9 "os/user" 10 11 "github.com/okta/okta-sdk-golang/v2/okta/cache" 12 13 "github.com/go-yaml/yaml" 14 "github.com/kelseyhightower/envconfig" 15) 16 17const Version = "2.0.0" 18 19type Client struct { 20 config *config 21 22 requestExecutor *RequestExecutor 23 24 resource resource 25 26 {{getClientTagResources operations}} 27} 28 29type resource struct { 30 client *Client 31} 32 33type clientContextKey struct { 34} 35 36func NewClient(ctx context.Context, conf ...ConfigSetter) (context.Context, *Client, error) { 37 config := &config{} 38 39 setConfigDefaults(config) 40 config = readConfigFromSystem(*config) 41 config = readConfigFromApplication(*config) 42 config = readConfigFromEnvironment(*config) 43 44 for _, confSetter := range conf { 45 confSetter(config) 46 } 47 48 var oktaCache cache.Cache 49 if !config.Okta.Client.Cache.Enabled { 50 oktaCache = cache.NewNoOpCache() 51 } else { 52 if config.CacheManager == nil { 53 oktaCache = cache.NewGoCache(config.Okta.Client.Cache.DefaultTtl, 54 config.Okta.Client.Cache.DefaultTti) 55 } else { 56 oktaCache = config.CacheManager 57 } 58 } 59 60 config.CacheManager = oktaCache 61 62 config, err := validateConfig(config) 63 if err != nil { 64 panic(err) 65 } 66 67 c := &Client{} 68 c.config = config 69 c.requestExecutor = NewRequestExecutor(&config.HttpClient, oktaCache, config) 70 71 c.resource.client = c 72 73 {{{getNewClientTagProps operations}}} 74 75 contextReturn := context.WithValue(ctx, clientContextKey{}, c) 76 77 return contextReturn, c, nil 78} 79 80func ClientFromContext(ctx context.Context) (*Client, bool) { 81 u, ok := ctx.Value(clientContextKey{}).(*Client) 82 return u, ok 83} 84 85func (c *Client) GetConfig() *config { 86 return c.config 87} 88 89func (c *Client) GetRequestExecutor() *RequestExecutor { 90 return c.requestExecutor 91} 92 93func setConfigDefaults(c *config) { 94 var conf []ConfigSetter 95 96 conf = append(conf, 97 WithConnectionTimeout(30), 98 WithCache(true), 99 WithCacheTtl(300), 100 WithCacheTti(300), 101 WithUserAgentExtra(""), 102 WithTestingDisableHttpsCheck(false), 103 WithRequestTimeout(0), 104 WithRateLimitMaxRetries(2), 105 WithAuthorizationMode("SSWS")) 106 107 for _, confSetter := range conf { 108 confSetter(c) 109 } 110} 111 112func readConfigFromFile(location string, c config) (*config, error) { 113 yamlConfig, err := ioutil.ReadFile(location) 114 115 if err != nil { 116 return nil, err 117 } 118 119 err = yaml.Unmarshal(yamlConfig, &c) 120 if err != nil { 121 return nil, err 122 } 123 124 return &c, err 125} 126 127func readConfigFromSystem(c config) *config { 128 currUser, err := user.Current() 129 if err != nil { 130 return &c 131 } 132 if currUser.HomeDir == "" { 133 return &c 134 } 135 136 conf, err := readConfigFromFile(currUser.HomeDir + "/.okta/okta.yaml", c) 137 138 if err != nil { 139 return &c 140 } 141 142 return conf 143} 144 145func readConfigFromApplication(c config) *config { 146 conf, err := readConfigFromFile(".okta.yaml", c) 147 148 if err != nil { 149 return &c 150 } 151 152 return conf 153} 154 155func readConfigFromEnvironment(c config) *config { 156 err := envconfig.Process("okta", &c) 157 if err != nil { 158 fmt.Println("error parsing") 159 return &c 160 } 161 return &c 162} 163