|
|
@@ -1,8 +1,10 @@
|
|
|
package integrations
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/digitalocean/godo"
|
|
|
"github.com/porter-dev/porter/api/types"
|
|
|
"gorm.io/gorm"
|
|
|
)
|
|
|
@@ -38,11 +40,38 @@ type OAuthIntegration struct {
|
|
|
// The project that this integration belongs to
|
|
|
ProjectID uint `json:"project_id"`
|
|
|
|
|
|
+ // (optional) an identifying email on the target identity provider.
|
|
|
+ // for example, for DigitalOcean this is the user's email.
|
|
|
+ TargetEmail string `json:"target_email"`
|
|
|
+
|
|
|
+ // (optional) an identifying string on the target identity provider.
|
|
|
+ // for example, for DigitalOcean this is the target project name.
|
|
|
+ TargetName string `json:"target_id"`
|
|
|
+
|
|
|
// ------------------------------------------------------------------
|
|
|
// All fields encrypted before storage.
|
|
|
// ------------------------------------------------------------------
|
|
|
}
|
|
|
|
|
|
+func (g *OAuthIntegration) PopulateTargetMetadata() {
|
|
|
+ switch g.Client {
|
|
|
+ case types.OAuthDigitalOcean:
|
|
|
+ client := godo.NewFromToken(string(g.AccessToken))
|
|
|
+
|
|
|
+ account, _, err := client.Account.Get(context.TODO())
|
|
|
+
|
|
|
+ if err == nil && account != nil {
|
|
|
+ g.TargetEmail = account.Email
|
|
|
+ }
|
|
|
+
|
|
|
+ proj, _, err := client.Projects.GetDefault(context.TODO())
|
|
|
+
|
|
|
+ if err == nil && proj != nil {
|
|
|
+ g.TargetName = proj.Name
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// GithubAppOAuthIntegration is the model used for storing github app oauth data
|
|
|
// Unlike the above, this model is tied to a specific user, not a project
|
|
|
type GithubAppOAuthIntegration struct {
|
|
|
@@ -56,10 +85,12 @@ type GithubAppOAuthIntegration struct {
|
|
|
// ToOAuthIntegrationType generates an external OAuthIntegration to be shared over REST
|
|
|
func (o *OAuthIntegration) ToOAuthIntegrationType() *types.OAuthIntegration {
|
|
|
return &types.OAuthIntegration{
|
|
|
- CreatedAt: o.CreatedAt,
|
|
|
- ID: o.ID,
|
|
|
- Client: o.Client,
|
|
|
- UserID: o.UserID,
|
|
|
- ProjectID: o.ProjectID,
|
|
|
+ CreatedAt: o.CreatedAt,
|
|
|
+ ID: o.ID,
|
|
|
+ Client: o.Client,
|
|
|
+ UserID: o.UserID,
|
|
|
+ ProjectID: o.ProjectID,
|
|
|
+ TargetEmail: o.TargetEmail,
|
|
|
+ TargetName: o.TargetName,
|
|
|
}
|
|
|
}
|