Skip to content

Commit

Permalink
add user email to upstash integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen committed May 13, 2024
1 parent 27359a7 commit 47ff3ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions api/server/handlers/oauth_callback/upstash.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"time"

"github.com/golang-jwt/jwt"
"github.com/porter-dev/porter/api/server/handlers"
"github.com/porter-dev/porter/api/server/shared"
"github.com/porter-dev/porter/api/server/shared/apierrors"
Expand Down Expand Up @@ -100,6 +101,25 @@ func (p *OAuthCallbackUpstashHandler) ServeHTTP(w http.ResponseWriter, r *http.R
return
}

t, _, err := new(jwt.Parser).ParseUnverified(token.AccessToken, jwt.MapClaims{}) // safe to use because we validated the token above
if err != nil {
err = telemetry.Error(ctx, span, err, "error parsing token")
p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

var email string
if claims, ok := t.Claims.(jwt.MapClaims); ok {
if emailVal, ok := claims["https://user.io/email"].(string); ok {
email = emailVal
}
}
if email == "" {
err = telemetry.Error(ctx, span, nil, "email not found in token")
p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
return
}

// make an http call to https://api.upstash.com/apikey with authorization: bearer <access_token>
// to get the api key
apiKey, err := fetchUpstashApiKey(ctx, token.AccessToken)
Expand All @@ -117,6 +137,7 @@ func (p *OAuthCallbackUpstashHandler) ServeHTTP(w http.ResponseWriter, r *http.R
},
ProjectID: projID,
DeveloperApiKey: []byte(apiKey),
UpstashEmail: email,
}

_, err = p.Repo().UpstashIntegration().Insert(ctx, oauthInt)
Expand Down
2 changes: 2 additions & 0 deletions internal/models/integrations/upstash.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ type UpstashIntegration struct {
SharedOAuthModel

DeveloperApiKey []byte `json:"developer_api_key"`

UpstashEmail string `json:"upstash_email"`
}

0 comments on commit 47ff3ba

Please sign in to comment.