oauth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package integrations
  2. import (
  3. "context"
  4. "time"
  5. "github.com/digitalocean/godo"
  6. "github.com/porter-dev/porter/api/types"
  7. "gorm.io/gorm"
  8. )
  9. // SharedOAuthModel stores general fields needed for OAuth Integration
  10. type SharedOAuthModel struct {
  11. // The ID issued to the client
  12. ClientID []byte `json:"client-id"`
  13. // The end-users's access token
  14. AccessToken []byte `json:"access-token"`
  15. // The end-user's refresh token
  16. RefreshToken []byte `json:"refresh-token"`
  17. // Time token expires and needs to be refreshed.
  18. // If 0, token will never refresh
  19. Expiry time.Time
  20. }
  21. // OAuthIntegration is an auth mechanism that uses oauth
  22. // https://tools.ietf.org/html/rfc6749
  23. type OAuthIntegration struct {
  24. gorm.Model
  25. SharedOAuthModel
  26. // The name of the auth mechanism
  27. Client types.OAuthIntegrationClient `json:"client"`
  28. // The id of the user that linked this auth mechanism
  29. UserID uint `json:"user_id"`
  30. // The project that this integration belongs to
  31. ProjectID uint `json:"project_id"`
  32. // (optional) an identifying email on the target identity provider.
  33. // for example, for DigitalOcean this is the user's email.
  34. TargetEmail string `json:"target_email"`
  35. // (optional) an identifying string on the target identity provider.
  36. // for example, for DigitalOcean this is the target project name.
  37. TargetName string `json:"target_id"`
  38. // ------------------------------------------------------------------
  39. // All fields encrypted before storage.
  40. // ------------------------------------------------------------------
  41. }
  42. func (g *OAuthIntegration) PopulateTargetMetadata() {
  43. switch g.Client {
  44. case types.OAuthDigitalOcean:
  45. client := godo.NewFromToken(string(g.AccessToken))
  46. account, _, err := client.Account.Get(context.TODO())
  47. if err == nil && account != nil {
  48. g.TargetEmail = account.Email
  49. }
  50. proj, _, err := client.Projects.GetDefault(context.TODO())
  51. if err == nil && proj != nil {
  52. g.TargetName = proj.Name
  53. }
  54. }
  55. }
  56. // ToOAuthIntegrationType generates an external OAuthIntegration to be shared over REST
  57. func (o *OAuthIntegration) ToOAuthIntegrationType() *types.OAuthIntegration {
  58. return &types.OAuthIntegration{
  59. CreatedAt: o.CreatedAt,
  60. ID: o.ID,
  61. Client: o.Client,
  62. UserID: o.UserID,
  63. ProjectID: o.ProjectID,
  64. TargetEmail: o.TargetEmail,
  65. TargetName: o.TargetName,
  66. }
  67. }
  68. // GithubAppOAuthIntegration is the model used for storing github app oauth data
  69. // Unlike the above, this model is tied to a specific user, not a project
  70. type GithubAppOAuthIntegration struct {
  71. gorm.Model
  72. SharedOAuthModel
  73. // The id of the user that linked this auth mechanism
  74. UserID uint `json:"user_id"`
  75. }
  76. // GitlabAppOAuthIntegration is the model used for storing gitlab app oauth data
  77. type GitlabAppOAuthIntegration struct {
  78. gorm.Model
  79. // The ID of the oauth integration linked with this auth mechanism
  80. OAuthIntegrationID uint `json:"oauth_integration_id"`
  81. // The ID of the gitlab integration linked with this auth mechanism
  82. GitlabIntegrationID uint `json:"gitlab_integration_id"`
  83. }