Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user email to upstash integration #4639

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
}
Loading