Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
DA-4596: update da-lib version to latest
Browse files Browse the repository at this point in the history
Signed-off-by: Ajinkya Nahar <[email protected]>
  • Loading branch information
Ajinkya Nahar committed Dec 21, 2021
1 parent 38c6de2 commit 30c7be9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
62 changes: 28 additions & 34 deletions auth0/jwks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,45 @@ func (a *ClientProvider) createAuthJwks(cert string) error {

func (a *ClientProvider) getPemCert(token *jwt.Token, refreshJwks bool) (string, error) {
cert := ""
cert, expired, err := a.getCachedJwks()
cert, err := a.getCachedJwks()
if err != nil {
return cert, err
}

// check if the cache expired as well is not invoked via refresh token cron
if !expired && !refreshJwks {
return cert, nil
}
// check if the refresh jwks cache flag coming from the refresh cron is set to true
if refreshJwks {
_, resp, err := a.httpClient.Request(fmt.Sprintf("%s/oauth/.well-known/jwks.json", a.AuthURL), "GET", nil, nil, nil)
if err != nil {
return cert, err
}

_, resp, err := a.httpClient.Request(fmt.Sprintf("%s/oauth/.well-known/jwks.json", a.AuthURL), "GET", nil, nil, nil)
if err != nil {
return cert, err
}
var jwks = Jwks{}
if err := json.Unmarshal(resp, &jwks); err != nil {
return cert, err
}

var jwks = Jwks{}
if err := json.Unmarshal(resp, &jwks); err != nil {
return cert, err
}
for _, k := range jwks.Keys {
if token.Header["kid"] == k.Kid {
cert = "-----BEGIN CERTIFICATE-----\n" + k.X5c[0] + "\n-----END CERTIFICATE-----"
}
}

for _, k := range jwks.Keys {
if token.Header["kid"] == k.Kid {
cert = "-----BEGIN CERTIFICATE-----\n" + k.X5c[0] + "\n-----END CERTIFICATE-----"
if cert == "" {
err := errors.New("unable to find appropriate key")
return cert, err
}
}

if cert == "" {
err := errors.New("unable to find appropriate key")
return cert, err
}
err = a.createAuthJwks(cert)
if err != nil {
return "", err
}

err = a.createAuthJwks(cert)
if err != nil {
return "", err
}

return cert, nil
}

func (a *ClientProvider) getCachedJwks() (string, bool, error) {
expired := true
func (a *ClientProvider) getCachedJwks() (string, error) {
res, err := a.esClient.Search(strings.TrimSpace(auth0JwksCache+a.Environment), searchJwksQuery)
if err != nil {
go func() {
Expand All @@ -93,27 +91,23 @@ func (a *ClientProvider) getCachedJwks() (string, bool, error) {
fmt.Println("Err: send to slack: ", err)
}()

return "", expired, err
return "", err
}

var e ESJwksSchema
err = json.Unmarshal(res, &e)
if err != nil {
log.Println("repository: GetOauthJwks: could not unmarshal the data", err)
return "", expired, err
return "", err
}

if len(e.Hits.Hits) > 0 {
data := e.Hits.Hits[0]
// compare current time v/s existing cached time + 30 mins
if data.Source.CreatedAt.Add(30*time.Minute).Unix() <= time.Now().UTC().Unix() {
expired = false
}

return data.Source.Jwks, expired, nil
return data.Source.Jwks, nil
}

return "", expired, errors.New("GetJwks: could not find the associated jwks")
return "", errors.New("GetJwks: could not find the associated jwks")
}

var searchJwksQuery = map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/LF-Engineering/insights-datasource-shared
go 1.15

require (
github.com/LF-Engineering/dev-analytics-libraries v1.1.26
github.com/LF-Engineering/dev-analytics-libraries v1.1.28
github.com/avast/retry-go v3.0.0+incompatible
github.com/aws/aws-sdk-go v1.42.24
github.com/aws/aws-sdk-go-v2 v1.11.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/LF-Engineering/dev-analytics-libraries v1.1.26 h1:NS07Nkh0waOVl5d9PkIQcWEovsshbdoZcJPaVf8SZQw=
github.com/LF-Engineering/dev-analytics-libraries v1.1.26/go.mod h1:O+9mOX1nf6qGKrZne33F6speSzrGj6+Y1tPF6jh/mcw=
github.com/LF-Engineering/dev-analytics-libraries v1.1.28 h1:sjmYNPSY3hXUl2+ouCqn+Xq7AmHkto9/5PsCV/7eYBw=
github.com/LF-Engineering/dev-analytics-libraries v1.1.28/go.mod h1:O+9mOX1nf6qGKrZne33F6speSzrGj6+Y1tPF6jh/mcw=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
Expand Down

0 comments on commit 30c7be9

Please sign in to comment.