44package okta
55
66import (
7+ "bytes"
78 "context"
9+ "encoding/json"
810 "fmt"
11+ "io"
912 "net/http"
1013 "net/url"
1114 "strings"
@@ -16,7 +19,7 @@ import (
1619 "github.com/hashicorp/vault/sdk/framework"
1720 "github.com/hashicorp/vault/sdk/helper/tokenutil"
1821 "github.com/hashicorp/vault/sdk/logical"
19- oktanew "github.com/okta/okta-sdk-golang/v2 /okta"
22+ oktanew "github.com/okta/okta-sdk-golang/v5 /okta"
2023)
2124
2225const (
@@ -290,36 +293,127 @@ func (b *backend) pathConfigExistenceCheck(ctx context.Context, req *logical.Req
290293}
291294
292295type oktaShim interface {
293- Client () (* oktanew.Client , context.Context )
296+ Client () (* oktanew.APIClient , context.Context )
294297 NewRequest (method string , url string , body interface {}) (* http.Request , error )
295298 Do (req * http.Request , v interface {}) (interface {}, error )
296299}
297300
298301type oktaShimNew struct {
299- client * oktanew.Client
302+ cfg * oktanew.Configuration
303+ client * oktanew.APIClient
300304 ctx context.Context
301305}
302306
303- func (new * oktaShimNew ) Client () (* oktanew.Client , context.Context ) {
307+ func (new * oktaShimNew ) Client () (* oktanew.APIClient , context.Context ) {
304308 return new .client , new .ctx
305309}
306310
307311func (new * oktaShimNew ) NewRequest (method string , url string , body interface {}) (* http.Request , error ) {
308312 if ! strings .HasPrefix (url , "/" ) {
309313 url = "/api/v1/" + url
310314 }
311- return new .client .GetRequestExecutor ().NewRequest (method , url , body )
315+
316+ // reimplementation of RequestExecutor.NewRequest() in v2 of okta-golang-sdk
317+ var buff io.ReadWriter
318+ if body != nil {
319+ switch v := body .(type ) {
320+ case []byte :
321+ buff = bytes .NewBuffer (v )
322+ case * bytes.Buffer :
323+ buff = v
324+ default :
325+ buff = & bytes.Buffer {}
326+ // need to create an encoder specifically to disable html escaping
327+ encoder := json .NewEncoder (buff )
328+ encoder .SetEscapeHTML (false )
329+ err := encoder .Encode (body )
330+ if err != nil {
331+ return nil , err
332+ }
333+ }
334+ }
335+
336+ url = new .cfg .Okta .Client .OrgUrl + url
337+ //url = re.config.Okta.Client.OrgUrl + url
338+ //
339+ req , err := http .NewRequest (method , url , buff )
340+ if err != nil {
341+ return nil , err
342+ }
343+ //
344+ var auth oktanew.Authorization
345+ //
346+ switch new .cfg .Okta .Client .AuthorizationMode {
347+ case "SSWS" :
348+ auth = oktanew .NewSSWSAuth (new .cfg .Okta .Client .Token , req )
349+ case "Bearer" :
350+ auth = oktanew .NewBearerAuth (new .cfg .Okta .Client .Token , req )
351+ case "PrivateKey" :
352+ auth = oktanew .NewPrivateKeyAuth (oktanew.PrivateKeyAuthConfig {
353+ // TokenCache: new.cfg., hmm
354+ HttpClient : new .cfg .HTTPClient ,
355+ PrivateKeySigner : new .cfg .PrivateKeySigner ,
356+ PrivateKey : new .cfg .Okta .Client .PrivateKey ,
357+ PrivateKeyId : new .cfg .Okta .Client .PrivateKeyId ,
358+ ClientId : new .cfg .Okta .Client .ClientId ,
359+ OrgURL : new .cfg .Okta .Client .OrgUrl ,
360+ Scopes : new .cfg .Okta .Client .Scopes ,
361+ MaxRetries : new .cfg .Okta .Client .RateLimit .MaxRetries ,
362+ MaxBackoff : new .cfg .Okta .Client .RateLimit .MaxBackoff ,
363+ Req : req ,
364+ })
365+ case "JWT" :
366+ auth = oktanew .NewJWTAuth (oktanew.JWTAuthConfig {
367+ // TokenCache: new.cfg.etokenCache,
368+ HttpClient : new .cfg .HTTPClient ,
369+ OrgURL : new .cfg .Okta .Client .OrgUrl ,
370+ Scopes : new .cfg .Okta .Client .Scopes ,
371+ ClientAssertion : new .cfg .Okta .Client .ClientAssertion ,
372+ MaxRetries : new .cfg .Okta .Client .RateLimit .MaxRetries ,
373+ MaxBackoff : new .cfg .Okta .Client .RateLimit .MaxBackoff ,
374+ Req : req ,
375+ })
376+ default :
377+ return nil , fmt .Errorf ("unknown authorization mode %v" , new .cfg .Okta .Client .AuthorizationMode )
378+ }
379+
380+ err = auth .Authorize ("POST" , url )
381+ if err != nil {
382+ return nil , err
383+ }
384+
385+ // req.Header.Add("User-Agent", NewUserAgent(re.config).String())
386+ req .Header .Add ("Accept" , "application/json" )
387+
388+ if body != nil {
389+ req .Header .Set ("Content-Type" , "application/json" )
390+ }
391+ //
392+ //// Force reset defaults
393+ //re.binary = false
394+ //re.headerAccept = "application/json"
395+ //re.headerContentType = "application/json"
396+ //return req, nil
397+
398+ return req , nil
312399}
313400
314401func (new * oktaShimNew ) Do (req * http.Request , v interface {}) (interface {}, error ) {
315- return new .client .GetRequestExecutor ().Do (new .ctx , req , v )
402+ resp , err := new .cfg .HTTPClient .Do (req )
403+ if err != nil {
404+ return nil , err
405+ }
406+
407+ defer resp .Body .Close ()
408+
409+ return nil , nil
316410}
317411
318412type oktaShimOld struct {
319413 client * oktaold.Client
320414}
321415
322- func (new * oktaShimOld ) Client () (* oktanew.Client , context.Context ) {
416+ func (new * oktaShimOld ) Client () (* oktanew.APIClient , context.Context ) {
323417 return nil , nil
324418}
325419
@@ -331,6 +425,24 @@ func (new *oktaShimOld) Do(req *http.Request, v interface{}) (interface{}, error
331425 return new .client .Do (req , v )
332426}
333427
428+ func (c * ConfigEntry ) OktaConfiguration (ctx context.Context ) (* oktanew.Configuration , error ) {
429+ baseURL := defaultBaseURL
430+ if c .Production != nil {
431+ if ! * c .Production {
432+ baseURL = previewBaseURL
433+ }
434+ }
435+ if c .BaseURL != "" {
436+ baseURL = c .BaseURL
437+ }
438+
439+ cfg , err := oktanew .NewConfiguration (oktanew .WithOrgUrl ("https://" + c .Org + "." + baseURL ), oktanew .WithToken (c .Token ))
440+ if err != nil {
441+ return nil , err
442+ }
443+ return cfg , nil
444+ }
445+
334446// OktaClient creates a basic okta client connection
335447func (c * ConfigEntry ) OktaClient (ctx context.Context ) (oktaShim , error ) {
336448 baseURL := defaultBaseURL
@@ -344,13 +456,13 @@ func (c *ConfigEntry) OktaClient(ctx context.Context) (oktaShim, error) {
344456 }
345457
346458 if c .Token != "" {
347- ctx , client , err := oktanew .NewClient ( ctx ,
459+ cfg , err := oktanew .NewConfiguration (
348460 oktanew .WithOrgUrl ("https://" + c .Org + "." + baseURL ),
349461 oktanew .WithToken (c .Token ))
350462 if err != nil {
351463 return nil , err
352464 }
353- return & oktaShimNew {client , ctx }, nil
465+ return & oktaShimNew {cfg , oktanew . NewAPIClient ( cfg ) , ctx }, nil
354466 }
355467 client , err := oktaold .NewClientWithDomain (cleanhttp .DefaultClient (), c .Org , baseURL , "" )
356468 if err != nil {
0 commit comments